2
votes

I have a Rails application configured to deploy via Capistrano and RVM. When I run a cap my_stage deploy (this applies for all my stages), Capistrano bundle installs to /var/www/my_app/shared/bundle, even though I have specified in my config/deploy.rb file that I want it to use the 1.9.2@my_app gemset.

This is contrary to my expectations - I expect Capistrano to deploy into my user's home directory: ~/.rvm/gems/ruby-1.9.2-p290@my_gemset/gems.

Am I doing something wrong? Or is this expected behaviour.

Here is my deploy file:

require 'capistrano/ext/multistage'
require 'bundler/capistrano'

set :stages, %w(local development staging production)
set :default_stage, "local"

set :application, "My Rails App"
set :repository,  "[email protected]:MyApp/my_app.git"

set :scm, :git
set :deploy_to, "/var/www/my_app"
set :use_sudo, false
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "my_key.pem")]

# RVM
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) 
require 'rvm/capistrano'                               
set :rvm_ruby_string, '1.9.2@my_app'
set :rvm_type, :user
set :user, 'my_user'

Here is my .rvmrc file in my Rails app:

rvm_trust_rvmrc_flags=1
rvm use 1.9.2@my_app

Thanks, Max

2
Getting the same problem, dunno wtf is going on... I installed RVM for multiuser... check out this site: blog.mcmoyer.com/using-rvm-gemsets-for-deploying didn't help with my situation though :( - hamstar
Initially, I was annoyed that I could not log on to the production server an run rake db:migrate from the deployment directory because RVM was looking for gems in ~/.rvm/..., but Capistrano had installed them to <deployment_root>/shared/bundle. I worked around this by running bundle exec rake db:migrate, which first loads Bundle options from <deployment_root>/.bundle/config. However, I would still like an explanation of this behavior. - maxenglander

2 Answers

3
votes

While I don't use Capistrano (I deploy to Heroku, so git is all I need), I'd imagine it probably uses the current recommended best-practice of calling "bundle install --deployment" on the production server which, according to the Bundler documentation, installs all of the gems locally into your app's deployment root instead of your GEM_HOME (which would be your RVM gemset in this case). When you use "bundle install --deployment" you have to use "bundle exec rake" (or bin/rake if you also installed with --binstubs) to run Rake in your bundled environment.

The main reason Bundler recommends deploying this way is to avoid any "dependency hell" situations in your deployed app. While an RVM gemset does the same thing, Bundler doesn't assume the presence or absence of RVM, it only assumes Ruby and RubyGems are installed and working.

When using Bundler, it's always recommended to use either "bundle exec" or binstubs regardless of the --deployment flag (even on your local machine in development or test mode), this way you can be 100% sure that you're only depending on the gems required by your Gemfile (and their dependencies, of course) and you can't accidentally deploy an app that depends on any locally installed gems on your machine that aren't deployed to the production server.

0
votes

Adding capistrano/rvm and setting my ruby version inside production.rb did a trick.

set :rvm_ruby_version, 'ruby-2.4.1'