Home

Awesome

rvm-capistrano

Description

RVM / Capistrano Integration Gem

Compatibility

Installation

RVM / Capistrano integration is available as a separate gem

$ gem install rvm-capistrano

Or, if the capistrano gem is already in your Gemfile, then add rvm-capistrano:

$ echo "gem 'rvm-capistrano'" >> Gemfile
$ bundle install

Modules

Since version 1.4.0 rvm-capistrano is divided into separate modules which allow selecting which parts of it should be included.

rvm/capistrano:

By default rvm/capistrano loads: selector, info_list, install_rvm, install_ruby, create_gemset.

Warning: selector and selector_mixed are to be used separately they can not be used both at the same time.

Requiring

Minimal code to load this gem is:

require "rvm/capistrano"

Usually it's placed in config/deploy.rb.

Example

The following code will:

Example:

require "rvm/capistrano"

set :rvm_ruby_string, :local              # use the same ruby as used locally for deployment
set :rvm_autolibs_flag, "read-only"       # more info: rvm help autolibs

before 'deploy:setup', 'rvm:install_rvm'  # install/update RVM
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset, OR:
# before 'deploy:setup', 'rvm:create_gemset' # only create gemset

Disabling bundle --deployment when using gemsets

Using gemsets is safer from bundle --deployment which is default, to disable it use:

set :bundle_dir, ''
set :bundle_flags, '--system --quiet'

RVM + Ruby on every deploy

Update RVM and make sure Ruby is installed on every deploy:

require "rvm/capistrano"

set :rvm_ruby_string, :local        # use the same ruby as used locally for deployment

before 'deploy', 'rvm:install_rvm'  # install/update RVM
before 'deploy', 'rvm:install_ruby' # install Ruby and create gemset (both if missing)

Create application alias and wrappers

For server scripts and configuration the easiest is to use wrappers from aliased path.

require "rvm/capistrano/alias_and_wrapp"
before 'deploy', 'rvm:create_alias'
before 'deploy', 'rvm:create_wrappers'

To see the path to be used in scripts use:

cap rvm:show_alias_path

It will show either that the path does not exist yet:

*** [err :: niczsoft.com] ls: cannot access /home/ad/.rvm//wrappers/ad/*: No such file or directory

or in case it exist it will list all available wrappers:

...
 ** [out :: niczsoft.com] /home/ad/.rvm//wrappers/ad/ruby
...

This will allow to use clean scripts where proper RVM settings are automatically loaded from the aliased wrappers. For example configuring PassengerRuby with /home/ad/.rvm//wrappers/ad/ruby, this way there is no need for changing scripts when the application ruby changes. In the same spirit you can use wrapper for bundle in cron or init.d scripts with /home/ad/.rvm//wrappers/ad/bundle exec [command] - it will automatically load proper configuration for the application, no need for any tricks.

To use the ruby version currently active locally

set :rvm_ruby_string, :local

To restrict rvm to only :app servers

Warning, when using :rvm_require_role parallel is used to select shell per server instead of :default_shell

set :rvm_require_role, :app
require "rvm/capistrano/selector_mixed"

It is important to set :rvm_require_role before require "rvm/capistrano/selector_mixed".

To restrict rvm to only some servers

set :rvm_require_role, :rvm
require "rvm/capistrano/selector_mixed"
role :rvm, "web1", "web2"
role :app, "web1", "web2", "web3"

To control rvm shell manually

require "rvm/capistrano/base"
set :default_shell, :bash
task :example do
  run "echo 'in rvm'", :shell => fetch(:rvm_shell)
end

Disable rvm shell for single command

task :example do
  run "echo 'not in rvm'", :shell => :bash
end

Show info on remote rvm and list rubies

cap rvm:info_list

Options

Tasks

$ cap -T rvm
cap rvm:create_gemset        # Create gemset
cap rvm:export_gemset        # Export the current RVM ruby gemset contents to a file.
cap rvm:import_gemset        # Import file contents to the current RVM ruby gemset.
cap rvm:info                 # Show rvm info
cap rvm:info_list            # Show info and list rubies
cap rvm:list                 # List rvm rubies
cap rvm:install_ruby         # Install RVM ruby to the server, create gemset ...
cap rvm:install_rvm          # Install/update RVM of the given choice to the server.
cap rvm:install_pkgs         # Install RVM packages to the server.
cap rvm:install_gem   GEM=my_gem  # Install gem {my_gem} on the server using selected ruby.
                                  # Use `ENV['GEM'] = "bundler"` in script to specify gems.
cap rvm:uninstall_gem GEM=my_gem  # Uninstall gem {my_gem} from the server selected ruby.
cap rvm:create_alias         # create #{application} alias
cap rvm:create_wrappers      # create wrappers for gem executables
cap rvm:show_alias_path      # show path to aliased path with wrappers

Development

SM Framework extension for gem development:

$ curl -L https://get.smf.sh | sh
$ sm ext install gem mpapis/sm_gem
$ sm gem --help