Deploy a Rails App or API to Heroku on Ubuntu

Christopher Dent
3 min readAug 13, 2021

Hello readers. This is a quick writeup on how to deploy a Rails APP or a Rails API backend to Heroku so you can use it to communicate with your frontend. It’s pretty simple but I do find myself looking up the steps time after time so I wanted to summarize it all in one place, and why not here in the ol’ blog.

First if you don’t already have the Heroku command line installed, you should go ahead and install that.

sudo snap install --classic heroku

Easy enough! Next step, login to Heroku via the command line:

heroku login

Heroku doesn’t support the Rails default database, SQLite, so you’ve got to make sure your app is using something else, and PostgreSQL is your best bet here. This is a small tweak to your gemfile — comment out or delete gem 'sqlite3' and replace it with gem 'pg' .

Tip: if you know you are going to deploy your web app, you can start with PG as your default database with a database flag as follows:

rails new <projectname>— api — database=postgresql

This may save you some headaches down the road! Moving along…

Your project needs to be in a git repo before you go any further. It should already be, but if not

git init

And then the usual

git add .
git commit -m "deployment preparation"

We’re almost there. Now you’re going to connect to depoy:

heroku create
git push heroku master

Note: As part of their efforts to promote equality and eliminate unnecessary references to slavery, GitHub has been moved towards using main instead of master. If your repo was created via GitHub and you followed their instructions, you might have to run:

git push heroku main

You’ll get a ton of output, and towards the end you’ll get your app’s URL. Nice! Now just migrate your database:

heroku run rake db:migrate

Final fun fact: there are lots of interesting Heroku commands you can run from the command line! I find

heroku run rails console

super helpful for debugging or altering the backend of a live app.

That my friends is it. I know, not the most exciting blog entry! But as I said I find myself looking these steps up over and over again so I wanted to jot it down in my own space for future reference and in case anyone else wants some straightforward steps to deploying on Heroku.

If you do stumble on this article and you’re new to all this, one thing to keep in mind is that Heroku’s free plan will put your apps to ‘sleep’ when you’re not using them. That means they’ll take forever to load when accessed for the first time in a while. If the app is just for you that’s fine, but if it’s for demo purposes, you’ll want to add appropriate loading indicators so your users know something is going on back there, especially with an API (the load times aren’t nearly as bad with a straight up Rails app). If you’re using your app professionally, you’re going to want to pay them their money for some dynos — but this gets expensive quick and if this is your goal you might want to explore some other options like AWS.

--

--

Christopher Dent

student of code. var my_homes = [‘NY’, ‘MTL’, ‘DC’, ‘EDI’, ‘FL’, ‘?’]