Using chruby as the default Ruby version manager

When it comes to ruby version management tools the users have three options:

  • RVM which is the most established, but also the most intrusive in terms of shell modifications.
  • rbenv which is lower impact.
  • chruby which claims to be even lighter than rbenv.

After sticking to RVM for almost 5 years, I decided to give chruby a try and see how does it work. Personally, the problem that I have with RVM is that it mangles the shell environment as much. Most of the times, this is not a problem on servers but for my local computer I prefer something lighter and less intrusive.

What is chruby?

chruby vs. RVM

How to install

➜ brew update 
➜ brew install ruby-install -HEAD # ruby-install is needed to facilitate installing additional version of Rubies.

To make sure that ruby-install is installed successfully:

➜ ruby-install -V 
ruby-install: 0.5.1

If you run ruby-install alone with no argument you can see the list of latest ruby versions:

➜ ruby-install 
Latest ruby versions:
ruby:
2.0.0-p645
2.1.6
2.2.2
jruby:
1.7.20
rbx:
2.2.10
2.3.0
2.4.1
2.5.3
maglev:
1.0.0
mruby:
1.1.0

As an example here, we want to have both ruby 2.2.2 and 2.0.0-p645 installed on our system:

➜ ruby-install ruby 2.2.2 
➜ ruby-install ruby 2.0.0-p645

That will install ruby 2.2.2 and 2.0.0 into /opt/rubies/ (for root user) or ~/.rubies for other users (because I didn’t use sudo when installing here it will install it in ~/.rubies.

Now we can install chruby and exploit its full power:

➜ brew install chruby

After that we need to add the following line to the ~/.bashrc or ~/.zshrc file:

source /usr/local/opt/chruby/share/chruby/chruby.sh

By default chruby will search for Rubies installed into /opt/rubies/ or ~/.rubies/. Because ruby-install already installed ruby 2.2.2 andruby 2.0.0 to ~/.rubies directory, chruby is able to find the vesions of ruby.

➜ chruby 
ruby-2.0.0-p645
ruby-2.2.2

chruby manages to find two already installed rubies. To use 2.2.2:

➜ chruby ruby-2.2.2
➜ chruby
ruby-2.0.0-p645
* ruby-2.2.2

There is an asterisk next to version 2.2; it means that this version is in use right now. You can verify that with asking the version of current ruby:

➜ ruby -v 
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-darwin14]

Just remember, after installing new Rubies, you must restart the shell before chruby can recognize them.

Up to this point, you have required knowledge to use chruby, if you want to know more, keep reading; otherwise skip it.

Auto-Switching

source /usr/local/share/chruby/chruby.sh 
source /usr/local/share/chruby/auto.sh

According to documentations, chruby will check the current and parent directories for a .ruby-version file. Here is a scenario: you want to automatically switch to a specific version of ruby upon cd’ing into that directory. The only thing you need to do is to create a file called .ruby-version (pay attention to the dot) in the directory you want and put the appropriate version in it.

➜ echo "ruby-2.0.0-p645" > ~/.ruby-version 
➜ source ~/.zshrc # or ~/.bashrc

This makes sure upon cd’ing to ~ directory the ruby version the shell use will be set to version 2.0.0.

RVM with chruby

Considering that you have RVM installed, you can integrated RVM with chruby.

Integration

source ~/.rvm/scripts/extras/chruby.sh

After doing so, your .zshrc (.bashrc) should contain the following lines, given you followed instructions in this post.

source /usr/local/opt/chruby/share/chruby/chruby.sh 
source /usr/local/share/chruby/auto.sh
source ~/.rvm/scripts/extras/chruby.sh

Don’t forget to do this:

source ~/.zshrc # or source ~/.bashrc

How to use

mrvm list known 
mrvm install 1.9.3
mrvm remove 1.8.7
mrvm upgrade 1.9.3 2.0.0

According to RVM’s documentation, the differences between rvm and mrvm are:

  • “mrvm use” will not work. To use and switch between different versions of ruby you should use chruby, for example:
➜ chruby 2.2.2
  • every call to mrvm will refresh RUBIES environment variable to include only RVM installed rubies.

That’s it. More things you can do using chruby are mentioned on its github page. In the meantime, I will keep playing with it to have a better understanding of it.

Software Engineer living in the Bay Area

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store