title: Aws-sdk gem 'cannot load such file' error on remote server but not locally
body: <p>This looks very similar to <a href="https://stackoverflow.com/questions/15078455/rails-error-cannot-load-such-file-aws-sdk-you-may-need-to-install-the-aws?rq=1">other</a> <a href="https://stackoverflow.com/questions/11236847/issue-with-paperclip-s3-in-rails-3-2-5-cannot-load-such-file-aws-sdk?rq=1">questions</a> but those solutions don't look like they apply here.</p>
  
  <p>I can run my rails app in production mode locally, but on a remote server it fails with a message about the <code>aws-sdk</code> gem:</p>
  
  <pre><code>A LoadError occurred in images#index:
  
  cannot load such file -- aws-sdk (You may need to install the aws-sdk gem)
  app/controllers/images_controller.rb:9:in `block (2 levels) in index'
  </code></pre>
  
  <p><code>aws-sdk</code> is being used by <code>Paperclip 4.2.0</code>, which as of now only integrates with aws-sdk 1.x (no version 2). <code>require 'aws-sdk'</code> is not anywhere in my own code. Offending action code:</p>
  
  <pre><code>@images = @viewable.images
  respond_to do |format|
    format.html {}
    #line 9:
    format.json {render json: @images, root: false, each_serializer: ImageSerializer}
  end
  </code></pre>
  
  <p>What I've investigated so far follows.</p>
  
  <h3>Remote vs Local configuration</h3>
  
  <p>This seems most likely to be the cause. Perhaps some crucial config is not being found by my server such that aws-sdk can't be initialised. Here's how my config works:</p>
  
  <p>S3 is configured according to <a href="https://github.com/thoughtbot/paperclip/wiki/Paperclip-with-Amazon-S3" rel="nofollow noreferrer">this page</a>. I followed the tutorial exactly except for a slight change to accommodate multiple production mode servers:</p>
  
  <p>config/application.yml (values loaded to <code>ENV</code> by <a href="https://rubygems.org/gems/figaro/versions/1.1.0" rel="nofollow noreferrer">figaro</a>):</p>
  
  <pre><code>production:
    s3_bucket: 'mybucket'
  
  development:
    s3_bucket: 'mybucket'
  </code></pre>
  
  <p>config/aws.yml (loaded automatically by aws-sdk):</p>
  
  <pre><code>development:
    access_key_id: AK_MY_ID
    secret_access_key: MYSECRET
  production:
    access_key_id: AK_MY_ID
    secret_access_key: MYSECRET
  </code></pre>
  
  <p>Excerpt from config/environments/production.rb:</p>
  
  <pre><code>config.paperclip_defaults = {
    storage: :s3,
    s3_credentials: {
      bucket: ENV['s3_bucket']
    }
  }
  </code></pre>
  
  <p>As far as I can tell symlinks for each yml file are being created correctly on each deploy:</p>
  
  <pre><code>deployer:~/apps/myapp/releases/20150505130826$ ll config/
  lrwxrwxrwx  1 deployer deployer    53 May  5 13:08 application.yml -&gt; /home/deployer/apps/myapp/shared/config/application.yml
  lrwxrwxrwx  1 deployer deployer    45 May  5 13:08 aws.yml -&gt; /home/deployer/apps/myapp/shared/config/aws.yml
  </code></pre>
  
  <p>Other things I've checked:</p>
  
  <h3>I doubt this controller is at fault</h3>
  
  <p>Other actions and controllers that access the same model also fail, and it fails on the remote server only.</p>
  
  <h3>Gem installation looks good</h3>
  
  <p>I have had no problems with other gems being lost during deployment over several months. There is no output from bundler saying that the install has failed. From capistrano deploy output:</p>
  
  <pre><code> ~/.rvm/bin/rvm ruby-2.1.5@myapp do bundle install --path /home/deployer/apps/myapp/shared/bundle --without development test --deployment --quiet as deployer@myapp.com
  DEBUG [c43674b8] Command: cd /home/deployer/apps/myapp/releases/20150505130826 &amp;&amp; ~/.rvm/bin/rvm ruby-2.1.5@myapp do bundle install --path /home/deployer/apps/myapp/shared/bundle --without development test --deployment --quiet
  INFO [c43674b8] Finished in 3.582 seconds with exit status 0 (successful).
  </code></pre>
  
  <p>From a shell I can inspect where the gem is installed:</p>
  
  <pre><code>~/apps/myapp/releases/20150505130826$ ~/.rvm/bin/rvm ruby-2.1.5@myapp do bundle show aws-sdk
  /home/deployer/apps/myapp/shared/bundle/ruby/2.1.0/gems/aws-sdk-1.64.0
  </code></pre>
  
  <p>In the rails console:</p>
  
  <pre><code>2.1.5 :001 &gt; Gem.loaded_specs['aws-sdk'].full_gem_path
   =&gt; "/home/deployer/apps/myapp/shared/bundle/ruby/2.1.0/gems/aws-sdk-1.64.0"
  </code></pre>
  
  <p>So it seems like Rails <em>does</em> know about the gem.</p>
  
  <h3>AWS credentials look good</h3>
  
  <p>Identical credentials work perfectly (in dev mode and in production mode) on my local machine. Credentials also work using s3cmd on the remote server. When I deliberately set them to incorrect values on my local machine, I get Access Denied errors from AWS, not the whole action falling over like this.</p>
  
  <p>Where else can I look for problems?</p>