Rails App Deployment On Heroku

group :production do
gem ‘pg’, ‘0.15.1’
gem ‘rails_12factor’, ‘0.0.2’

Note also the addition of the rails_12factor gem, which is used by Heroku to serve static assets such as images and stylesheets.

As mentioned in Section 1.2.4, it’s also a good idea to specify explictly which version of Ruby our applications expects:

ruby ‘2.0.0’
(Here I’ve also added the optional RVM gemset line for convenience. You should substitute ’1.9.3’ if that’s the version of Ruby you’re using, though for this tutorial the difference shouldn’t ever matter.) Applying these changes to the Gemfile from Listing 1.5 yields Listing 1.9.

Listing 1.9. A Gemfile with added gems and explicit Ruby version.
source ‘https://rubygems.org’
ruby ‘2.0.0’

gem ‘rails’, ‘4.0.4’

group :development do
gem ‘sqlite3’, ‘1.3.8’

gem ‘sass-rails’, ‘4.0.1’
gem ‘uglifier’, ‘2.1.1’
gem ‘coffee-rails’, ‘4.0.1’
gem ‘jquery-rails’, ‘3.0.4’
gem ‘turbolinks’, ‘1.1.1’
gem ‘jbuilder’, ‘1.0.2’

group :doc do
gem ‘sdoc’, ‘0.3.20’, require: false

group :production do
gem ‘pg’, ‘0.15.1’
gem ‘rails_12factor’, ‘0.0.2’
To install it, we run bundle install with a special flag:

$ bundle install –without production
The –without production option prevents the local installation of any production gems, which in this case consists of pg and rails_12factor. (If Bundler complains about a readline error, try adding gem ‘rb-read\-line’, ‘~> 0.4.2’ to your Gemfile.) Because the only gems we’ve added are restricted to a production environment, right now this command doesn’t actually install any additional local gems, but it’s needed to update Gemfile.lock with the pg and rails_12factor gems and the specific Ruby version. We can commit the resulting change as follows:

$ git commit -a -m “Update Gemfile.lock for Heroku”
(Some readers have reported that they need one last bit of configuration at this point, namely, creating the files Heroku needs to serve static assets like images and CSS:

# This should only be used if your Heroku deploy fails without it.
$ rake assets:precompile
$ git add .
$ git commit -m “Add precompiled assets for Heroku”
(This uses the rake command, which we’ll cover in more detail in Section 2.2.) The asset precompile step shouldn’t be necessary, and I have been unable to reproduce the issue, but the reports are common enough that I include it here for reference.)

Next we have to create and configure a new Heroku account. The first step is to sign up for Heroku; after checking your email to complete the creation of your account, install the necessary Heroku software using the Heroku Toolbelt.22 Then use the heroku command to log in at the command line (you may have to exit and restart your terminal program first):

$ heroku login
Finally, navigate back to your Rails project directory and use the heroku command to create a place on the Heroku servers for the sample app to live (Listing 1.10).

Listing 1.10. Creating a new application at Heroku.
$ cd ~/rails_projects/first_app
$ heroku create
Created http://stormy-cloud-5881.herokuapp.com/ |
Git remote heroku added
The heroku command creates a new subdomain just for our application, available for immediate viewing. There’s nothing there yet, though, so let’s get busy deploying.

1.4.2 Heroku deployment, step one

To deploy the application, the first step is to use Git to push it up to Heroku:

$ git push heroku master
1.4.3 Heroku deployment, step two

There is no step two! We’re already done (Figure 1.10). To see your newly deployed application, you can visit the address that you saw when you ran heroku create (i.e., Listing 1.10, but with the address for your app, not the address for mine). You can also use an argument to the heroku command that automatically opens your browser with the right address:

$ heroku open
Unfortunately, the resulting page is an error; as of Rails 4.0, for technical reasons the default Rails page doesn’t work on Heroku. The good news is that the error will go away (in the context of the full sample application) when we add a root route in Section 5.3.2.

bundle exec rake railties:install:migrations
bundle exec rake db:migrate
bundle exec rake spree_sample:load


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: