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.
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.
1. Create a new Heroku account.
2. Install the Heroku CLI on your machine.
3. After installation, the heroku command is now available in your system. 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 for pushing 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
Fire the following commands to create a rails application.
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.
To this:
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.
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):
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.
Create and Migrate the database.
Change the main page route in routes.rb and start the server
Push your code changes to git
You can also clone the code. Here’s the source code of the repository: https://github.com/ishag-bac/Deploy
As we want to deploy Ruby on Rails application on Heroku, we will need the following.
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!
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:
The same version of Ruby should be running locally as well. You can check the ruby version by running $ ruby -v.
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.
Push your code to Heroku on the master branch.
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.
To seed your database with data, run.
Get the URL of your application and visit in the browser.
Download our White Papers and e-Books for FREE!
DownloadThe 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:
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.
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:
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.
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.
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!
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.
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.