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?
It is a lightweight Ruby version management tool. chruby’s code is just 90 lines and it has perfect test coverage. It is an interesting alternative to RVM. chruby can be utilized to pick a Ruby version for your application and guarantee that your development enviroment matches production.
chruby vs. RVM
chruby is light. It doesn’t include any mechanism to install Ruby implementations like RVM does. The sole job is to manage Ruby enviroments and it allows you to quickly switch between Ruby implementations. If you want to install rubies you can do it manually or use ruby-install script.
How to install
Here we will use homebrew to install packages.
➜ 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
If you want chruby to auto-switch the current version of Ruby when you cd between your different projects, simply load auto.sh in ~/.bashrc or ~/.zshrc:
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
One of basic RVM functionalities is to install different versions of ruby. On the other hand, chruby does not provide this function (in this post, we used it along with ruby-install), although users can use chruby for switching rubies and RVM for installing rubies like the way we did it with ruby-install.
Considering that you have RVM installed, you can integrated RVM with chruby.
Integration
Put the following line in your .zshrc or .bashrc.
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
To avoid confusion with full version of RVM the integration is called mrvm (mini RVM). You can use it the same way as you would use rvm:
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.