How to Deploy a Ruby on Rails App to Heroku

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:

  1. Ruby installed on your local machine.
  2. Rails installed on your local machine.
  3. Git installed on your local machine.
  4. 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

  1. Open your terminal and navigate to the directory where you want to create your Rails application.
$ cd /path/to/your/directory
  1. Create a new Rails application using the following command:
$ rails new myapp

Replace myapp with the desired name for your application.

  1. Change into the application’s directory:
$ cd myapp

Step 2: Set up Git

  1. Initialize a new Git repository by running the command:
$ git init
  1. Add all the files in your application to the repository:
$ git add .
  1. Commit the changes:
$ git commit -m "Initial commit"

Step 3: Create a Heroku application

  1. Log in to your Heroku account from the terminal:
$ heroku login
  1. 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

  1. 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.

  1. Save the Gemfile and run the bundle install command:
$ bundle install

Step 5: Set up the database

  1. 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 the default 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'] %>
  1. Run the following command to create the development and test databases:
$ rails db:create

Step 6: Deploy your application to Heroku

  1. 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.

  1. 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

  1. 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:

  1. Commit your changes to Git:
$ git add .
$ git commit -m "Update application"
  1. 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!

Related Post