Deploy A Rails App With Heroku

Written by Brad Traversy on 16 March 2013.


herokuThis is a follow up article to the What is Heroku article. I will go through the steps to install and deploy a Ruby on Rails application with Heroku. I am assuming that you already have Ruby on Rails installed on either Linux or Windows. We will go through deployment and installation using Windows as well as Linux. We will use the Ubuntu/Debian distribution in this tutorial.

Signup

The very first thing you need to do is go to heroku.com and create an account by clicking the "Sign Up" button. You will enter the email you want to use and the password which we will use later in the command line to log in.

For Windows Users - Install Heroku Toolbelt

If you use Windows, then you need to install the Heroku Toolbelt. If you have Rails installed on Windows, chances are you are using RailsInstaller. That is absolutely fine and is the recommended way of using Rails with Windows. You now need to download and install the Heroku Toolbelt program. You can get it here.

Just install it like any other software. Once it is complete, you should see a "Git Bash" icon on your desktop. This is a shell-like command line we can use. It is much like the command-line utility you have with RailsInstaller. In Windows, your apps go in the C:\Sites folder. Open that up and navigate to your "Sites" folder which was created when you installed RailsInstaller.

cd C:\Sites

Now if you already created/generated a Rails up, then go into that directory

cd myapp

If you do not have an app, then generate a new one

rails new myappname

Now cd into the directory

cd myapp

For Linux Users

You do not need the Toolbelt utility but you can use it if you want. You will not have a "Sites" directory. Instead you should be in your users home folder(/home/username). This is where your app will go.

So if you have not generated an app, open a Terminal and navigate to your users home directory. You most likely are already there when you open a terminal. To generate type

rails new myappname

Now go into the directory

cd myapp

The rest of the steps are the same for Windows and Linux

Install the Heroku Gem

What you want to do now is install the heroku gem. Edit the Gemfile in your app folder. Do this by using your GUI (Windows Explorer or Linux GUI).

If on Windows, your Rails apps will go in the C:\Sites folder. On Linux it will be in your Home/Username directory

While in the Gemfile, add the following...

gem 'heroku'

Save the Gemfile

Now go back into the command-line and enter

bundle install

This will get the Gem installed.

Login To Heroku

Now we need to login to heroku via command-line. Type the following

heroku login

It will now as for your credentials. Use the email and password that you created in the step above. You might get a message saying that it couldn't find an SSH key and it will ask to create one. Click Yes. If all goes well it will say authorization successful.

Create an app on Heroku

Now you need to create the Heroku app. Make sure you are in your apps directory.

heroku create myappname

Now we can use Git to upload to the repository. If you do not know Git don't worry, we only need to know a few commands.

Initialize the directory

git init

This tells Git that we want to use this directory

Add the files to the staging area

git add .

The dot(.) says we want to add ALL files and folders

Commit to the changes

git commit -m 'initial commit'

The -m option makes it so it doesn't take us to another page to add comments. Instead we can just add a comment in the command ('initial commit')

Push To Heroku

So our app is ready to deploy. Push the files to the Heroku repository with the following command

git push heroku master

This should now push our files up. If you see an error about public keys, try the following..

heroku ssh-keygen

This will generate a new key. Now add it with

heroku keys:add

Now try again

git push heroku master

Thats it. Now you have a deployed app. Visit yourapp.herokuapp.com to see the live site

Now the only commands you need to do to upload it is

git add .
git commit -m 'commit comment'
git push heroku master

About The Author

Brad Traversy is a web developer/designer from Boston Massachusetts. He specializes in web development using open source content management systems such as Joomla, Drupal and Wordpress.

Latest News