Search code examples
ruby-on-railsruby-on-rails-4capistranounicorn

Issue with running bundle using Capistrano


I've seen this issue in a few other questions/Github issues, but they haven't been able to shed enough light to lead to the solution.

The error:

bash: bundle: command not found

SSHKit::Runner::ExecuteError: Exception while executing as my-user@my-IP-address: cd /path-to-my-app/current ; bundle exec unicorn -D -c config/unicorn.rb -E production exit status: 127

cd /path-to-my-app/current ; bundle exec unicorn -D -c config/unicorn.rb -E production stdout: Nothing written
cd /path-to-my-app/current ; bundle exec unicorn -D -c config/unicorn.rb -E production stderr: bash: bundle: command not found

I'm using the latest version of Capistrano on my rails app. Specifically, it's a Rails 4.2.0 app with the gems capistrano-rails and capistrano-rvm.

I have a pretty standard config/unicorn.rb file, which is how I start unicorn while logged into my server. I first kill the current PID with:

kill -9 PID

Then I start unicorn with:

bundle exec unicorn -D -c /path-to-my/config/unicorn.rb -E production

This works great, but obviously I need capistrano to do that, so I essentially have those tasks in my deploy.rb file, however I get that error mentioned above. Here are the two tasks I have in deploy.rb:

namespace :deploy do
  namespace :unicorn do
    task :restart do
      on roles(:app), in: :sequence, wait: 5 do
        execute "kill -s USR2 `cat /path-to-my/tmp/pids/app-name.pid`"
      end
    end

    desc 'Start unicorn' 
    task :start do
      on roles(:app), in: :sequence, wait: 5 do
        execute "cd #{current_path} ; bundle exec unicorn -D -c config/unicorn.rb -E production"
      end
    end
  end
end

I have a similar task to restart DelayedJob, which throws the same error. The command is execute "cd #{current_path} ; RAILS_ENV=production bin/delayed_job -n2 restart". Like I mentioned above, when logged into the server with my user (same user I use for Capistrano), all of these tasks work as expected.

Every other built-in task works great with Capistrano, like precompiling assets, migrating my database, etc. It is the custom tasks I've added that are getting the error.

Let me know if I can add anything to help solve the problem.


Solution

  • Look at output of Capistrano carefully. Such commands as bundle assets:precompile makes initialization of required ruby version:

    cd /<app path>/20150301211440 && ( RAILS_ENV=staging /usr/local/rvm/bin/rvm 2.1.5 do bundle exec rake assets:precompile )
    

    Seems like you don't do it. So, probably you a using system ruby by default and it doesn't have a bundler gem.

    Try to specify ruby version use RVM in your commands too. I think it should fix your problem.