A beginner guide to deploy Rails application to Heroku

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

Many beginners want to deploy their simple application on 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 is used as a web application deployment model.

Before starting deployment, we need four things Git version control, SSH key, Bit-bucket 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 latest version of Git, follow the instructions to install Git.
  • To verify the installation was successful, open terminal or command prompt and type git --version. It will look like this.
$ git –version

git version 2.9.2

  • If you have installed Git first time you need to create username and email using 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 Ruby on Rails application.

Step 2. Create Ruby on Rails application.

  • If you are new to Ruby on Rails, visit Rails Guides and create simple blog application.
  • In root directory of project run $ git init to initialize new repository. Add all the project files to repository using 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 Bit-bucket account if you don’t already have one.
  • We need SSH key to use Bit-bucket, so how to get and install SSH key in Bit-bucket account.
  • Add your public key to Bit-bucket by clicking on the avatar image. Select “Bit-bucket settings” and then “SSH Keys”.
  • Once you’ve added your public key, click on ‘+’ sign to create a new repository, make sure “This is a private repository” 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 application to Heroku.

Heroku uses the PostgreSQL as 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 group.

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

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 heroku create command to create a place on the Heroku servers for the app to live.
             $ heroku create
  • To deploy the application use 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:

Recent Blogs
Contact-Us Contact-Us
Newsletter Newsletter