Deploy Rails to Heroku

Codecademy Team
Guide for putting your Rails app online with Heroku.

Background

After developing a Rails app locally, the next step is to put it online. After developing a Rails app and previewing it locally on your computer, the next step is to put it online so others can see it. This is called deploying your app.

Heroku is a popular hosting service that is free to start using. Here’s how to deploy your Rails app to Heroku.

Instructions

  1. Create a new Heroku account.

  2. Install the Heroku Toolbelt on your computer.

  3. In the terminal, log in using the email address and password you used when creating your Heroku account:

    $ heroku login
  4. In Gemfile, add the pg gem to your Rails project. Change:

    gem sqlite

    to

    gem 'sqlite3', group: :development
    gem 'pg', '0.18.1', group: :production
  5. In Gemfile, add the rails_12factor gem::

    gem 'rails_12factor', group: :production
  6. In the terminal, install the gems specified in the Gemfile:

    $ bundle install
  7. Ensure config/database.yml is using the postgresql adapter. Change:

    production:
      <<: *default
      database: db/production.sqlite3
    

    to

    production:
      <<: *default
      adapter: postgresql
      database: db/production.sqlite3
    
  8. Commit your changes to git:

    $ git add .
    $ git commit -m "Heroku config"
  9. In the terminal, create an app on Heroku:

    $ heroku create
  10. Push your code to Heroku on the main branch:

    $ git push heroku main

Note: If you’re unable to push your code, you may have skipped a step or have a differently named branch. 11. If you are using the database in your application, migrate the database by running: bash $ heroku run rake db:migrate 12. If you need to seed your database with data, run: bash $ heroku run rake db:seed 13. Get the URL of your app and visit it in the browser: bash $ heroku apps:info In the output, copy the address in the Web URL field. Open a new tab in your browser, and visit your app.

Check out Heroku’s Rails docs for more information.