If you’ve developed a Ruby on Rails application and are looking for a reliable and straightforward way to host it, Heroku is an excellent choice. Heroku is a cloud platform that simplifies deployment, scaling, and management of web applications. In this guide, we’ll walk you through the process of deploying a Ruby on Rails application to Heroku, from start to finish.
Prerequisites
Before you begin, make sure you have the following:
- A Ruby on Rails Application: You should have a fully functional Ruby on Rails application ready for deployment.
- Heroku Account: Sign up for a Heroku account if you don’t already have one.
- Heroku CLI: Install the Heroku Command Line Interface (CLI) on your local machine. You can download it from here.
- Git: Ensure that Git is installed on your local machine. You’ll need it for version control.
- Database: Your Rails application should be using a supported database like PostgreSQL, as Heroku defaults to PostgreSQL.
- Gemfile: Make sure your Rails application’s
Gemfile
specifies the correct Ruby and Rails versions.
Step 1: Login to Heroku
Open your terminal and log in to Heroku by running:
heroku login
This command will open a web page where you can enter your Heroku credentials.
Step 2: Prepare Your Application
Before deploying to Heroku, there are a few changes you need to make to your Rails application.
2.1. Add a Procfile
Create a Procfile
in the root of your application with the following content:
web: bundle exec rails server -p $PORT
This file tells Heroku how to run your application.
2.2. Configure Your Database
Since Heroku defaults to PostgreSQL, you should ensure your config/database.yml
is properly configured to use the DATABASE_URL
environment variable provided by Heroku.
2.3. Update the Gemfile
Make sure your Gemfile
includes the pg
gem for PostgreSQL. You can add it under the production
group:
group :production do
gem 'pg'
end
2.4. Set Secret Key Base
Ensure that your config/secrets.yml
or config/credentials.yml.enc
has a properly set SECRET_KEY_BASE
. You can generate a new key using:
rails secret
Step 3: Initialize Git Repository
If your application is not already in a Git repository, initialize one:
git init
git add .
git commit -m "Initial commit"
Step 4: Create a Heroku App
To create a new Heroku app, run:
heroku create your-app-name
Heroku will generate a random name if you don’t specify one.
Step 5: Deploy Your Application
Push your code to Heroku:
git push heroku master
This will deploy your application to Heroku and automatically run the necessary build tasks.
Step 6: Migrate the Database
Run the database migrations on Heroku:
heroku run rake db:migrate
This command will apply your database schema to Heroku’s PostgreSQL database.
Step 7: Open Your Application
Your application is now deployed on Heroku. To open it in your browser, run:
heroku open
Step 8: Monitor and Scale
Heroku provides various tools to monitor and scale your application. You can check the logs using:
heroku logs --tail
And you can scale your application dynos depending on your traffic:
heroku ps:scale web=2
Conclusion
Deploying a Ruby on Rails application to Heroku is a relatively straightforward process thanks to Heroku’s platform-as-a-service offering. This guide has walked you through the essential steps to deploy your application successfully. Heroku’s simplicity and scalability make it an excellent choice for hosting your Ruby on Rails applications.
Leave a Reply