How to: Make Carrierwave work on Heroku

Aaron Wolfson edited this page Mar 7, 2017 · 10 revisions
Clone this wiki locally

Heroku has a read-only filesystem, so uploads must be stored on S3 and cannot be cached in the public directory.

You can work around this by setting the cache_dir in your Uploader classes to the tmp directory:

class AvatarUploader < CarrierWave::Uploader::Base
  def cache_dir

You can also set the cache_dir for all uploaders in an initializer:

CarrierWave.configure do |config|
  config.cache_dir = "#{Rails.root}/tmp/uploads"

Note that these techniques will disable the feature of making uploads work across form redisplays. Review this example application for a technique that attempts to preserve this feature. In brief:

require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Static, :urls => ['/carrierwave'], :root => 'tmp' # adding this line
run YourApplicationName::Application

# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
  config.root = Rails.root.join('tmp') # adding these...
  config.cache_dir = 'carrierwave' # ...two lines

  config.fog_credentials = {
    :provider               => 'AWS',                        # required
    :aws_access_key_id      => 'key',                        # required
    :aws_secret_access_key  => 'secret',                     # required
    :region                 => 'eu-west-1',                  # optional, defaults to 'us-east-1'
    :host                   => '',             # optional, defaults to nil
    :endpoint               => '' # optional, defaults to nil
  config.fog_directory  = 'directory'                             # required
  config.fog_public     = false                                   # optional, defaults to true
  config.fog_attributes = {'Cache-Control'=>'max-age=315576000'}  # optional, defaults to {}

Note that this technique may not work if you have more than one "dyno" on Heroku that has a different "tmp" directory. See the Heroku documentation for more detail.