In this article, I’ll be going through the creation of virtual Ruby development environments. Using this sort of approach when developing applications allows you to start from a clean state at any time and also have vastly different environments setup for various projects on the same server.
I must say that the brilliant RVM is about as good as it gets when it comes to virtual development environments. RVM can install Ruby for you and contains a very complete virtual environment experience with bash completion and even automatic switching of environments based on the project directory you are in.
RVM Installation & Usage
You may install RVM, the latest Ruby and rubygems as follows:
curl -L https://get.rvm.io | bash -s stable --ruby
This may be installed an root (to /usr/local/rvm) or as a user (to ~/.rvm)
Now enable bash completion as follows:
echo "[[ -r $rvm_path/scripts/completion ]] && source $rvm_path/scripts/completion" >> ~/.bashrc source ~/.bashrc
You may uninstall RVM at any time using the command:
View the Ruby version and any other installed versions as follows:
To install an older Ruby version, simply run install:
rvm install 1.8.7
You may now use the version:
rvm use 1.8.7
You may check what version you are on at any time by simply using:
To upgrade RVM to the latest version at any time, simply use:
rvm get stable
Environments are created against specific Ruby versions. So for example, one could have two environments called “project1” as long as they are created against a different version of Ruby.
To create a virtual environment and install some gems (Ruby modules) in it, switch to the Ruby version you are interested in and create a gemset:
rvm use 1.9.3 rvm gemset create project1
You may switch to the new gemset using any of the following commands (take your pick):
rvm gemset use project1 rvm @project1 rvm 1.9.3@project1
And naturally, you may install gems in that gemset as follows:
gem install redcarpet
To list all created gemsets (environments), use:
rvm gemset list
To force a particular environment for a project, you may create a .rvmrc file in the project folder with the appropriate commands needed to run the project:
rvm use 1.9.3 rvm gemset use project1
For further information about RVM, be sure to check out the official site. This is a must-have for any Ruby developer.