Deploy Rails to 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
Create a new Heroku account.
Install the Heroku Toolbelt on your computer.
In the terminal, log in using the email address and password you used when creating your Heroku account:
$ heroku loginIn Gemfile, add the
pg
gem to your Rails project. Change:gem sqliteto
gem 'sqlite3', group: :developmentgem 'pg', '0.18.1', group: :productionIn Gemfile, add the
rails_12factor
gem::gem 'rails_12factor', group: :productionIn the terminal, install the gems specified in the Gemfile:
$ bundle installEnsure config/database.yml is using the
postgresql
adapter. Change:production: <<: *default database: db/production.sqlite3
to
production: <<: *default adapter: postgresql database: db/production.sqlite3
Commit your changes to git:
$ git add .$ git commit -m "Heroku config"In the terminal, create an app on Heroku:
$ heroku createPush 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 theWeb URL
field. Open a new tab in your browser, and visit your app.
Check out Heroku’s Rails docs for more information.
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
- Article
Deploying a Flask App
Learn how to deploy your own Flask application with Heroku. - Article
Going Beyond with Heroku
With Heroku being "the fastest way to go from idea to URL", deploying an app is a fairly simple process. But there are many more features that make Heroku a solid choice when it comes to configuring, scaling, tuning, and managing apps.