Recommend
If you like my work with RVM, please recommend me *with a comment as to why you recommend me* on
Working With Rails – Thank You!
IRC
I am 'wayneeseguin' in #rvm on irc.freenode.net
If I do not respond right away, leave a message and I'll respond or leave you a memo when I am around.
Sponsors
$ rvm help # Documentation Index

rvmrc files

There are 3 types of rvmrc files, system user and project. They are discussed in detail below.

System /etc/rvmrc

The system rvmrc file is loaded before RVM initializes and before the user's ~/.rvmrc. /etc/rvmrc settings are applied to all users on the system.

User $HOME/.rvmrc

The users rvmrc file overwrites settings in /etc/rvmrc and is loaded before RVM initializes. $HOME/.rvmrc settings are applied only for the user belonging to $HOME.

System / User rvmrc examples

Have RVM install rubies when used instead of only displaying a warning and exiting.

rvm_install_on_use_flag=1

Have RVM compile using say, 3, compile threads.

rvm_make_flags="-j 3"

Have RVM install to a different location (notice that it ends with /rvm).

rvm_path=/opt/rvm

Have RVM compile rubies and libraries as x86_64 on *Mac OS X*.

rvm_archflags="-arch x86_64"

Similarly for i386 on *Mac OS X*.

rvm_archflags="-arch i386"

More examples may be found in ~/.rvm/examples/rvmrc.

Project .rvmrc

The project .rvmrc file is different than the system & user. System & user rvmrc files are meant for altering the settings and behavior of RVM. The project rvmrc files are intended to be used to setup your project's ruby environment when you switch to the project root directory.

If you are using the latest version of RVM (rvm get head) then you can take advantage of a few new features, namely hooks and the new project specific ‘.rvmrc’ file. (Available in RVM 0.0.85+)

As of RVM 1.8.0, after a survey where greater than 80% of respondants wanted the feature on by default, project .rvmrc files are opt-out by default (therefore on). In order to disable this feature, set the following value in either /etc/rvmrc or ~/.rvmrc:

rvm_project_rvmrc=0

Note that project .rvmrc files are shell scripts that run when you cd into a directory containing one. You are asked to trust it the first time as well. This means that you can put any commands and code you'd like into them in order to get your environment for a project bootstrapped and ready to use. Be sure to read them before trusting them if you did not write them.

Let’s say I have my project directory as ~/projects/ and in this directory I have three projects 'projecta', 'projectb', and 'projectc'. Project a runs on jruby, project b runs on ree 1.8.7, and project c runs on 1.9.1. The setup:

mkdir -p ~/projects/projecta ~/projects/projectb ~/projects/projectc
cd ~/projects/projecta/
rvm --rvmrc --create jruby@projecta
cd ~/projects/projectb/
rvm --rvmrc --create ree@projectb
cd ~/projects/projectc/
rvm --rvmrc --create 1.9.1@projectc

Then if I type this:

cd ~/projects/projecta

[jruby-1.4.0] $ ruby -v

  jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_15) [x86_64-java]

[jruby-1.4.0] $ gem env gemdir

  /Users/wayne/.rvm/gems/jruby/1.4.0@projecta

I will then be running jruby with 'projecta' gemset and we see that the prompt is now prefixed with '[jruby-1.4.0]'.

Now if we change to project b's directory.

cd ~/projects/projectb

[ree-1.8.7-2009.10] $ ruby -v

  ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0], MBARI 0x6770, Ruby Enterprise Edition 2009.10

[ree-1.8.7-2009.10] $ gem env gemdir

  /Users/wayne/.rvm/gems/ree/1.8.7@projectb

We see that we are now running ree with a 'projectb' gemset and we see that the prompt is now prefixed with '[ree-1.8.7-2009.10]'.

Finally we change to project c's directory.

cd ~/projects/projectc

[ruby-1.9.1-p243] (0) $ ruby -v

  ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-darwin10.2.0]

[ruby-1.9.1-p243] (0) $ gem env gemdir

  /Users/wayne/.rvm/gems/ruby/1.9.1@projectc

We see that we are now running ree with a 'projectc' gemset and we see that the prompt is now prefixed with '[ruby-1.9.1-p243]'.

Note that the project .rvmrc file is actually a shell script and may contain any valid shell commands in order for you to initialize your project's environment. Be sure to read them before trusting them if you did not write them.

To turn off the project specific rvmrc functionality in your $HOME/.rvmrc set

rvm_project_rvmrc=0

To enable switching to default / system when leaving a directory in your $HOME/.rvmrc set

rvm_project_rvmrc_default=1

NOTE: As of RVM v1.8.0, the $rvm_project_rvmrc_default is set to OFF by default. However, rvm_project_rvmrc is set to ON.

Warning

Do NOT use shell commands in the System and User rvmrc files. For example do not put 'rvm ree' or any other such commands in these two rvmrc files. They are for setting environment variables only to modify RVM's behavior and settings.

More Examples

If you use gemset export/import then you can have this be automatically run when you cd into a project directory as follows.

if [[ -s "projectname.gems" ]] ; then
  rvm gemset import projectname.gems | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
fi

If you use Bundler you can add in an automatic bundler install like so:

if ! command -v bundle ; then
  gem install bundler
fi

If you would like to auto bundle when you cd to a project directory you may do the following (note that I am filtering success output to reduce noise).

  bundle | grep -v 'Using' | grep -v 'complete' | sed '/^$/d'

A complete example with the above techniques would look like the following. In addition I have abstracted the ruby string and gemset name out for easy updates to the .rvmrc file when you decide to change either.

I also show here how you check if the desired interpreter is installed and if not print an error message to the user stating this fact.

#!/usr/bin/env bash

ruby_string="ruby-1.9.2-p136"
gemset_name="projectname"

if rvm list strings | grep -q "${ruby_string}" ; then

  # Load or create the specified environment
  if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
    && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
    \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
  else
    rvm --create  "${ruby_string}@${gemset_name}"
  fi

  (
    # Ensure that Bundler is installed, install it if it is not.
    if ! command -v bundle ; then
      gem install bundler
    fi

    # Bundle while redcing excess noise.
    bundle | grep -v 'Using' | grep -v 'complete' | sed '/^$/d'
  )&

else

  # Notify the user to install the desired interpreter before proceeding.
  echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."

fi

Community Resources

RVM Documentation Index