Deploy your first Rails 4.0 app on Heroku using Vagrant

Share this post!
Ruby_on_Rails.svg_smallVagrant_smallheroku_small

Introduction

This post describes how to develop a simple Rails 4.0 application using Vagrant, and how to deploy it to Heroku.

In a previous post I have already described how to configure a development environment for Rails using Vagrant. Please check the post if you have problems installing the development environment.

The application used on this example is just a test one. In fact, I’m using a-kind-of “15 minutes test blog” application you can find in many places on the web; the app does not really matter as you can use this one or another from your own. Because the aim of this post is to show the procedure, the app to be used is up to you.

Choose your project

As I explained on the post mentioned above, your project’s home folder is ‘magically’ shared with Vagrant’s virtual machine. The project’s home folder is mounted on ‘/vagrant’ on the virtual machine. At this point you have two choices:

  1. you can create a folder on your workspace named for instance ‘vagrant_debian’ where you will create the virtual machine, and then you can create one or more Rails apps inside this folder. Pros: you only need one virtual machine for all your projects running on this vagrant host. Cons: the Vagrantfile is not on the projects’ home folder (is on the upper level) so you do not keep the file on version control. You can always copy it to every project’s home folder, but then you have two copies, the good one and the copy. In this case be careful with the modifications.
  2. you can create a Rails app, and then inside the app you create the Vagrant’s virtual machine. Pros: the Vagrantfile is treated as another file on version control. Cons: you would need a virtual machine for every Rails app.

For the purpose of this post I will use the second option. Start a terminal and execute:

$ mkdir test_blog // create the folder
$ cd test_blog
$ vagrant init // create the vagrant virtual machine; do not forget to change Vagrantfile
$ vagrant up
$ vagrant ssh // ssh the virtual machine
$ cd /vagrant // now you are on the right place

You haven’t generated a Rails 4.0 app yet. Now you can either generate your own test app or you can use mine. If you choose the first option you probably know the steps already: “rails new test_blog”, etc… If you choose the second option, just fire up these commands on your terminal:

$ clone https://github.com/memaker/test_blog.git .
$ bundle install
$ rake db:migrate
$ rails s

Your app should be now up and running on the standard URL http://localhost:3000. Start a browser and try it.

Heroku setup

Before going up any further, remember that Heroku needs the code to be under version control using Git. Easy, these are the steps to add your code to your Git repository:

$ cd test_blog // just in case your are somewhere else, please be on the right place
$ git init
$ git add .
$ git commit -m 'Initial commit'

Let’s change the Gemfile to comply with Heroku. Heroku does not support SQLite, so I will change the Gemfile to use Postgres instead; take this into account if your app uses SQLite as mine does. Edit your Gemfile and substitute this line:

gem 'sqlite3'

by this code:

group :development, :test do
  gem 'sqlite3'
end

group :production do
  gem 'pg'
end

I will add the gem ‘rails_12factor’ needed later for the deployment phase. Add these extra lines to the Gemfile:

# needed for the deployment to Heroku
gem 'rails_12factor'

Update your Rails configuration

The Gemfile is done. Besides this, you should update the ‘production’ section of your ‘config/database.yml’ file to reflect the changes on the database:

production:
  adapter: postgresql
  encoding: unicode
  # For details on connection pooling, see rails configuration guide
  # http://guides.rubyonrails.org/configuring.html#database-pooling
  pool: 5

The last modification on the configuration files is on the ‘config/routes.rb’ file. Find this part of the code and updated it to make ‘posts’ your site’s root URL:

# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'posts#index'
Heroku needs the 'Heroku Toolbelt' to be installed on the Vagrant virtual machine. This is easy running these commands:
$ vagrant ssh // just in case... you need to be on the Vagrant host
$ wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

Deploy time

Now it is time to deploy the application to Heroku. You will need your Heroku user and pass, and the Postgres add-on. This post explain how to provision the Postgres add-on on Heroku. Once revised, go to the shell and execute:

$ cd test_blog // just in case, be on the right place
$ heroku create // this will ask you for the email and password on Heroku
$ git config --list | grep heroku // verify your app
$ git push heroku master // upload the application
$ heroku config | grep HEROKU_POSTGRESQL // check your configuration
$ heroku pg:promote HEROKU_POSTGRESQL_IVORY_URL // the HEROKU... variable is the result of the previous command
$ heroku run rake db:setup // create the remote db

Your app should be running now on Heroku using Postgres. Open a browser and try the URL showed on the ‘git push heroku master’ command. As an example, mine is https://dry-sea-8357.herokuapp.com/

Conclusion

I have been checking up carefully all the steps, but something might be wrong when you try to follow them. In this case I would recommend first to check this post explaining how to deploy a Rails 4.x application. This will surely put soem more light on your problem.

In any case, you can always write comments on this post -see below- in case of doubt. I am not an expert but I will do my best to help you. Enjoy!

, ,