Subscribe LATEST BLOG POST (MORE)

RVM - Installation & Usage (08/16)

Recently I tried to install several Ruby on Rails websites to my Linode VPS, which depend on different versions of Rails – as a troublesome issue. RVM is exactly the tool to handle this case.

The following are the steps I have taken.

Install RVM

Follow the instructions on RVM Installation Page. I created a dedicated user “www” to deal with website related tasks, so I installed RVM for that user.

In the Bash shell (statements with prefix “#” are just comments):

# Change to user "www"
su - www

# Install RVM
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

# Load RVM into your shell sessions as a function
# I use .bashrc instead of .bash_profile
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bashrc

# Reload shell configuration & test
. ~/.bashrc
type rvm | head -1

# If you see "rvm is a function", then the installation should have succeeded.
# After that, be sure to execute the following command and check the notes:
rvm notes

# For example, I execute the following to make ruby 1.8.7 as default
rvm install 1.8.7
rvm system ; rvm gemset export system.gems ; rvm 1.8.7 ; rvm gemset import system # migrate your gems
rvm --default 1.8.7

Configure project to use different Gemsets

I learned about this from this web page: The Path to Better RVM & Passenger Integration

For example, I wanted to install Redmine and let it use a separate set of gems (since the latest stable version of Redmine requires Rails 2.3.11, while most of my projects use Rails 3). Steps are as follows:

# Change to user "www" if you haven't
su - www
# Change to the project's directory
cd ~/redmine

# Create new gemsets profile, this would generate a .rvmrc file in the current directory.
# Next time when you cd into this directory, a prompt will "pop-up" to let you confirm whether
# this .rvmrc file can be trusted.
rvm use ruby-1.8.7@redmine --rvmrc --create

# Add the content of the next snippet to file config/setup_load_paths.rb
vi config/setup_load_paths.rb

Content to be add to config/setup_load_paths.rb (this file will be processed by Phusion Passenger):

if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
  begin
    rvm_path     = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
    rvm_lib_path = File.join(rvm_path, 'lib')
    $LOAD_PATH.unshift rvm_lib_path
    require 'rvm'
    RVM.use_from_path! File.dirname(File.dirname(__FILE__))
  rescue LoadError
    # RVM is unavailable at this point.
    raise "RVM ruby lib is currently unavailable."
  end
end

# Select the correct item for which you use below.
# If you're not using bundler, remove it completely.

# Note: since Redmine doesn't use bundler, the following statements are commented out.
# You can remove them in this case. If your project uses bundler, choose to uncomment one of 
# the following snippets.

# If we're using a Bundler 1.0 beta
#ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
#require 'bundler/setup'

# Or Bundler 0.9...
#if File.exist?(".bundle/environment.rb")
#  require '.bundle/environment'
#else
#  require 'rubygems'
#  require 'bundler'
#  Bundler.setup
#end

After that, you can cd into your project’s directory and install the gems that are required by the project. For example:

# Change to user "www" if you haven't
su - www
# Change to the project's directory
cd ~/redmine

# You can execute "rvm info" to check which rvm profile is being used.

# Install gems required by Redmine
gem install rails -v=2.3.11
gem install rack -v=1.1.1
# ...

# Then you can add configurations for Passenger and Nginx/Apache etc.

References

Comments

OPEN SOURCE

SHARED READING