How To Deploy Ruby on Rails Application on Heroku

Total Blog Views: 72

Blog Status: publish

Created By: swaz_ahmed Created at: 06-24-2024

Tags: heroku deployment heroku-cli production rubyonrails apache2 sqlite3 mvc

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.

 

sudo snap install --classic heroku 

       

       3. The Heroku command is now available in your system after installation. Use your Heroku account credentials to log in.

 

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 or push the code later. 

 

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

 

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.

 

gem 'sqlite3' 

to this:

 

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. 

 

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:

 

# 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):

 

bundle install

 

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

 

production:      
   <<: *default      
   database: app_production

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

 

Run the scaffold command to create the Post:

 

rails g scaffold posts title:string content:text 

 

Create and Migrate the database:

 

rails db:create
rails db:migrate

 

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

 

root "posts#index"

rails s

Push your code changes to git:

 

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

 

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.

 

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:

 

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: 

 

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

heroku create

 

Push your code to Heroku on the master branch:

 

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:

 

heroku run rails db:migrate

 

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

 

heroku apps:info

 

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:

 

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:

 

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!


swaz_ahmed

I am swaz_ahmed blogger on shadbox. I am influencer,content writer,author and publisher. Feel free to ask me any question and suggestions.



Comments



Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads