In this article I will describe the steps I used in order to get a Rails environment up and running under Linux using RVM (I’m using Fedora 15). I will assume you know your way around Linux and you should be able to replace any of the Fedora specifics with your distributions corresponding tools (in particular YUM the package manager). If you are following this article and are using Mac OS X you should be able to replace the YUM package manager with homebrew.
I will be installing and using Ruby with RVM (Ruby Version Manager) which is a command line tool which will allow me to easily install and manage multiple version of Ruby. This is a requirement since I will be developing using Ruby and jRuby.
What is RVM
As I mentioned above, RVM is a command line tool which allows us to install, manage and use multiple ruby environments. You can install ruby 1.8.7, 1.9.2, JRuby, etc. and you can also maintain several environments of the save version but with different set of gems installed. RVM handles this by creating self-contained environments isolated from each other. This also allows us to test our applications with different versions of ruby and/or setups.
I think its time to get started with the installation.
Before we start with the installation of RVM, let’s make sure we have all of the tools used by RVM.
- bash (at least version 3.2)
Make sure you have these tools installed using your package manager. This is the command with yum (Fedora package manager):
yum install bash awk sed grep which tar curl gunzip bunzip git subversion
I know it might be a bit redundant, but it ensures that I’m not missing anything. In my case I already had them all installed since I had selected a Development Workstation during install, but it was going to perform an upgrade on bash, curl, git, grep and subversion.
RVM can be installed as a single or multi user. We will be using the single user install as the RVM documentation recommends which will create a self-contained RVM installation under our home directory ~/.rvm (in oder to perform the multi user installation just run the same install command using the root account which installs under /usr/local/rvm)
We will be installing the latest version from git, if you need or want to install a specific version refer to the installation documentation on the RVM site.
Install the latest release
user$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)
The install script will give us some extra information and additional dependencies depending on the environments we intend to use:
For Ruby (MRI and Ruby Enterprise Edition)
yum install -y gcc-c++ patch readline readline-devl zlib zlib-devel libyaml-devel libffi-devel openssl-devel
yum install -y make bzip2
yum install -y iconv-devel
yum install -y java
Most Rails install will need the following
yum install -y libxslt-devel sqlite-devel openssh
Now we just need to update or .bashrc file and start installing Ruby. Add the following line at the end of .bashrc
[[ -s "~/.rvm/scripts/rvm" ]] && source "~/.rvm/scripts/rvm"
In my case the script updated my .bashrc file.
Before we continue you can either close your terminal session and start a new one or execute the following command
Installing Ruby and Rails
I’m going to set up two environments, ruby 1.9.2 and jruby. First up is ruby 1.9.2
rvm install 1.9.2
This will install Ruby from source and create the environment under ~/.rvm/rubies/ruby-1.9.2-p180. Next we switch to our new ruby environment and set it as the default and start the Rails installation.
rvm use 1.9.2 --default
gem install rails
Next we install our JRuby environment.
rvm install jruby
JRuby is able to load the 1.8 or 1.9 Ruby API (Defaulting to 1.8). If you need to run with the 1.9 API you will need to export JRUBY_OPTS=”–1.9″ which is not much of a problem if you don’t use it very often. But, if you want to use the 1.9 API as your default it would really be nice to have it done each time you have RVM load JRuby. To do so, we will be using RVM hooks.
RVM hooks are files located under ~/.rvm/hooks/ which are triggered depending on their name. Supported hooks include after_user, before_install, after_install, after_do and after_cd. Each hook has the entire RVM environment available.
We will be using the after_use hook in order to change our JRuby API to 1.9 each time we execute rvm use jruby. create ~/.rvm/hooks/after_use (or update if it already exists) with the following code
case "$GEM_HOME" in
JRUBY_OPTS="--1.9" ; export JRUBY_OPTS
Now, whenever you execute rvm use jruby you should see the following
Now switch over to JRuby we can now install Rails
rvm use jruby
gem install rails
If you want jruby to be the default environment you use just add the –default at the end of your use command. A neat feature of rvm is that while using jruby you can actually use ruby instead of jruby from the command line.