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!
rbenv
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 -)"
chruby
Same process as rbenv, but just a little different.
$ git clone git://github.com/postmodern/chruby.git ~/.chruby
$ cd ~/.chruby
$ sudo make install
OR
$ 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
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.
You can see a full list of available plugins here.
rbenv-gem-rehash
https://github.com/sstephenson/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
https://github.com/rkh/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-aliases
https://github.com/tpope/rbenv-aliases
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
.
rbenv-each
https://github.com/chriseppstein/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
https://github.com/dcarley/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.
Frequently Asked Questions (FAQs) about Chruby and Rbenv Tips & Tricks
How do I install Chruby on my system?
To install Chruby on your system, you first need to download it from the official GitHub repository. Once downloaded, extract the files and run the scripts/install script. This will install Chruby into /usr/local. You can also install it into a custom directory by running scripts/install -p custom/path. After installation, add the following lines to your shell profile: source /usr/local/share/chruby/chruby.sh and source /usr/local/share/chruby/auto.sh. This will enable Chruby every time you start a new shell session.
How do I switch between different Ruby versions using Chruby?
Chruby allows you to easily switch between different Ruby versions. To do this, simply use the chruby command followed by the version number. For example, to switch to Ruby 2.6.3, you would type chruby 2.6.3. If you want to make a particular Ruby version the default, you can add the chruby command to your shell profile.
What is Rbenv and how does it differ from Chruby?
Rbenv is another Ruby version management tool. It differs from Chruby in a few ways. Firstly, Rbenv uses shims to intercept Ruby commands, while Chruby changes the PATH variable. Secondly, Rbenv requires a separate plugin to install Ruby versions, while Chruby does not. Lastly, Rbenv has a more complex setup process compared to Chruby.
How do I install Rbenv on my system?
To install Rbenv, you can use the Homebrew package manager on macOS or git on Linux. After installation, you need to add the following line to your shell profile: eval “$(rbenv init -)”. This will initialize Rbenv every time you start a new shell session. To install a new Ruby version, you can use the rbenv install command followed by the version number.
How do I switch between different Ruby versions using Rbenv?
To switch between different Ruby versions using Rbenv, you can use the rbenv global command followed by the version number. For example, to switch to Ruby 2.6.3, you would type rbenv global 2.6.3. If you want to use a different Ruby version for a specific project, you can use the rbenv local command in the project directory.
Can I use both Chruby and Rbenv on the same system?
Yes, you can use both Chruby and Rbenv on the same system. However, it’s generally not recommended as it can lead to confusion and potential conflicts. It’s best to choose one tool and stick with it.
How do I uninstall Chruby or Rbenv from my system?
To uninstall Chruby, you can simply remove the lines you added to your shell profile and delete the Chruby directory. To uninstall Rbenv, you can remove the lines from your shell profile, delete the Rbenv directory, and remove any shims and versions you installed.
How do I update Chruby or Rbenv to the latest version?
To update Chruby, you can download the latest version from the GitHub repository and run the install script again. To update Rbenv, you can use the git pull command in the Rbenv directory.
Can I use Chruby or Rbenv with other Ruby tools like Bundler or Rails?
Yes, you can use Chruby or Rbenv with other Ruby tools like Bundler or Rails. Both tools are designed to work seamlessly with the Ruby ecosystem.
What are some common issues I might encounter when using Chruby or Rbenv and how can I troubleshoot them?
Some common issues include not being able to switch Ruby versions, getting errors when installing a new Ruby version, or commands not being recognized. To troubleshoot these issues, you can check the Chruby or Rbenv documentation, search for similar issues on Stack Overflow, or ask for help in the Ruby community.
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.