Rails 3 -- Bundler/Capistrano Errors

Ruby on-RailsRuby on-Rails-3CapistranoBundler

Ruby on-Rails Problem Overview


I have a basic Rails 3 app working locally on my development box, but want to test out deploying early on to make sure everything works. I'm using Capistrano to deploy.

When I run cap deploy (after all the other necessary setup), it breaks on this command with this error:

[...]
* executing 'bundle:install'
* executing "bundle install --gemfile /var/www/trex/releases/20100917172521/Gemfile --path /var/www/trex/shared/bundle --deployment --quiet --without development test"

servers: ["www.[my domain].com"]
[www.[my domain].com] executing command
** [out :: www.[my domain].com] sh: bundle: command not found
command finished
[...]

So it looks like it can't find the bundle command on the server.

However, when I log in to the server...

$ ruby -v
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
$ rails -v
Rails 3.0.0
$ bundle -v
Bundler version 1.0.0

...the bundle command works just fine.

What could be going wrong?

(Furthermore, for completeness:)

$ which ruby
~/.rvm/rubies/ruby-1.9.2-p0/bin/ruby
$ which rails
~/.rvm/gems/ruby-1.9.2-p0/bin/rails
$ which bundle
~/.rvm/gems/ruby-1.9.2-p0/bin/bundle

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

UPDATE:

For RVM >= 1.11.3, you should now just use the rvm-capistrano gem. For older RVM >= 1.0.1, the answer below still applies.


ORIGINAL ANSWER:

Okay, though I still haven't gotten a full cap deploy to work, I did fix this problem. The problem was Capistrano trying to use a different path for Bundler (and other gems) than the RVM paths.

Check your Capistrano path by doing cap shell, then echo $PATH. You'll probably see your standard /usr/local/bin and /usr/bin, but that's not where RVM has Bundler, et al., stored.

Edit your Capistrano config/deploy.rb file, and add the following lines, per these instructions:

# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

# Load RVM's capistrano plugin.    
require "rvm/capistrano"

set :rvm_ruby_string, '1.9.2'
set :rvm_type, :user  # Don't use system-wide RVM

That finally got Capistrano to see Bundler and start loading gems appropriately.

Solution 2 - Ruby on-Rails

Bundler isn't found because .bash_profile is not being loaded and thus your PATH is wrong. This is probably because you have the RVM script in .bash_profile.

The simple answer is to move the RVM script from .bash_profile to .bashrc and Capistrano should be able to find it (also verify that .bash_profile sources .bashrc).

Capistrano uses SSH to execute commands on the server via a non-interactive shell. This shell session will source .bashrc but not .bash_profile. I added an ECHO statement to both and ran an LS via SSH. You can see in the results below that only .bashrc is sourced:

$ ssh user@123.amazonaws.com ls
.bashrc loaded
git
file1
file2

Solution 3 - Ruby on-Rails

I had an identical problem using rbenv. The solution was to take the rbenv specific lines from the bottom of my .bashrc file and put them at the top. The first line of my .bashrc file was returning aborting if the shell wasn't running in interactive mode.

Solution 4 - Ruby on-Rails

That last line should actually be

set :rvm_type, :user

that is, user must be a symbol and not a variable, otherwise you'll get

undefined local variable or method `user'

Solution 5 - Ruby on-Rails

No rvm/capistrano worked for me. The best solution I found was adding to deploy.rb file the following line (it's for non system-wide RVM):

set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

Solution 6 - Ruby on-Rails

It was my understanding that the bundle command is not found because the PATH variable, defined in the user's ~/.bash_profile, isn't loaded by Capistrano.

To get around this I have created a task :bundle_gems.

task :bundle_gems do
	run "cd #{deploy_to}/current && export PATH=/usr/local/pgsql/bin:/opt/ruby-enterprise-X.X.X/bin:$PATH && bundle install vendor/gems"
end

Note that I also include the path to PostgreSQL binaries - installation of the pg gem was failing because they could not be found, even when bundle could be found.

This seems like a messy approach, though. Presumably there is a more 'global' place to define paths to binaries that I don't know about.

Update 23/12

To add a directory to $PATH for all users: https://serverfault.com/questions/102932/adding-a-directory-to-path-in-centos

However this still won't be loaded because it is a non-interactive non-login shell.

One suggestion was to add the paths to /etc/bashrc: https://stackoverflow.com/questions/940533/how-do-i-set-path-such-that-ssh-userhost-command-works

However this also didn't work for me. I believe its because SSH doesn't load /etc/bashrc either.

Another suggestion was to edit ~/.ssh/environment: http://www.ruby-forum.com/topic/79248. However this seems almost as messy as specifying the paths in deploy.rb.

Solution 7 - Ruby on-Rails

This one worked for me:

set :bundle_cmd, 'source $HOME/.bash_profile && bundle'

Solution 8 - Ruby on-Rails

I tried a number of the suggestions. Had problems with setting the paths in the deploy.rb file for the RVM environment. My final solution was to include the following:

In the config/deploy.rb file add:

require "bundler/capistrano"

Also in config/deploy.rb, or in my case config/production.rb as I was using the multistage option for Capistrano

after "deploy", "rvm:trust_rvmrc"

This step simply ensures that we stop getting the 'do you want to trust the .rvmrc file' and it calls a task in the deploy.rb file such as:

namespace :rvm do
   task :trust_rvmrc do
      run "rvm rvmrc trust #{release_path}"
   end
end

After putting in these slight changes I was able to run cap production deploy which checked out the code; executed the asset pipeline deployment, linked up the release folder to current, executed bundle install and cleaned up.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionT.J. SchuckView Question on Stackoverflow
Solution 1 - Ruby on-RailsT.J. SchuckView Answer on Stackoverflow
Solution 2 - Ruby on-RailsPete CampbellView Answer on Stackoverflow
Solution 3 - Ruby on-RailsLelonView Answer on Stackoverflow
Solution 4 - Ruby on-RailsRussKView Answer on Stackoverflow
Solution 5 - Ruby on-RailshipertrackerView Answer on Stackoverflow
Solution 6 - Ruby on-RailsSai PerchardView Answer on Stackoverflow
Solution 7 - Ruby on-RailsNesha ZoricView Answer on Stackoverflow
Solution 8 - Ruby on-RailsGrant SayerView Answer on Stackoverflow