Linux: How To Use RVM to Manage Ruby

Ruby is a very popular programming language that has Ruby on Rails, or RoR, is a popular development framework that allows you to easily get your application up and running with minimal hassle.

Developing applications often times requires that you emulate different environments. Different versions of Ruby may be necessary for different projects. With conventional installations, this would impede your ability to be flexible.

Luckily, the Ruby Version Manager, known more widely as RVM, allows you to easily install multiple, contained versions of Ruby and easily switch between them.

You can read how to install RVM, Ruby and Rails on Linux Mint. Which it is also valid for Ubuntu and Debian (which have proven on virtual machines, single installation).

This article will assume that you’ve already installed RVM. We will be discussing the basic usage of this utility and how you can properly manage your Ruby environments.

Basic Syntax

If you’ve already installed RVM, you should have the rvm command available to you. This is the way we call RVM.

The basic syntax of the command is:

There are also RVM flags that can alter the behavior of RVM, which are given in a similar way to command options.

You can get a list of the available commands by typing:

If you would like help on a specific command, you can reference it after “help” to get more detailed instruction:

How To Enable Tab Completion

We will enable RVM tab completion by putting the following line in our .bashrc file:

This will allow us to complete RVM commands by typing the TAB key twice after entering part of the command. For instance, we can type:

At this point, we can hit TAB twice, and it will complete to:

We can then finish typing parameters.

Keep in mind that this also works with arguments. If you are switching to another Ruby version, you can type:

After you type a space and then TAB twice, you will be presented with a list of the available Ruby versions.

How To Install and Uninstall Rubies

We can list all of the Rubies we have available to install with this command:

Once you’ve selected the Ruby you wish to install, you can issue this command:

If you ever wish to uninstall a version of Ruby, you can do this simply by typing:

How To Switch Rubies

Once you’ve installed a few versions of Ruby, you can list them with this command:

As you can see, RVM gives you a handy guide to tell you which are the current and the default Ruby versions. In this case, they are one and the same.

Switch to a different Ruby by typing:

Set a default Ruby to use by using the --default flag:

To switch to the default Ruby, type:

In order to use the version of Ruby installed on the system (not through RVM), you can specify:

How To Use Gemsets

One common way to distribute code in Ruby is to use a format called gems. Gems can be installed to extend the capabilities of the core Ruby distribution, and there are often gems that are required to be installed to get certain programs to function correctly.

In keeping with RVM’s mission of providing contained Ruby environments, it is also possible to install gems that are only associated with a single Ruby installation. RVM calls this functionality gemsets.

This means that you can have two different versions of the same gem, or you can make gems unaware of other gems on the system.

To see the available gemsets for the current Ruby, you can type:

If you have more than one Ruby version installed, you can see all of the gemsets by typing:

By default, you should have two gemsets configured:

  • default: The gemset that is applied if no other gemset is specified.
  • global: This gemset is inherited by every other gemset that is used. This set generally does not need to be selected because it will be included automatically. You should install shared gems here.

You can create another gemset easily. We will create a gemset called “test_project” to demonstrate how this works:

If you would rather copy a current gemset to a new gemset to run some tests, you can issue this command:

We can change the gemset we wish to use:

We can also change the Ruby version and gemset at one time. This is done giving the Ruby version, followed by the “@” character, and then specifying the gemset:

Now, we can install a rbtree gem by issuing this command:

We can now change to our default gemset and install an earlier version of the same gem:

We now have two separate versions of the rbtree gem installed and we can test them independently by switching the gemset that we are using.

If you’re confused about which gemset you’re currently working with, this command will print the current active gemset:

When you’ve finished using a gemset, perhaps because your testing is complete, you can get rid of it by issuing the following command:

How To Configure Defaults

RVM can be configured with defaults on a few different levels. RVM keeps its defaults in a file here:

You can see what RVM will use if you do not give it specific directions to do otherwise.

Note: You should not edit this file. It is overwritten when RVM is upgraded.

If you would like to override these settings, you can do so in a separate file at:

For ease of use, you can copy parameters out of the config/db file and place it in the user/db file to modify easily.

How To Automate Your Environment

You can create project-specific configurations that specify what Ruby version and gemset to use by creating an .rvmrc file inside of your project directory.

This eliminates the need to manually keep track of the ruby version you have active.

To create a project-specific environment, just create an .rvmrc file in the project’s top-level directory:

Inside, you just need to type “rvm”, followed by the Ruby version, the “@” symbol, and then the gemset:

That’s all you need. You may have to accept the configuration the first time you enter the directory.

Ensure that you have created the gemset and installed the Ruby version you are specifying, or else you will be prompted to install and create the necessary components whenever you switch into that directory.

You can also include any kind of project-specific RVM configuration within this same file.

How To Update RVM

When RVM comes out with a new version, you can update your installation easy from within the utility.

Simply issue the following command:

RVM will then fetch and install the newest version and then reload the environment. This is where your configurations would be wiped out if you placed them in config/db instead of user/db.

If you would like to upgrade to the latest available version (not necessarily stable), you can type:

Conclusion

As you can see, RVM is a versatile to

ol that can be used to manage projects and entire Ruby environments. You can use RVM to configure development conditions, server installations, and even to deploy your application.

If you work with Ruby on a regular basis, learning how to craft individualized Ruby environments with RVM is well worth it. It can help speed up your initial set up and can help you avoid making costly mistakes.

Leave a Reply

Your email address will not be published. Required fields are marked *

14 − one =