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:
This may be installed an root (to /usr/local/rvm) or as a user (to ~/.rvm)
Now enable bash completion as follows:
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:
You may now use the version:
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:
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:
You may switch to the new gemset using any of the following commands (take your pick):
And naturally, you may install gems in that gemset as follows:
To list all created gemsets (environments), use:
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:
For further information about RVM, be sure to check out the official site. This is a must-have for any Ruby developer.