How to Deploy Your Rails App to Heroku
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.
- Download Postgres.app. Postgres.app helps read/process your app’s database.
- 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.
- Open your directory in your text editor of choice.
- Delete your
Gemfile.lock
. - Make the following changes to your
Gemfile
to delete the SQLite gem and add the PostgreSQL gem.
• Deletegem 'sqlite3'
.
• Addgem 'pg'
. - Run
bundle install
in your command line. - Locate your
database.yml
file in yourconfig
folder. Comment out all existing code and replace it with the code below. - Make sure the Postgres.app is up and running.
- Run
rake db:setup
,rake db:migrate
,rake db:seed
andrails s
to get your app up and running. - 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 abovedevelopment:
adapter: postgresql
encoding: unicode
database: demo_test_development
pool: 5
username: <your-computer’s-user-name>
password:
timeout: 5000test:
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.
- Make a Heroku account.
- 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.
- Run
heroku create
. Your app information will display after successfully executed. - Run
git push heroku master
to push files to Heroku. Beware: this could take a while! - Run
heroku run rake db:migrate
andheroku run rake db:seed
. - 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!