Table of Contents

Introduction

For any developer, the most satisfying thing is to make their development available to each individual after building it as a reference source. So, after locally previewing and developing a Rails application on your system, the following step is to put it online so that others can observe it. This is called deploying the application. Now here comes Heroku.

It allows you to deploy your Ruby on Rails application quickly and is prominent for learners because it’s an open-source and “effortless” push-to-deploy system. Concisely, Heroku handles pretty much everything for the individual. Let us check how you can deploy Ruby on Rails application on Heroku with the following steps.

Steps to Deploy Ruby on Rails Application on Heroku Cloud Platform

To publish your app on the cloud, here are the steps you need to follow. Deploying Ruby on Rails app to Heroku platform as a service is not that tricky. This guide will show you how to begin with your RoR app from local server to deploying it on Heroku.

Local Setup

1. Create a new Heroku account.
2. Install the Heroku CLI on your machine.

Copy Text
$ sudo snap install --classic heroku

3. After installation, the heroku command is now available in your system. Use your Heroku account credentials to log in.

Copy Text
admin1@admin1-Latitude-3510:~$ heroku login
heroku: Press any key to open up the browser to login or q to exit: 

4. Create a new ssh key if not available otherwise, press Enter instantly to upload the existing ssh key used for pushing the code later.

Copy Text
$ heroku login
heroku: Enter your Heroku credentials Email: [email protected]
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

Create a Rails Application

Fire the following commands to create a rails application.

Copy Text
rails new app -d postgresql
cd app
bundle a tailwindcss-rails
rails tailwindcss:install

Disclaimer:- Here we are using Ruby2.7.2 and Rails 6.1.7 running on Ubuntu 22.04.1

Working with Ruby is entertaining, but you can’t deploy the application running on SQLite3 on Heroku. PostgreSQL is the practical standard for databases on Heroku. Add the gem ‘pg’ if you’re using your current RoR application.

Copy Text
gem 'sqlite3'

To this:

Copy Text
gem 'pg'

Note:- During development PostgreSQL is the highly approved database to use. Because of differences between your development and deployment environments, an analogy is maintained which helps to prevent devious bugs from being established in the application. Install Postgres locally if it is yet to be available on your system.

In Gemfile, add rails_12factor gem if you use former Rails versions to authorize static asset serving and logging on Heroku.

Copy Text
gem 'rails_12factor', group: :production

During deploying a new application, the rails_12factor gem is not needed. But if you are upgrading an already existing application, you can remove the rails_12factor gem provided you have the proper configuration in your config/environments/production.rb file:

Copy Text
# config/environments/production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
if ENV["RAILS_LOG_TO_STDOUT"].present?
Logger                       = ActiveSupport::Logger.new(STDOUT)
   Logger.formatter  = config.log_formatter
    config.Logger = ActiveSupport::TaggedLogging.new(Logger)
end

Now reinstall your dependencies (to generate a new Gemfile.lock):

Copy Text
$ bundle install

Amend the database.yml with your data and make sure the config/database.yml is using the postgresql adapter.

Copy Text
production:      
   <<: *default      
   database: app_production

Tоproduction:
    <<: *default
   adapter: postgresql    
    database: app_production

Run the scaffold command to create the Post.

Copy Text
$ rails g scaffold posts title:string content:text

Create and Migrate the database.

Copy Text
$ rails db:create 
$ rails db:migrate

Change the main page route in routes.rb and start the server

Copy Text
root "posts#index"
rails s

Push your code changes to git

Copy Text
git init
git add .
git commit -m "Deploying Rails application

Source Code: deploying-rails-app

You can also clone the code. Here’s the source code of the repository: https://github.com/ishag-bac/Deploy

Prerequisites

As we want to deploy Ruby on Rails application on Heroku, we will need the following.

  • Basic knowledge of Ruby/Rails and Git.
  • An installed version of Ruby 2.5.0+ and Rails 6+ in your local machine.
  • A verified Heroku account.
  • Install git in your system if not already installed because Heroku will depend on it for deployment.
  • If your project is not available in Git, then commit your application changes in git.

Are you looking for proficient Ruby on Rails developers highly-skilled with development and deployment as well?
Contact Bacancy today and hire Ruby on Rails developer to start building your dream project!

Specify Ruby Version

Rails 6 requires Ruby 2.5.0 or above. By default, a recent version of Ruby is installed in Heroku. However, you can specify the exact version in your Gemfile using the ruby domain-specific languages. Depending on the current Ruby version running in the application, it might look like this:

Copy Text
ruby '2.7.2'

The same version of Ruby should be running locally as well. You can check the ruby version by running $ ruby -v.

Deploy Ruby on Rails application on Heroku

After installing Heroku CLI and logging into your Heroku account, ensure you are in the correct directory path containing your application, then follow the instructions below.

Create an application in Heroku using the below command in the terminal.

Copy Text
$ heroku create

Push your code to Heroku on the master branch.

Copy Text
$ git push heroku master

Note:- Check the default branch name before deployment. If it uses master, use git push heroku master. Otherwise ,use git push heroku main.

Migrate the database of your application by running.

Copy Text
$ heroku run rails db:migrate

To seed your database with data, run.

Copy Text
$ heroku run rails db:seed

Get the URL of your application and visit in the browser.

Copy Text
$ heroku apps:info
cta img
Resource img

In a Hurry? Fetch our Resources, Save and Read them Later!

Download our White Papers and e-Books for FREE!

Download

Visit Your Application

The deployment of the source code to Heroku is done. Now you can instruct to execute a process type to Heroku. Heroku implements this process by running the associated command in a dyno. [Dyno is the basic unit of composition, a container on the Heroku server.]

Ensure that you have a dyno running the web process type with the command:

Copy Text
$ heroku ps:scale web=1

You can check the state of the app’s dynos. All the running dynos of your application can be listed out by applying the heroku ps command.

Using heroku open, we can open the deploying application.

View Logs

If the application is not functioning correctly or you run into any problems, you must check the logs. With the help of heroku logging commands, you can get information about your application.

By running the command with the –tail flag option like this, you can also get the entire stream of logs:

Copy Text
$ heroku logs --tail

Troubleshooting

Check your logs if you push up your application, and it crashes (heroku ps shows state crashed) to find out what exactly went wrong while pushing up the application. Here are some issues.

Runtime Dependencies on Development Gems and Test Gems

Check your Bundler groups if you’re missing a gem while deploying. Heroku builds your application without the development or test groups, and if your app depends on a gem from one of these groups to run, you should move it out of the group. Therefore before deploying Ruby on Rails app to Heroku, test if it works locally, then push it to Heroku.

Conclusion

We hope you found our comprehensive guide useful and would try to deploy ruby on rails application on Heroku. In case of any queries, feel free to reach out to us. We will be glad to assist you in your technical endeavors. You can find relevant Ruby on Rails tutorials if that interests you. Do share this blog on social media and with your friends, and comment if you have any suggestions. Happy to help!

Frequently Asked Questions (FAQs)

Heroku is a platform as a service (PaaS) container cloud platform. Developers use it to deploy, scale and manage modern applications and even Ruby on Rails applications can be deployed on this platform.

Heroku offers great possibilities and ease of deployment. Developers can easily and painlessly deploy Ruby on Rails application on Heroku, hence it is a great advantage.

Are you looking for sure-shot ways to speed up Ruby on Rails app performance?

Experience instantly extend applications with fully-managed services. Get a high-quality service which empowers developers Hire hand-picked RoR developers to keep your app up-to-date and avoid the most common pitfalls.

BOOK A FREE 30 MIN CALL TO KNOW HOW

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?