Every ruby developer will probably use some kind of ruby version manager. As far as I know, it all started with rvm. Rvm has a ton of features, but I never really was fan. Most of the features that are available where not necessary for me. So since I wanted a minimal tool to manage multiple ruby versions, I quickly took a look at rbenv. Rbenv is aimed to be simpler, and focusses mainly on the task at.
But then came chruby, the newest and third tool by my count.
So in short, rvm is by far the most complex of the three tools. Overwriting cd (to allow auto-switching) is a common concern for a lot of people. Rvm doesn’t only manage versions, it also installs them, which strikes as a breach of Unix.
One of the “big” features to use rvm is its gemsets, which isolates sets of gems into groups and thus prevent gem-hell. Since bundler came onto play, this problem no longer exists.
I’ve been a long time rbenv user, but still there where some things that bugged me. The biggest downside for me is the use of shims. A shim is basically a library that intercepts a certain call and changes the parameters. So in the case of rbenv, when calling any command ( ruby, irb, gem ) or even gem binaries, it gets redirected to the correct binary that is active at that time. That is why you need to rehash after every install you make, to make sure new commands get shimmed.
Basically, what a manager needs to do is modify some environment variables so that the correct binary and set of libraries are loaded. And now that’s what chruby does!
Taking the main points from the chruby repository README, changing rubies with chruby will:
- Updates $PATH
- Correctly sets $GEM_HOME and $GEM_PATH
- Additionally set s $RUBY_ROOT, $RUBY_ENGINE, $RUBY_VERSION and $GEM_ROOT
- Call hash -r to clear the command-lookup hash-table
No need for shims and no unwanted features. Chruby is a small and readable shell, which weighs less then 90 LOC.
Chruby can also do auto-switching. To make use of auto-switching, you only need to source and additional script. Once this is enabled, you will automatically switch rubies when you enter a project directory containing a .ruby-version file. All of this is achieved via a pre-prompt command and not by overwriting the cd command.
You can simply install chruby on OS X using the brew package manager:
$ brew install chruby
Next, simply add the following lines to your shell profile file. I’m using bash, so mine looks like this:
if [[ -e /usr/local/share/chruby ]]; then source /usr/local/share/chruby/chruby.sh source /usr/local/share/chruby/auto.sh fi
Since chruby is only a version manager, we still need to install multiple ruby versions. When I still used rbenv, I used ruby-build for the job. Ruby-build is easy to use and integrates well with rbenv. It’s that good, that you can even use it with chruby.
Though the author of chruby came with its own version: ruby-install . Though both ruby-build en ruby-install have the same end-goal, ruby-install seems again more lightweight, simpler and more powerful in usage. So this time, I went with ruby-install.
A nice bonus with ruby-install is that it supports multiple package managers for installing build dependencies ( apt, yum, pacman, macports and homebrew).
Installing it is again dead simple using homebrew:
$ brew install ruby-install
Now installing a ruby version is plain simple. With the ruby-install command, you can list all known ruby versions:
$ ruby-install Known ruby versions: ruby: 1: 1.9.3-p484 1.9: 1.9.3-p484 1.9.1: 1.9.1-p431
You can install them by simply typing:
$ ruby-install ruby 2.1.0
Check out the ruby-install readme for all the available options.
Note: unfortunately, IntelliJ doesn’t play along with Chruby. So be so kind and vote to implement😉