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.
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!
First, download rbenv.
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
In your shell configuration file (you should know what yours is:
~\.(bash|zsh)(rc|_profile)), append the following:
(You can skip this step if you installed through Homebrew.)
# rbenv export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
Same process as rbenv, but just a little different.
$ git clone git://github.com/postmodern/chruby.git ~/.chruby $ cd ~/.chruby $ sudo make install
$ wget -O chruby-0.3.8.tar.gz https://github.com/postmodern/chruby/archive/v0.3.8.tar.gz $ tar -xzvf chruby-0.3.8.tar.gz $ cd chruby-0.3.8/ $ sudo make install
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
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:
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 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
$ 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.
You can see a full list of available plugins here.
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.
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.
You may notice that we solved this problem earlier with a simple symlink, but this plugin does the same thing. It’s simply
rbenv alias VERSION\_ALIAS REAL\_VERSION.
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.
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.
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.
The Principles of Beautiful Web Design, 4th Edition
Docker for Web Developers
HTML5 Games: Novice to Ninja