Deploying Your Ruby on Rails App to Heroku

Zach Weber
3 min readJun 18, 2020

--

After building my largest app to date, I wanted to be able to show it to friends and family, as well as prospective employers as part of my resume. In order to do that, I knew that my app would need to be hosted on a cloud service provider, such as Elastic Beanstalk (AWS), Google App Engine, Heroku, and Dokku. Each of them have their pros and cons, so do do your research before picking one.

Having worked at a few startups, I was familiar with using Heroku as a staging environment for internal testing of our software, but had never tried to host one of my own apps on it. Since I was already familiar with Heroku from a frontend user perspective, I decided to give it a try as my PaaS (Platform as a Service). Due to this, I used PostgreSQL as my database management system when creating my Rails backend, which is required when using Heroku with Rails projects.

Overall, I found Heroku to be rather user-friendly. Setting up the React.js frontend was pretty simple (see my post on how to do that), but I did find deploying the backend to be a bit more challenging and time-consuming. I used Heroku’s official documentation as a guide, but I’ll walk through the steps I took, too.

1. Create a Heroku account

You can do that here. It’s easy and free. Once your account has been created, just click “New”, then “Create new app”, then add the app’s name and the region you live in (U.S. or Europe).

2. Install the Heroku CLI on your machine

Once you’ve created an account, you’ll need to install Heroku CLI on your local machine:

$ brew tap heroku

After you’ve installed Heroku, you must sign into your Heroku account through the CLI:

$ heroku login
heroku: Enter your Heroku credentials
Email: user@email.com
Password: yourPassword

3. Create your Rails project with PostgreSQL

Next, if you haven’t already done so, you’ll need to create your Rails project. For me, I wanted to use Rails as an API, so I used this command:

$ rails new <my-project> --api -T --database=postgresql

I realize that’s a lot of commands in a row, so let’s break it down:

rails new <my-project> = this creates a new Rails projects

--api = this tells Rails you don’t want any of the stuff you wouldn’t need for an application where Rails is not rendering views

--database=postgresql = this sets up your backend to use a Postgres Database so you’re able to deploy to Heroku. As a result, you will need to run rails db:create and have Postgres running while your app is in development

4. Push your code to GitHub

Heroku works hand in hand with GitHub, so make sure that you have an account. After you’ve created your Rails project and written some code, you’ll want to open a repository, then push your code to that repository:

$ git init
$ git add .
$ git commit -m "first commit"

5. Deploy your project to Heroku

Once you’ve committed your code to GitHub, you need to create your Heroku app:

$ heroku create

Next, you must push the code that’s been committed to GitHub to your newly created Heroku app:

$ git push heroku master

6. Migrate (and seed) your Heroku database

Similar to ActiveRecord, you have to migrate your database in Heroku:

$ heroku run rake db:migrate

If you have any seed data, you’ll also need to run $ heroku run rake db:seed.

For a full list of Heroku rake commands, go here.

Conclusion

After this, your Rails project is ready to be used! If you’re using your app as an API, all you need to do is pull the API from your Heroku app link instead of your local machine (i.e. localhost:3000). For more detail on how to set up development and production environments for your fully deployed Heroku app, check out this post of mine. Happy coding!

--

--

Zach Weber

Denver-based Software Engineer, nature lover, and lifelong learner