RVM Quick Start

RVM

This awesome technology allows you to manage all of your ruby and gem combinations! If you are new to Ruby, trust me, you want to use RVM for every project you do. It keeps the environment and gems (libraries) separate for each project, preventing clashes in libraries/dependencies and allowing you to easily have other people get their system set up and ready to go for working in your project.

Quick Usage Reminder

Since I often come back to this page to remind myself how to RVM-ify a folder, I added this quick start here:

rvm --ruby-version use 1.9.3@my_app

Install

Follow the instructions here to Install as a user…

  • I tested by following the instructions to do rvm list known
  • If the install script does nothing or complains about certificates, try curl without cert validation:
     bash < <(curl -k https://rvm.beginrescueend.com/install/rvm)
  • Note the warnings given in console during the rvm installation. In particular, the one about removing ‘&& return’ statements from .bashrc.
  • I also got errors in the compile steps… Google is your friend. Typically goes like:
    • Install XCode (>=4.2)
    • Start XCode and install Command Line Utilities
    • Try again with: rvm install 1.9.3 --with-gcc=gcc

When installing rvm on a fresh MacBook Pro, I noticed I now have ruby v2.0!

$ rvm list rubies
rvm rubies
=* ruby-2.0.0-p0 [ x86_64 ]

Different rubies

rvm install ruby-1.9.3
rvm install jruby-1.5.5

Listing:

$ rvm list
rvm Rubies
  jruby-1.4.0 [[x86_64-java]]
  jruby-1.5.5 [[x86_64-java]]

System Ruby
  system [ ]

For example:

rvm install 1.9.2-p180

and a bit of patience: it may take up to 30 min to get everything downloaded, compiled, and configured.

Installing Gems

[notice]DO NOT use sudo with RVM…[/notice]

It is also helpful to skip installing the rdocs…

Edit ~/.gemrc

install: --no-ri --no-rdoc
update: --no-ri --no-rdoc

Gemsets

Kind of the whole point behind RVM is to allow you to keep your collection of specific gems intact for each project — instead of polluting your ruby gem system space and getting incompatibility issues. In short, use Gemsets to keep your dependencies under control on a per-project basis!

rvm use jruby-1.5.5
rvm gemset create movie

In the future, you can simply say:

$ rvm jruby-1.5.5@movie

Or better yet, create a .rvmrc file as shown below in ‘Project Configuration’ so that you automatically switch to the correct ruby/gem combination when changing into your project directory.

To make it easy to create new gemsets on the fly, edit ~/.rvmrc

rvm_gemset_create_on_use_flag=1

Then you can simply indicate you want to use the gemset, and it will be automatically created.

rvm ruby-1.9.3-p392@cuke
$ rvm gemset list

gemsets for ruby-1.9.3-p392 (found in /Users/jon/.rvm/gems/ruby-1.9.3-p392)
   (default)
=> cuke
   global

Trouble Compiling Native Extensions

During bundle install, if you run into an error like this (for any similar gem):

Installing nokogiri (1.5.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

Then go here and download and install the osx-gcc-installer, and bundle install again.

Project Configuration

Create a project configuration file that will automatically load up the proper RVM setup. Create a new folder for the app/project. Next, create a .rvmrc file containing the following line:

echo "rvm ruby-1.9.3@blazeapp" > .rvmrc

RVM will find it only when you cd into the folder, or force the issue:

$cd .

Fancier .rvmrc File

Here is a slightly fancier version that I don’t recall where it is from, maybe the RVM website?:

#!/usr/bin/env bash

# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory

# First we specify our desired [@], the @gemset name is optional.
environment_id="ruby-1.9.2-p290@blazev2"

#
# Uncomment following line if you want options to be set only for given project.
#
# PROJECT_JRUBY_OPTS=( --1.9 )

#
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
#
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" 
  && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
  . "${rvm_path:-$HOME/.rvm}/environments/$environment_id"

  if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
  then
    . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
  fi
else
  # If the environment file has not yet been created, use the RVM CLI to select.
  if ! rvm "$environment_id" --create
  then
    echo "Failed to create RVM environment '${environment_id}'."
    exit 1
  fi
fi

Test

If you have set things up properly, now a project can be “bootstrapped” simply by cd’ing into the project directory.

$ cd ~/railsprojects/gitfun
$ rvm system
$ which ruby
/usr/bin/ruby
$ cd MovieReviewsPlayground/
jonsmac2-2:MovieReviewsPlayground jon$ which ruby
/Users/jon/.rvm/rubies/jruby-1.5.5/bin/ruby

 

Leave a Reply