Avijit Karmaker
4 min readMar 23, 2021

--

How to deploy a rails app in Heroku- Ubuntu (beginners Friendly)

Deploy a ruby on rails hassle-free

When I first started my rails journey, I was afraid to experiment on almost anything. There were so many files in the framework and so many things to worry about. Dealing with databases was very intimidating and confusing. Creating database, migration files, migrating database, updating database were all very confusing initially. Then after learning those things for a while It was time for me to deploy my apps on a platform. It turned out to be a nightmare for me. I couldn’t figure out how to link PostgreSQL with the Heroku platform. When I was deploying my app initially it was showing all kinds of errors. At one point I just wanted to give up because of all the complications and hundreds of errors I was getting that I couldn’t understand. Eventually, I did figure it out but it took me a lot of time. For a beginner, experiencing that kind of frustration just to deploy an app could be soul-crushing. After the experience I had, I got motivated to write a clear instruction manual that may be helpful to the rails beginners. By following this process an aspiring rails developer can deploy his/her rails app without any anxiety and can focus on producing better code. Let's have a look at the procedure step by step.

First step:

Users have to create a free account in Heroku to use it. Heroku allows users to host up to 5 apps free of cost per account. User can create multiple accounts with a different email and can use this feature as long as he or she wants. Visit this link and create a free account:

https://signup.heroku.com/

Next Step:

This article only focuses on deploying the rails app from ubuntu. We will try to cover the instructions for windows in a future article. To use Heroku in ubuntu it is important to install the Heroku CLI globally from the terminal.

sudo snap install --classic heroku

This command will install Heroku globally and a user will be able to use Heroku in any app within the computer.

Next Step:

Heroku doesn’t allow users to deploy an application running on SQLite3. The user needs to configure his app with PostgreSQL.

In production the de-facto standard for a database on Heroku is PostgreSQL. There are several ways to use PostgreSQL in a rails app. Before following any one of the suggested methods it is required to install PostgreSQL globally. The following command will install PostgreSQL:

Open terminal. Then run:

sudo apt update

It will update the system and will make it ready for PostgreSQL. Then run:

sudo apt install postgresql postgresql-contrib

it will globally install PostgreSQL and users will be able to configure PostgreSQL while creating the rails app. Now it is time to create the rails app with PostgreSQL.

rails new [application name] -d postgresql

Replace the application name with the app name. This will generate a rails application that is configured with PostgreSQL database which is a default requirement for Heroku.

Next Step:

Now it is time to configure the database. Although it is not a mandatory thing, it is a good practice to use a custom role other than the default role that PostgreSQL comes with. To do that, some changes are necessary for the database.yml file.

default: &default  
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: postgres role name
password: postgres password

development: <<: *default
database: name of the desired database.

Now it is time to create the database locally. To create the database according to modified configuration user can run the following commands:

rails db:create rails db:migrate 

These commands will create the database locally. Now everything is ready for Heroku deployment.

Last Step:

So far we have installed Heroku CLI, PostgreSQL and created a rails application with a customized database. It is time to deploy the app in Heroku. Before deployment, it is important to create a GitHub repository and push the app into the main branch. It is extremely important to remember that the app can be pushed to Heroku only from the main branch. If the user doesn't want to keep the app in the main branch of the repository then the user can create the repository first. Clone the empty repository and the create the app in the local main branch.

After following the given instructions user now have to commit the changes in Github. To do that user needs to run the following commands:

git add . 
git commit -m "your commit message"
git push main (if user wants to keep the app in remote repository's main branch)

To publish the app user will need to create a Heroku app. To do that user needs to log in first with his Heroku credentials. From the terminal user needs to run :

heroku login 

It will take the user to the login page of Heroku. If the login is successful, the user will see a message in his/her terminal. After that user will be eligible to create the app.

heroku create

will create a Heroku app. Then the following commands will push the app files to the Heroku app.

git add . 
git commit -m "your commit message"
git push heroku main

To make the database accessible in Heroku user needs to run:

heroku run rake db:migrate

It will deploy the app with the database in Heroku. To check the application user can write:

heroku open

It will take the user to the page of the application.

Final Words:

Learning and managing rails can be extremely stressful for beginners. So many configurations to manage and learn. No one wants to deal with unnecessary deployment problems. I believe by following the above instructions a beginner will be able to successfully deploy his/her desired app every time. I hope this article will help aspiring developers to progress and will give them a boost of confidence.

--

--