I was a long time user of RVM for installing and switching
Rubies. It made my life pretty easy even as I listened to others struggle with
it. I was a little uncomfortable putting other things like project-specific
environment variables in my
.rvmrc files. It seemed dirty, but it worked and I
rolled with it for many years.
Eventually, I tried something different. I’d been sold on the idea of using multiple, simpler tools together. Here’s the result.
Installing a Ruby:
brew install ruby-install
ruby-install ruby-2.2 got me the Ruby I wanted. “Too simple,” I
thought. “Now I won’t be able to say I want to use that version.”
Use a Ruby:
brew install chruby
chruby spat out a list of installed Rubies.
1 2 3 4 5
“Hunh,” thinks I.
chruby will by default find Rubies installed in the default
directory used by
“OK. But now I have to run that for each project to get the Ruby the project needs.”
brew install direnv
Then create an
.envrc file in a project root directory with the appropriate
commands for the project. It can be made pretty convenient by adding the
following function to a
.direnvrc file in your HOME directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Thanks to Steve Tooke for most of the above.
So one of my projects is a Rails app. It’s
.envrc looks like:
1 2 3 4 5
This sets some environment variables suitable for testing and switches my Ruby
environment around. I get Ruby version 2.2.4 and GEM_HOME, GEM_PATH, and
GEM_ROOT adjusted to bring in global gems from the 2.2.4 install and
project-specific gems from
<project-root>/.direnv/ruby thanks to direnv’s
layout ruby feature.
I recommend adding
.direnv to your project’s
Bonus Round – Chef DK!
I use Chef and the recommended way to install Chef on a development workstation
is to install the Chef DK. Chef DK
includes its own embedded Ruby and gem environment and
chruby knows nothing
about it. We can use
direnv to twiddle the environment for Chef projects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Thanks to Seth Chisamore for the Chef DK function.
.envrc files in Chef projects have a line like this:
It’s working for now and I can happily switch between Chef DK projects and projects needing their own Rubies (e.g. Rails sites).