Last Updated: November 22, 2018
·
4.779K
· meddle0x53

Install rubies with Rbenv

Why not RVM? There are differences:

  • RVM is tool, designed to bring you listing of all available rubies in the net, downloading and compiling them, switching between them, gemsets.
  • Rbenv is tool for switching between ruby versions. You download and install the rubies, you decide what tool for dividing the gems to sets should be used.
  • RVM overrides the ‘cd’ command, in other words when you change directory, the command also ‘looks’ for .rvmrc file in it and executes it. This way you can heave per-project ruby versions.
  • Rbenv changes the executables in you downloaded gems so if you execute ‘rails’ it looks for .rbenv_version file in the current directory (or in its parents if there is no such file) and uses it to decide the version.
  • RVM is all in one.
  • Rbenv has a plugin system and can be set up to be on par with RVM‘s functionality.

If you ask me, which is better, I’ll sincerely answer you – it depends. The important thing is – you should use a tool that works for you, helps you and doesn’t get in your way. For now I prefer Rbenv, because I like the idea of upgrating/downgrating thing through plugins…

First of all if you have RVM, remove it the two tools don’t work well together:

rvm implode

Now let us install Rbenv:

git clone git://github.com/sstephenson/rbenv.git ~/.rbenv

This downloads Rbenv in your home’s .rbenv directory.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

This prepends the rbenv commands to your PATH. For Ubuntu and Mint use .profile, for zsh - .zshrc.

echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

This enables shims and auto-completion.Again for Ubuntu and Mint use .profile, for zsh - . You can read more about shims at the Rbenv's github.

exec $SHELL -l

This will restart your shell and the PATH changes will take effect.

We can list the available ruby versions with this:

rbenv versions

We don’s have rubies. So how to install one? I said that Rbenv can only switch between already installed rubies, so you can download ruby binary or source (and compile it) and put it in .rbenv/versions, for example ‘~/.rbenv/versions/1.9.3-p429′. But there is another way. I mentioned earlier, that Rbenv has a plugin system, so we will install a plugin called ruby-build which purpose is to download and install/uninstall rubies.

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone https://github.com/sstephenson/ruby-build.git

This is it. To install a plugin, all you have to do is to put it in ~/.rbenv/plugins. Now lets list the known rubies:

rbenv install --list

Cool. Now to install ruby 1.9.3, patch 429:

rbenv install 1.9.3-p429
rbenv rehash

The second line is important. When you download rubies or executable gems, you will have to run it to update the shim files. Now we have ruby 193-p429. Lets select it globally:

rbenv global 1.9.3-p429

This is the default ruby as of now. You can view it with ‘rbenv global’ or ‘rbenv version’.
Let us continue following my RVM post and to install ruby 2.0.0 with rails:

rbenv install 2.0.0-p195
rbenv rehash
rbenv global 2.0.0-p195
gem install rails
rbenv rehash

Everything is done, now if you run ‘rails -v’ you will have Rails 3.2.13 installed, but if you switch the global ruby with ‘rbenv global 1.9.3-p429′, I won’t have rails installed.

Gemsets… You don’t have them. If you want something similar, you will have to install a plugin.

mkdir -p ~/.rbenv/plugins
cd ~/.rbenv/plugins
git clone git://github.com/jamis/rbenv-gemset.git

So we have the gemset plugin. The idea is that you can include gemsets in a project. Create somewhere a directory for a rails 4 project. Go to this directory and execute:

echo rails4 > .rbenv-gemsets

Now for this folder the gemset is rails4, if you want to use this gemset in another project just navigate to its root folder and execute the above command. That’s it. So now if we are in a folder that is modified this way, we can install rails 4:

gem install rails --version 4.0.0 --no-ri --no-rdoc

Try it, if you are in the folder the rails version is 4, if you navigate out of it is 3.2. Now the same useful things as in the post for RVM:

  • To delete a gemset, just delete the .rbenv-gemsets folder.
  • To use the system ruby just delete the global version file - ~/.rbenv/version or run ‘rbenv global system’. If you have problems look at RBENV_VERSION environment variable, you may have to unset it.
  • To update Rbenv just navigate to ~/.rbenv and pull the newest version through git.
  • To delete Rbenv just delete the ~/.rbenv folder and clear your shell’s profile file from Rbenv specific stuff.

Useful Links:

  1. Rbenv site - http://rbenv.org
  2. Ruby Toolbox’s page about Ruby Version Managers - https://www.ruby-toolbox.com/categories/rubyversionmanagement
  3. Cool post about RVM and Rbenv - http://jonathan-jackson.net/rvm-and-rbenv
  4. Rbenv plugins - https://github.com/sstephenson/rbenv/wiki/Plugins