Overview

This article will walk you through configuring and deploying your Spree application to Heroku.

This guide assumes that your application is deploy-ready and that you have a Heroku application already created on the Heroku stack for this application. If you don’t have a Heroku app already, follow this guide.

Heroku’s tools assume that your application is version controlled by Git, as Git is used to push the code to Heroku.

Configuring your application

Specify Ruby version

You should speficy the exact ruby version you want to run in your Gemfile:

ruby '2.0.0'

Keep in mind that Spree 2.0.0 requires a version of Ruby greater than or equal to Ruby 1.9.3. See Heroku Ruby support page for details on build behaviour related to Ruby versions.

Add Heroku 12 Factor Gem

Add the Heroku 12 Factor gem to your Gemfile:

gem 'rails_12factor', group: :production

This will enable your application to serve static assets and direct logging to stdout.

Asset Pipeline

If you’re on Rails 4 or greater. There’s no longer a initialize_on_precompile config option because you should be able to run assets:precompile without a database connection. See Heroku troubleshooting page for details. Unfortunately Spree still needs to connect to db on startup so you’ll have to enable the user-env-compile feature on heroku to run your store.

When deploying to Heroku by default Rails will attempt to intialize itself before the assets are precompiled. This step will fail because the application will attempt to establish a database connection, which Heroku will not have set up yet.

To work around this issue, put this line underneath the other config.assets lines inside config/application.rb:

config.assets.initialize_on_precompile = false

The assets for your application will still be precompiled, it’s just that Rails won’t be intialized during this process.

Paperclip image quality issues

Heroku currently defaults to a surprisingly old version of ImageMagick (6.5 as of March 2014) which can cause problems. Aside from the fact that 6.5 is missing some of the newer command line arguments that Paperclip can invoke, its image conversion quality is noticeably inferior to that of the current release. You can easily work around this by using a Heroku buildpack to provide the latest ImageMagick release. You may have to :reprocess! your images after upgrading ImageMagick.

S3 Support

Because Heroku’s filesystem is readonly, you will need to configure Spree to upload the assets to an off-site server, such as S3. If you don’t have an S3 account already, you can set one up here

This guide will assume that you have an S3 account already, along with a bucket under that account for your files to go into, and that you have generated the access key and secret for your S3 account.

To configure Spree to upload images to S3, put these lines into config/initializers/spree.rb:

Spree.config do |config|
  config.use_s3 = true
  config.s3_bucket = '<bucket>'
  config.s3_access_key = "<key>"
  config.s3_secret = "<secret>"
end

If you’re using the Western Europe S3 server, you will need to set two additional options inside this block:

Spree.config do |config|
  ...
  config.attachment_url = ":s3_eu_url"
  config.s3_host_alias = "s3-eu-west-1.amazonaws.com"
end

And additionally you will need to tell paperclip how to construct the URLs for your images by placing this code outside the +config+ block inside config/initializers/spree.rb:

Paperclip.interpolates(:s3_eu_url) do |attachment, style|
"#{attachment.s3_protocol}://#{Spree::Config[:s3_host_alias]}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/},"")}"
end

Pushing to Heroku

Once you have configured the above settings, you can push your Spree application to Heroku:

$ git push heroku master

Once your application is on Heroku, you will need to set up the schema by running this command:

$ heroku run rake db:migrate

You may then wish to set up an admin user as well which can be done by loading the rails console:

$ heroku run rails console

And then running this code:

user = Spree::User.create!(:email => "[email protected]", :password => "yourpassword")
user.spree_roles.create!(:name => "admin")

Exit out of the console and then attempt to sign in to your application to verify these credentials.

SSL Support

For information about SSL support with Heroku, please read their SSL Guide.