Chruby and Rbenv Tips and Tricks

Rbenv and chruby are the cool kids on the block now. Obviously, we want the most we can get out of our favorite tools. So this article isn’t about which one to like more, nor is it an introduction to either version manager. Instead, we’re going to cover some advanced rbenv and chruby usage to maximize your productivity.

Setup

If you have no experience with rbenv or chruby you should read their README files first, but here’s the quick start:

A note on Homebrew installation. I love Homebrew, but it would be redundant to keep saying brew install whatever for everything, because I’ll also be showing the manual (git) method. In other words, if you come across anything and don’t want to install it through git, just brew install it!

I like the former over the latter because it’s easy to remember and concise. Take your pick.

Append this to your shell configuration:

# chruby
source /usr/local/share/chruby/chruby.sh

Reload the shell and you’re good to go! Try running rbenv or chruby to see if it worked. If you don’t have any ruby versions already installed, that’s OK. Take a look at ruby-install or ruby-build.

Scenarios

Now that you’re all setup, I’m going to run through several realistic scenarios and how to solve them using rbenv and chruby. Let’s go!

I want my Ruby version to change automatically.

Rbenv does this automatically. No plugins needed.

Chruby does this as well, but you have to append source /usr/local/share/chruby/auto.sh to your shell configuration. That’s it.

I keep my Ruby versions somewhere else.

This can be useful when switching from rbenv to chruby or vice versa. Replace the given directory with whatever directory you happen to store your rubies.

Rbenv needs a little bit of a hack, but it’s just a simple symlink.

$ ln -s /opt/rubies/2.1.5 ~/.rbenv/versions/2.1.5

Chruby has an array ($RUBIES) just for this purpose. Append this to your shell configuration:

RUBIES+=(~/.rbenv/versions/*)

I want a default global Ruby version.

Both allow for a global ~/.ruby-version file which you could put in your dotfiles. (e.g. echo "2.1.5" > ~/.ruby-version) This assumes that you have chruby auto version switching enabled, though.

Rbenv has rbenv global 2.1.5.

Chruby allows you to call itself in your shell configuration like so:

# below other chruby config
chruby 2.1.5

My project uses a different Ruby version name.

Occasionally a project will have a strange prefix (like RVM’s ruby-) before the actual version. If chruby or rbenv can’t seem to find the given version, it’s a simple symlink. In this example, our project has a Ruby version file that specifies ruby-2.1.5, but our version is named 2.1.5.

$ cd ~/.
$ ln -s 2.1.5 ruby-2.1.5

Cool Rbenv Plugins

A great thing about rbenv is the lack of bloating features like RVM. However, sometimes we want something extra. Let’s take a look at a few plugins for rbenv. To install these, you need to check each plugin’s GitHub repository for an install section. It’s usually just a simple git clone, but you may also use Homebrew.

rbenv-gem-rehash

Rbenv uses executable shims for loading Ruby and its gems. Running rbenv rehash refreshes these shims. This, after every gem installation, run rbenv rehash. Lucky for us, this plugin automatically rehashes the shims. There’s no configuration, just install it and you’ll never have to rehash rbenv again.

rbenv-use

So, you’re still partial to a few RVM commands, eh? One of these commands is rvm use VERSION. This plugin adds the rbenv use VERSION command to ease this transition. However, you should not rely on this plugin for your projects. The .ruby-version file should be setting your version in most of your environments.

rbenv-each

This plugin runs a command on each version of Ruby on your system. The most likely situation I could see for this is testing for multiple versions. The syntax is similar to that of bundle exec, just run rbenv each <a command> to run the command against each Ruby.

rbenv-sudo

Sometimes it’s necessary to run Ruby as a root user, but it’s usually a very painful and messy process. Luckily this plugin does just that! Just run rbenv sudo <a command> to run the command as a root Ruby process.

In Conclusion

Rbenv and chruby are both excellent Ruby version managers. I have used each of them myself with great results. Each is highly extensible and easy to use. The scenarios above were meant to mimic real life, but if you find yourself in a scenario that wasn’t described above, feel free to leave a comment.

Jesse Herrick is an avid Ruby developer who specializes in web development. He is a back-end developer at Littlelines and loves programming. You can read his personal blog at: https://jesse.codes.

Free Guide:

7 Habits of Successful CTOs

"What makes a great CTO?" Engineering skills? Business savvy? An innate tendency to channel a mythical creature (ahem, unicorn)? All of the above? Discover the top traits of the most successful CTOs in this free guide.