breaking into the unknown…

deploying rails on heroku

1 Comment

Heroku is a cloud application platform – a new way of building and deploying web apps. It take care of all your deployment need like  managing servers, deployment, ongoing operations, and scaling. It is very easy to deploy a rails app on Heroku. Also the steps are well documented here. The document is self sufficient and do not need any other blog on it. But my client raised a jira asking me to document the complete step. Well I can’t post him the link saying it is self explanatory 🙂 .

So here is the documentation I have written.


=> create your HEROKU account

You must have an account on HEROKU. if not sign up here . Remember your credential, you will need them at time of deployment. For me it is as below

email :
password : I will not tell you 🙂

=> Install HEROKU toolbelt

It must be installed on your machine from where you are going to deploy the app on HEROKU. It can be installed with below command on ubuntu.

wget -qO- | sh

For Mac and window you need to download and execute the file available here for window and here for mac.

=> Test your HEROKU login from console

$ heroku login # It will upload your system public key to your heroku account, with below message
Enter your Heroku credentials.
Password (typing will be hidden): **************
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/

just press enter, the public key is needed to push your code later on. Here, new public key is created and uploaded to your heroku account. But in my case, you already have the public key, my terminal log look like below.

$ heroku login
Enter your Heroku credentials.
Password (typing will be hidden): **************
Authentication successful.

In this case , since I already have the public key, no new key is generated and key upload step is also skiped, so you should upload the key with the below command otherwise it give error later on while deployment

$ heroku keys:add

=> Install GIT on your system

HEROKU work only with GIT. So it must be installed on your system. on Ubuntu you can install as below. for other OS use the corresponding command.

$ sudo apt-get install git

=> STORE Your Application on git

If you are already using GIT as you source control, you can skip this step.You can add your project to git with below command.

$cd Projects/myfinance # move to your project folder
$ git init
$ git add .
$ git commit -m “initial commit”

STEP 2: Configure Your Application

You have to make only one change to your application before deploying to HEROKU i,e the gem for database in your Gemfile.
NOTE : HEROKU support only pg database, so database in production must be set to pg in the gemfile. By default the Gemfile have below line for database. set by default to sqlit3

gem ‘sqlit3’

You can change above line to gem ‘pg’ or maintain different setting for different environment as below

gem ‘sqlit3’, :group => :development # sqlit3 will be used only in development
gem ‘mysql’, :groups => [:demo, :staging] # mysql will be used in demo and staging
gem ‘pg’, :group => :production # pg gem will be used in production

run bundle install on the terminal
$ bundle install

It will modify the Gemfile and Gemfile.lock,as you can see in git status

$ git status
# On branch master
#    modified:   Gemfile
#    modified:   Gemfile.lock

commit the modified file
$ git commit – a -m”pg gem added for production”

STEP 3: Deploying the Application to HEROKU

=> creating git remote branch on HEROKU

In general case, we have our own remote git repository to which all developer pull and push the code. our project is still not associated with any remote branch as you can see with below comand
$ git remote
origin # only origin i,e your local branch is acting as remote branch also

Add the remote git branch on HEROKU with the below command
$ heroku create
Creating powerful-coast-4672… done, stack is cedar |
Git remote heroku added

You can see that, a remote git branch is added for you on HEROKU. confirm it with below command.
$ git remote
heroku # you have heroku added as your remote git branch

=> Push the code to heroku

Note : If you get Permission denied or other error see this post for solution

$ git push heroku master

Counting objects: 65, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (51/51), done.
Writing objects: 100% (65/65), 26.21 KiB, done.
Total 65 (delta 3), reused 0 (delta 0)

—–> Ruby/Rails app detected
—–> Installing dependencies using Bundler version 1.3.2
Running: bundle install –without development:test –path vendor/bundle –binstubs vendor/bundle/bin –deployment
Fetching gem metadata from………
Fetching gem metadata from
Installing rake (10.0.4)
Your bundle is complete! It was installed into ./vendor/bundle
Post-install message from rdoc:
Depending on your version of ruby, you may need to install ruby rdoc/ri data:
<= 1.8.6 : unsupported
= 1.8.7 : gem install rdoc-data; rdoc-data –install
= 1.9.1 : gem install rdoc-data; rdoc-data –install
>= 1.9.2 : nothing to do! Yay!
Cleaning up the bundler cache.
—–> Writing config/database.yml to read from DATABASE_URL
—–> Preparing app for Rails asset pipeline
Running: rake assets:precompile
Asset precompilation completed (16.98s)
—–> Rails plugin injection
Injecting rails_log_stdout
Injecting rails3_serve_static_assets
—–> Discovering process types
Procfile declares types      -> (none)
Default types for Ruby/Rails -> console, rake, web, worker

—–> Compiled slug size: 9.4MB
—–> Launching… done, v6 deployed to Heroku

* [new branch]      master -> master

STEP 4: Starting Your Application

Once you have pushed your code to heroku, you can see in the log above that, it is performing all the regular task like, running the bundle install,

clearing the cache, precompiling the assets etc. Though, you not need to run these command manually, if you want you can do it as below.

1=>heroku run bundle install –deployment # will install all the gems in vendor/bundle folder
2=>heroku run rake db:migrate #  will run all pending migration if any
3=>heroku run bundle exec rake assets:precompile # will precomplie all your assets
4=>heroku restart # will restart the heroku dyno

Recently while deploying my new app on heroku above commands , I found that migration not running on Heroku.

getting below error

heroku[api]: Starting process with command `bundle exec rake db:migrate` by
heroku[run.7050]: Error R13 (Attach error) -> Failed to attach to process
heroku[run.7050]: State changed from up to complete
heroku[run.7050]: Process exited with status 128

It is solved by replacing the command as below i,e running in background following this link.

heroku run:detached rake db:migrate

heroku run:detached rake db:seed


you can run all the command, you use generally use with respect to rake or bundle or anyother rails command by prefixing it with heroku run

We have deployed our code to Heroku in the last step. We can now instruct Heroku to execute a process type. Heroku does this by running the associated command in a dyno – a lightweight container which is the basic unit of composition on Heroku.verify that the dyno running with the below command

$ heroku ps:scale web=1
Scaling web processes… done, now running 1 # so one dyno is running

You can check the state of the app’s dynos. The heroku ps command lists the running dynos of your application:

$ heroku ps
=== web: `bundle exec rails server -p $PORT`
web.1: up 2013/04/18 15:48:12 (~ 44s ago)

So, everything is fine, lets start or open our application with below command

$ heroku open
Opening secret-everglades-4564… done

It will open up your application in the browser with some random url. For me it is

If you go to your HEROKU account, you can see all your deployed application on the dashboard, click any of them and other settings you will find all the details like git url, application url etc.

Till now we have not associated any domain name to our application, so it will be accessible with the url :

We will map the above url to proper url say in the next step

STEP 5: Assiging proper url to Application
Let say we have purchased, a domain name for our application. We want that our application should be accessible at URL rather then

coming soon…..


Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

One thought on “deploying rails on heroku

  1. It looks helpful. But you would have gone with Capistrano deployment with Heroku.. And did you posted how the domain URL configuration has to be done. Even if you post something about heroku free instance bandwidth and where we can register domain quite cheap it will be very helpful..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s