Why I use
chruby instead of RVM or
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:
Here’s why I use
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
completely separate tool) to install versions of Ruby. Or install them
by hand. Or whatever.
rbenv, on the other hand, do things
cd so that it can switch rubies “magically”. They
also install shims of commands like
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
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
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-install. This doesn’t install a version of Ruby, just
ruby-install. I do this with a Homebrew Brewfile.
ruby-installto install a version of Ruby, then install Bundler for that version of Ruby:
ruby-install ruby-2.5.0 ~/.rubies/ruby-2.5.0/bin/gem install bundler
chrubyin your shell (like this). This means you can run
chruby system(for example) to switch to that version of Ruby, wherever you are. Just running
chrubywill list the available versions (not including “system”).
- Set your default Ruby by calling
chruby ruby-2.5.0in your
~/.bash_profile. Without this, you’ll need to manually call
chrubyevery time you want to use something other than the system Ruby.
- You can also optionally set up
chrubyto auto-switch Ruby versions based on the presence of
.ruby-versionfiles in folders you
cdto by adding it to your
~/.bash_profile, too (like this). With this enabled, you can also set your default Ruby with a
~/.ruby-versioninstead of running
chrubyin your shell.
You can see 3 and 5 in my bash
profile. I haven’t done step 4 because I have a
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
is pretty clear and easy to follow.