title: Unicorn can't find bundler using capistrano
body: <p>I can't seem to get bundler to work with unicorn on deploy. I am using capistrano to deploy.</p>
  
  <p>Looking at the unicorn log while deploying, I get the following errors:</p>
  
  <pre><code>/home/deployer/apps/my-app/releases/20130417145122)
  I, [2013-04-17T14:53:04.982369 #6565]  INFO -- : forked child re-executing...
  /home/deployer/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.5/lib/bundler/definition.rb:16:in `build': /home/deployer/apps/my-app/releases/20130417020329/Gemfile not found (Bundler::GemfileNotFound)
    from /home/deployer/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.5/lib/bundler.rb:148:in `definition'
    from /home/deployer/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.5/lib/bundler.rb:116:in `setup'
    from /home/deployer/.rvm/gems/ruby-1.9.3-p392@global/gems/bundler-1.3.5/lib/bundler/setup.rb:17:in `&lt;top (required)&gt;'
    from /home/deployer/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /home/deployer/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
  E, [2013-04-17T14:53:05.054942 #18166] ERROR -- : reaped #&lt;Process::Status: pid 6565 exit 1&gt; exec()-ed
  </code></pre>
  
  <p>Capistrano is cleaning up the last 5 releases of the app, and I see that bundler is trying to access a non-existent (last deleted) release.</p>
  
  <p>I found this code that I placed into unicorn.rb.erb, but it didn't help (I tried hardcoding it as well):
  <a href="http://davidvollbracht.com/blog/headachenewunicorn-capistrano-bundler-usr2" rel="nofollow noreferrer">http://davidvollbracht.com/blog/headachenewunicorn-capistrano-bundler-usr2</a></p>
  
  <pre><code>before_exec do |server|
    ENV['BUNDLE_GEMFILE'] = "&lt;%= current_path %&gt;/Gemfile"
  end
  </code></pre>
  
  <p>After deploying my code, I've started and stopped NGINX, and started and stopped unicorn, but I get the same issue.</p>
  
  <p>My original post on stack overflow is here, but I thought it had to do with the asset pipeline: <a href="https://stackoverflow.com/questions/16052084/asset-pipeline-not-working-in-rails-production-environment">Asset pipeline not working in rails production environment</a></p>
  
  <p>Thoughts?</p>
  
  <p>Here's my unicorn.rb.erb file:</p>
  
  <pre><code>working_directory "&lt;%= current_path %&gt;"
  pid "&lt;%= unicorn_pid %&gt;"
  stderr_path "&lt;%= unicorn_log %&gt;"
  stdout_path "&lt;%= unicorn_log %&gt;"
  
  listen "/tmp/unicorn.&lt;%= application %&gt;.sock"
  worker_processes &lt;%= unicorn_workers %&gt;
  timeout 30
  
  preload_app true
  
  before_exec do |server|
    ENV['BUNDLE_GEMFILE'] = "&lt;%= current_path %&gt;/Gemfile"
  end
  
  before_fork do |server, worker|
    # Disconnect since the database connection will not carry over
    if defined? ActiveRecord::Base
      ActiveRecord::Base.connection.disconnect!
    end
  
    # Quit the old unicorn process
    old_pid = "#{server.config[:pid]}.oldbin"
    if File.exists?(old_pid) &amp;&amp; server.pid != old_pid
      begin
        Process.kill("QUIT", File.read(old_pid).to_i)
      rescue Errno::ENOENT, Errno::ESRCH
        # someone else did our job for us
      end
    end
  end
  
  after_fork do |server, worker|
    # Start up the database connection again in the worker
    if defined?(ActiveRecord::Base)
      ActiveRecord::Base.establish_connection
    end
  end
  </code></pre>
  
  <p>Here's my unicorn_init.sh file:</p>
  
  <pre><code>#!/bin/sh
  ### BEGIN INIT INFO
  # Provides:          unicorn
  # Required-Start:    $remote_fs $syslog
  # Required-Stop:     $remote_fs $syslog
  # Default-Start:     2 3 4 5
  # Default-Stop:      0 1 6
  # Short-Description: Manage unicorn server
  # Description:       Start, stop, restart unicorn server for a specific application.
  ### END INIT INFO
  set -e
  
  # Feel free to change any of the following variables for your app:
  TIMEOUT=${TIMEOUT-60}
  APP_ROOT=/home/deployer/apps/my-app/current
  PID=$APP_ROOT/tmp/pids/unicorn.pid
  CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
  AS_USER=deployer
  set -u
  
  OLD_PIN="$PID.oldbin"
  
  sig () {
    test -s "$PID" &amp;&amp; kill -$1 `cat $PID`
  }
  
  oldsig () {
    test -s $OLD_PIN &amp;&amp; kill -$1 `cat $OLD_PIN`
  }
  
  run () {
    if [ "$(id -un)" = "$AS_USER" ]; then
      eval $1
    else
      su -c "$1" - $AS_USER
    fi
  }
  
  case "$1" in
  start)
    sig 0 &amp;&amp; echo &gt;&amp;2 "Already running" &amp;&amp; exit 0
    run "$CMD"
    ;;
  stop)
    sig QUIT &amp;&amp; exit 0
    echo &gt;&amp;2 "Not running"
    ;;
  force-stop)
    sig TERM &amp;&amp; exit 0
    echo &gt;&amp;2 "Not running"
    ;;
  restart|reload)
    sig USR2 &amp;&amp; echo reloaded OK &amp;&amp; exit 0
    echo &gt;&amp;2 "Couldn't reload, starting '$CMD' instead"
    run "$CMD"
    ;;
  upgrade)
    if sig USR2 &amp;&amp; sleep 2 &amp;&amp; sig 0 &amp;&amp; oldsig QUIT
    then
      n=$TIMEOUT
      while test -s $OLD_PIN &amp;&amp; test $n -ge 0
      do
        printf '.' &amp;&amp; sleep 1 &amp;&amp; n=$(( $n - 1 ))
      done
      echo
  
      if test $n -lt 0 &amp;&amp; test -s $OLD_PIN
      then
        echo &gt;&amp;2 "$OLD_PIN still exists after $TIMEOUT seconds"
        exit 1
      fi
      exit 0
    fi
    echo &gt;&amp;2 "Couldn't upgrade, starting '$CMD' instead"
    run "$CMD"
    ;;
  reopen-logs)
    sig USR1
    ;;
  *)
    echo &gt;&amp;2 "Usage: $0 &lt;start|stop|restart|upgrade|force-stop|reopen-logs&gt;"
    exit 1
    ;;
  esac
  </code></pre>