When working in Ruby — or any language for that matter — using a version manager for jumping between installed versions is wise. Though I’ll point out several version managers, I also focus this article on two in particular: Frum and chruby.
Before we discuss any of the various Ruby version managers, I want to highlight basic gem setup that will help you no matter which Ruby version manager you pick. This article will also assume you are on macOS and using both Bash and Homebrew.
First, you’ll want to configure
.gemrc to avoid generating documentation in order to speed up
install time. For more information about the
.gemrc file and the
RubyGems environment, in general, check out the official
Next, you’ll want to populate
$HOME/.ruby-version with the latest version of Ruby.
Run the following to set all of this up:
printf "%s\n" "---" > "$HOME/.gemrc" printf "%s\n" "gem: --no-document" >> "$HOME/.gemrc" printf "%s\n" "3.0.0" > "$HOME/.ruby-version"
💡 I like to globally default to the latest version of Ruby via my
$HOME/.ruby-version file, while
my specific projects will have their own
.ruby-version file which can then override the global
version as necessary.
OK, now we are ready to talk about Ruby version managers.
To install and configure
frum, you’ll only need to run the following Bash code from your terminal:
brew install frum printf "%s\n" 'FRUM_DIR="$HOME/.cache/frum"' >> "$HOME/.bashrc" printf "%s\n" 'eval "$(frum init)"' >> "$HOME/.bashrc" exec $SHELL
Usage is straightforward and a breeze:
frum install --list # List available Ruby versions for install. frum versions # List currently installed Ruby versions. frum install 3.0.2 # Install a specific Ruby version. frum local 3.0.0 # Switch to specific version within current directory. frum uninstall 2.7.0 # Uninstall a specific version:
In case you are like me and don’t like to type a lot, the following aliases are a great way to speed up your workflow:
alias rbi="frum install" alias rbc="frum local" alias rbu="frum uninstall" alias rbv="frum versions"
Now I can do everything I was doing before, in the Usage section, but with fewer keystrokes:
rbv # List currently installed Ruby versions. rbi 3.0.2 # Install a particular Ruby version. rbc 3.0.0 # Switch to specific version within current directory. rbu 2.7.0 # Uninstall a specific version.
chruby is written in Bash and a tool I’ve used for years.
The documentation for
chruby is straightforward but I’ll walk you through how I’ve used it in case
my experience is helpful.
To setup and use
chruby, you’ll need Ruby Install
for installing various Ruby versions. Within your terminal, run the following Bash code:
brew install chruby brew install ruby-install ruby-install "ruby-3.0.0"
Ruby Install, by the way, is a specialized tool for installing different versions of Ruby. While
Frum has this functionality baked in, what’s nice about Ruby Install — or Frum — is you don’t have
to wait for any updates to
ruby-install in order to download and install a
Ruby version. As long as the Ruby core team has published a versioned download, you’re good to go!
Now that both
chruby and Ruby Install are on your machine, you only need to teach your shell how
to auto-switch Ruby versions when you change into a directory that has a
.ruby-version file. To do
that, you’ll want to add this to your
if [[ -r "$HOMEBREW_PREFIX/opt/chruby/share/chruby/chruby.sh" ]]; then source "$HOMEBREW_PREFIX/opt/chruby/share/chruby/chruby.sh" source "$HOMEBREW_PREFIX/opt/chruby/share/chruby/auto.sh" fi
💡 For further details on auto-switching, see the associated chruby documentation.
chruby is straightforward. First, for a list of all currently installed Ruby versions, type:
Then depending on your system, you should see a list like the following:
ruby-2.7.2 * ruby-3.0.0
Your current version of Ruby will have an asterisk. To switch to a different version, run the following:
# Long chruby ruby-2.7.2 # ...or... # Short chruby 2.7.2
And there you have it, that simple.
The following aliases are a great way to speed up your workflow:
alias rb="chruby" alias rbi="ruby-install"
Now I can list Ruby versions, change to a new Ruby version, or install a new version of Ruby as follows:
# List Ruby versions. rb # Switch to a specific Ruby version. rb 3.0.0 # Install a specific Ruby version. rbi ruby-3.0.0
chruby not be your cup of tea, here are a few other version managers to be aware of:
asdf - This program aims to be a version manager for any language. For Ruby, that means installing the Ruby plugin via:
asdf plugin-add ruby. Unfortunately,
asdfsuffers the same problems as RVM in that it tries to do too much. I’d rather my tools be specialized to do one thing really well.
rbenv - Aiming to be a lighter weight version of RVM,
rbenvsucceeds for the most part. It does require the use of shims to jump between versions, which does require extra maintenance.
RVM - One of the first version managers to hit the scene. RVM is way more complicated than it needs to be in terms of install, setup, and even gem management. I would avoid using this if possible.
Should you need further examples of setting up a version manager, configuring your machine, or machine automation in general, make sure to check out my Dotfiles and/or macOS Configuration projects for a deeper dive.
I hope this article has convinced you that a lighter and simpler version manager can improve your workflow and that you really can type less and do more. Enjoy!