Heroku is a cloud-based platform that allows you to deploy and manage your applications. It supports multiple programming languages including Ruby on Rails. In this tutorial, you will learn how to deploy a Ruby on Rails application to Heroku.
Prerequisites
Before you begin, make sure you have the following:
- Ruby installed on your local machine.
- Rails installed on your local machine.
- Git installed on your local machine.
- A Heroku account. If you don’t have one, you can sign up for free at https://www.heroku.com.
Step 1: Set up your Rails application
- Open your terminal and navigate to the directory where you want to create your Rails application.
$ cd /path/to/your/directory
- Create a new Rails application using the following command:
$ rails new myapp
Replace myapp
with the desired name for your application.
- Change into the application’s directory:
$ cd myapp
Step 2: Set up Git
- Initialize a new Git repository by running the command:
$ git init
- Add all the files in your application to the repository:
$ git add .
- Commit the changes:
$ git commit -m "Initial commit"
Step 3: Create a Heroku application
- Log in to your Heroku account from the terminal:
$ heroku login
- Create a new Heroku application:
$ heroku create
This will create a new Heroku application and add a new remote repository to your Git repository.
Step 4: Configure your Rails application for Heroku
- Open the
Gemfile
in your Rails application and add the following line:
gem 'pg', '>= 0.18', '< 2.0'
This is the PostgreSQL database adapter that Heroku requires.
- Save the
Gemfile
and run thebundle install
command:
$ bundle install
Step 5: Set up the database
- By default, Rails uses a SQLite database. However, Heroku requires a PostgreSQL database. You need to update the database configuration to use PostgreSQL.
Open the
config/database.yml
file and update thedefault
section as follows:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
host: <%= ENV['DATABASE_HOST'] %>
port: <%= ENV['DATABASE_PORT'] %>
- Run the following command to create the development and test databases:
$ rails db:create
Step 6: Deploy your application to Heroku
- Deploy your application to Heroku by pushing your code to the remote repository:
$ git push heroku master
This command will push your application to the Heroku remote repository and trigger a build and deployment.
- After the deployment is successful, you can open your application in the browser by running the command:
$ heroku open
Step 7: Set up environment variables
- Some Rails applications require environment variables to be set for configuration. You can set environment variables in Heroku using the
heroku config:set
command.For example, if your application requires a
SECRET_KEY_BASE
environment variable, you can set it as follows:
$ heroku config:set SECRET_KEY_BASE=your-secret-key
Replace your-secret-key
with the actual secret key for your application.
Step 8: Scale your application
By default, Heroku assigns a single web dyno to your application. Dynos are isolated, virtualized Linux containers that run your application. You can scale your application to have more dynos using the Heroku dynos
command.
For example, to scale your application to have 2 web dynos, run the following command:
$ heroku ps:scale web=2
This will add an additional dyno to your application.
Step 9: View application logs
Heroku provides a logging facility that allows you to view the logs generated by your application. You can view the logs by running the following command:
$ heroku logs --tail
This will show the logs in real-time.
Step 10: Update your application
If you make changes to your application code and want to deploy the updates to Heroku, follow these steps:
- Commit your changes to Git:
$ git add .
$ git commit -m "Update application"
- Push the changes to Heroku:
$ git push heroku master
Your changes will be built and deployed to Heroku.
Conclusion
Congratulations! You have successfully deployed your Ruby on Rails application to Heroku. You learned how to set up your Rails application, configure Git, create a Heroku application, configure your Rails application for Heroku, and deploy your application. You also learned how to set up environment variables, scale your application, and view application logs. Now you can share your application with the world!