How to Deploy Your Rails App to Heroku

Peyton Doyle
4 min readSep 25, 2019

--

Picture this…you have created an amazing Rails app that you love dearly, but no one can see it beyond people at your computer via rails s . Tragic. Thankfully, there is a solution: Heroku. Heroku is a service that allows you to deploy your Rails app to a website that all of your friends and family can see, without being tied to a local server.

There are a few steps needed to prepare your environment to get your Rails app on Heroku.

  1. Download Postgres.app. Postgres.app helps read/process your app’s database.
  2. Make sure you’ve initialized your local repository and committed to it. I won’t be covering how to push an existing local directory to a new Github repo, but instructions on that can be found here.

If you built your app with your database in SQLite, you will need to convert the database to be in PostgreSQL. If your database is already in PostgreSQL, skip to heroku set up below. Though this process appears to be incredibly daunting, it can be accomplished in just a few steps.

  1. Open your directory in your text editor of choice.
  2. Delete your Gemfile.lock.
  3. Make the following changes to your Gemfile to delete the SQLite gem and add the PostgreSQL gem.
    • Delete gem 'sqlite3'.
    • Add gem 'pg'.
  4. Run bundle install in your command line.
  5. Locate your database.yml file in your config folder. Comment out all existing code and replace it with the code below.
  6. Make sure the Postgres.app is up and running.
  7. Run rake db:setup,rake db:migrate,rake db:seed and rails sto get your app up and running.
  8. Go to localhost:3000 in your browser to confirm everything is good to go.
//Code for SQLite to PostgreSQL: Step 5
//Replace your database.yml file with the above
development:
adapter: postgresql
encoding: unicode
database: demo_test_development
pool: 5
username: <your-computer’s-user-name>
password:
timeout: 5000
test:
adapter: postgresql
encoding: unicode
database: demo_test_test
pool: 5
username: <your-computer’s-user-name>
password:
timeout: 5000

There are a few quick steps to make sure your Heroku account is up and running.

  1. Make a Heroku account.
  2. Install the Heroku CLI. Installer available here or on the command line via Homebrew: brew tap heroku/brew && brew install heroku.

You are almost there. You have your database in PostgreSQL and your Heroku account up and running. Time to push your repository to Heroku.

  1. Run heroku create. Your app information will display after successfully executed.
  2. Run git push heroku master to push files to Heroku. Beware: this could take a while!
  3. Run heroku run rake db:migrate and heroku run rake db:seed.
  4. Run heroku open to see your app up and running!

You should be able to share your Heroku link with your friends, family and potential future employers to show off your great web app!

This process wasn’t an easy one for me to figure out and it took a lot longer than expected. Here are some helpful hints to make the process as smooth as possible:

  • When creating new Rails apps going forward, use the command rails new myapp --database=postgresql, so your apps will now be configured for Postgres. You won’t need to convert from SQLite again!
  • If you make changes to your app content including migrations or seed data, be sure to run through steps 2–4 in the pushing to heroku section so your app remains up to date.
  • If you run into errors when you try to open your Heroku app, check the logs (by running heroku logs) to figure out what went wrong. Google the error– there are tons of resources that are available for troubleshooting.

This certainly is an error-prone process, but Google remains an amazing source for debugging. It helped me figure it out and certain could help you too. Enjoy your newly-hosted web app!

--

--