Case study: Check out Rarebird’s brand-new Hydrogen store →

Deploy Solidus to Heroku like a boss

Daniele Palombo

10 Mar 2017Development, Solidus, Dev Ops, Ruby On Rails

Daniele Palombo

3 mins read
Deploy Solidus to Heroku like a boss

In this tutorial we'll show you how to deploy a Solidus eCommerce application to Heroku, the most famous cloud platform.

Here are our requirements:

Install and configure your eCommerce locally

First of all, like you did a thousand times, create your new Rails application:

rails new my_ecommerce --database=postgresql
cd my_ecommerce

Initialize your project and make your first commit in Git:

git init
git add .
git commit -m 'Initial project'

In order to turn your simple application into an eCommerce, you now need to install the Solidus gem. Simply add the following lines to your Gemfile:

gem 'solidus', '~> 2.1.0'
gem 'solidus_auth_devise'
gem 'aws-sdk', '< 2.0'

Note that we are also specifying the aws-sdk gem; this is needed if we want to use S3. Edit your config/database.yml and add the database configuration:

default: &default
  adapter: postgresql
  pool: 5

  <<: *default
  database: my_ecommerce_dev

  <<: *default
  database: my_ecommerce_test

Install and run migrations on Solidus:

bundle exec rake db:create
bundle exec rails g spree:install
bundle exec rails g solidus:auth:install
bundle exec rake railties:install:migrations
bundle exec rake db:migrate

Run bundle exec rails s then go to http://localhost:3000 and make sure you correctly visualize the homepage.

Configure Heroku

Ok, let's start running some commands on Heroku. Make sure you are already logged in Heroku with the heroku login command. Then you can create your new Heroku application with the following:

heroku apps:create my_ecommerce

This command creates a new application called my_ecommerce on Heroku. A new remote repository named heroku (pointing to the new application) will also be added to your git config. You can check this with:

git remote -v

Configure the production environment

Add the heroku-postgresql add-on to your Heroku application:

heroku addons:create heroku-postgresql

After adding the add-on you can see the new configuration environment with the command:

heroku config

The above command should return something like this:

=== my_ecommerce Config Vars
DATABASE_URL: postgres://

The DATABASE_URL variable stores the location of the database. You can now add the following lines to your config/database.yml under the production environment settings:

  <<: *default
  url: <%= ENV['DATABASE_URL'] %>

Saving files on S3

Heroku uses an Ephemeral filesystem, which means that it doesn't provide any sort of persistent storage for data. Your images would therefore be removed after each deploy. In order to bypass this limitation you'll need to use an external service (like Amazon S3) to store your images. Run the following commands to prepare the Heroku environment:

heroku config:add S3_BUCKET_NAME='YOUR_BUCKET_NAME'

Add this configuration in config/initializers/spree.rb:

if Rails.env.production?
  attachment_config = {
    s3_credentials: {
      access_key_id:     ENV['AWS_ACCESS_KEY_ID'],
      secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
      bucket:            ENV['S3_BUCKET_NAME']

    storage:        :s3,
    s3_headers:     { 'Cache-Control' => 'max-age=31557600' },
    s3_protocol:    'https',
    bucket:         ENV['S3_BUCKET_NAME'],
    url:            ':s3_domain_url',

    styles: {
      mini:     '48x48>',
      small:    '100x100>',
      product:  '240x240>',
      large:    '600x600>'

    path:           '/:class/:id/:style/:basename.:extension',
    default_url:    'noimage/:style.png',
    default_style:  'product'

  attachment_config.each do |key, value|
    Spree::Image.attachment_definitions[:attachment][key.to_sym] = value

Deploy Solidus to Heroku

Now commit and push your code to the Heroku repository:

git add .
git commit -m 'Configure Heroku Environment'
git push heroku master

If the push command runs without errors you'll see:

remote: Verifying deploy... done.
 * [new branch]      master -> master

Populate your remote database

Last but not least, you can now put your data in the database. Heroku CLI helps you with the command heroku run, that runs rake tasks on the deployed application.

Migrate the database and run the seed data:

heroku run rake db:migrate
heroku run rake db:seed

The seed process creates the default admin user and the basic configuration needed to bootstrap your eCommerce and start selling on the Internet.

If you want to add sample data you can run:

heroku run rake spree_sample:load

Now you can open your browser and enjoy your Solidus eCommerce by visiting or using the following Heroku CLI command:

heroku open

You can find the code used for this tutorial on this repo.

Related posts