A beginner guide to deploy Rails application to Heroku

A beginner guide to deploy Rails application to Heroku

01 Jan 2018
A beginner guide to deploy Rails application to Heroku

Many beginners want to deploy their simple application in the production environment, but they do not get any free cloud service. So what they can do? Well, there is a solution provided by Heroku. Heroku is a cloud platform as a service supporting several programming languages that are used as a web application deployment model.

Before starting deployment, we need four things Git version control, SSH key, Bitbucket remote repository, and Heroku account. If you don't have any one of these, don't worry.

So, let’s get started!


Step 1. Install Git version control system.

  • To install git on your system, go ahead and download the latest version of Git, follow the instructions to install Git.
  • To verify the installation was successful, open the terminal or command prompt and type the git --version. It will look like this.
$ git –version

git version 2.9.2

  • If you have installed Git the first time you need to create a username and email using the below commands.
             $ git config --global user.name "Foo Bar"             
             $ git config --global user.emailfoobar@example.com

Congratulation! you have Git on your system. The next step is to create a Ruby on Rails application.


Step 2. Create Ruby on Rails application.

  • If you are new to Ruby on Rails, visit Rails Guides and create a simple blog application.
  • In the root directory of a project run $ git init to initialize the new repository. Add all the project files to the repository using the command $ git add -A.
  • To tell Git we want to keep the changes, we use the commit command.
             $ git commit –m “Initialize repository”


Step 3. Setup Bit-bucket remote repository.

You also can use GitHub remote repository, but there is a problem, GitHub does not provide free private repositories while Bit-bucket does.

  • Sign up for a Bitbucket account if you don’t already have one.
  • We need an SSH key to use Bitbucket, so how to get and install SSH key in Bitbucket account.
  • Add your public key to Bit-bucket by clicking on the avatar image. Select “Bitbucket settings” and then “SSH Keys”.
  • Once you’ve added your public key, click on the ‘+’ sign to create a new repository, make sure “This is a private repository” is checked, and select "Include a README" to No.
  • Now we are going to push our project to the Bit - bucket. The below commands first tell Git that you want to add Bit-bucket as the origin for your repository, and then push your repository up to the remote origin. To find the remote origin URL has gone to bit-bucket -> open repository created in previous steps -> scroll down and choose "I have an existing project".
            $ git remote add origin git@bitbucket.org:/
            $ git push -u origin –all


Step 4. Deploy the application to Heroku.

Heroku uses PostgreSQL as a production database, which means that we need to add the 'pg gem' in the production environment to allow Rails to talk to PostgreSQL. Open Gem file and below the group.

group :production do
gem 'pg', '0.20.0'

To prepare the project for deployment, we run bundle install with a special flag to prevent the local installation of any production gems.

$ bundle install --without production

Next, we have to create and configure a new Heroku account.

  • The first step is to sign up for Heroku. Then check to see if your system already has the Heroku command-line client installed. If you don't have Heroku installed. Install it using Heroku Toolbelt.
            $ heroku version
  • Once you’ve verified that the Heroku command-line interface is installed, use the Heroku command to log in and add your SSH key.
            $ heroku login
            $ herokukeys:add
  • Finally, use the Heroku create command to create a place on the Heroku servers for the app to live.
             $ heroku create
  • To deploy the application using Git to push the master branch up to Heroku.

To see your newly deployed application, visit the address that you saw when you run Heroku create.

Leave a comment: