Why I use chruby
instead of RVM or rbenv
When you’re working with multiple Ruby projects, you often end up
needing to run different versions of Ruby. Version managers help
make this easier, and there are three well-known choices for Ruby:
RVM,
rbenv
, and
chruby
.
Here’s why I use chruby
.
chruby
is simpler and easier to understand
chruby
is essentially a very
small shell script that sets a few environment variables (mostly
$PATH
) to point at a given install of Ruby. It also has a separate,
optional, script to auto-switch ruby versions when changing
directories. Both of the scripts are short enough and clear enough that
I can read and follow what they’re doing. It doesn’t do anything more
than that, itself; you use
ruby-install
(a
completely separate tool) to install versions of Ruby. Or install them
by hand. Or whatever.
RVM and
rbenv
, on the other hand, do things
like overriding cd
so that it can switch rubies “magically”. They
also install shims of commands like gem
, ruby
, and others that
dynamically look up the available Rubies. These sometimes have bugs, or
aren’t entirely transparent. RVM does a bunch of this to enable
features like Gemsets, but most of that
functionality has been surpassed by Bundler.
rbenv
is a bit better, but the shims still require “rehashing”
(updating) whenever you install a new gem that offers executables.
In short: I’ve had weird issues in the past that ended up being down to
RVM or rbenv
components misbehaving. With chruby
, there’s nothing
actually there other than a script that tweaks environment variables,
so issues are pretty much nonexistent.
What you need to do to use chruby
Getting started with chruby
is pretty easy and, because it’s designed
to be self-contained and loosely coupled, it’s easy to back out if
change your mind. Here’s how to install it and set it up with your first
Ruby version:
- Install
chruby
andruby-install
. This doesn’t install a version of Ruby, justchruby
itself andruby-install
. I do this with a Homebrew Brewfile. - Use
ruby-install
to install a version of Ruby, then install Bundler for that version of Ruby:1 2
ruby-install ruby-2.5.0 ~/.rubies/ruby-2.5.0/bin/gem install bundler
- Enable
chruby
in your shell (like this). This means you can runchruby ruby-2.5.0
orchruby system
(for example) to switch to that version of Ruby, wherever you are. Just runningchruby
will list the available versions (not including “system”). - Set your default Ruby by calling
chruby ruby-2.5.0
in your~/.bash_profile
. Without this, you’ll need to manually callchruby
every time you want to use something other than the system Ruby. - You can also optionally set up
chruby
to auto-switch Ruby versions based on the presence of.ruby-version
files in folders youcd
to by adding it to your~/.bash_profile
, too (like this). With this enabled, you can also set your default Ruby with a~/.ruby-version
instead of runningchruby
in your shell.
You can see 3 and 5 in my bash
profile. I haven’t done step 4 because I have a .ruby-version
in my home directory.
chruby
is a simple tool that does one thing well
Like all good Unix tools,
chruby
does one thing well and works with other programs to achieve
its goal. It doesn’t try to take over, or insert itself between its
user and Ruby, it just helps switch between versions of the language.
I hope this helps you get started with chruby
. If you want to know
more, chruby
’s documentation
is pretty clear and easy to follow.