Installing rbenv

5 minute read

Ruby version managers are tools which allow you to switch ruby versions. If you have never used one of them and have always done sudo apt install ruby and don’t know the advantages of using one, read on.

What are ruby version managers

We already touched upon this topic when talking about the tool selection. We will elaborate about it here.

Ruby is a well-known programming language under active development. Every year (or so), a new version of Ruby is released with new features, better speed and so on. Older versions of the language (and some features in those versions) are deprecated (set to be discontinued in the upcoming versions) and bugs are fixed.

However, there are times when you need older versions of Ruby. For example, you have an older Ruby on Rails app which requires you to have ruby 2.1 while your package manager (in our case apt) supplies you with Ruby 2.5 while at the same time another application requires you to have ruby 2.5. How do you solve that? Ruby version managers are an answer to that.

What do they do?

Ruby version managers (like rvm, rbenv and chruby) allow you to install multiple versions of Ruby without having to change the global (also called system) ruby version. So you can install any number of ruby versions without having to change the global one (the one you installed by running the sudo apt install ruby command).

Once you have the ruby version manager, you can use it to install multiple rubies (multiple ruby versions are sometimes just called rubies) and switch to them per directory so that when you are in one directory, you will use one ruby version but when you change the directories, it will automatically change the ruby version. In case of rbenv, you need to create a file named .ruby-version in a directory containing the ruby version and when you switch (cd) to that directory, the version manager will automatically change the ruby version according to what is mentioned in the file.

For example, assuming you have rbenv installed and have two versions (say version 2.4.5 and 2.5.2) installed via rbenv while system ruby is at version 2.5.1. Then you can do something like this:

$ cd 
$ mkdir tmp
$ cd tmp
~/tmp$ mkdir firstdir
~/tmp$ mkdir seconddir
~/tmp$ echo "2.4.5" > firstdir/.ruby-version
~/tmp$ echo "2.5.2" > seconddir/.ruby-version
~/tmp$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
~/tmp$ cd firstdir/
~/tmp/firstdir$ ruby -v
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-linux]
~/tmp/firstdir$ cd ../seconddir/
~/tmp/seconddir$ ruby -v
ruby 2.5.2p104 (2018-10-18 revision 65133) [x86_64-linux]

You also do not have to worry about the Gems (ruby packages) installed against a particular version. rbenv manages the isolation of Gems for you.

An awkward catch

This might sound weird but it’s true: to install rbenv properly and use it to install rubies you system must already have ruby installed.

So please, go ahead and do a sudo apt install ruby before you proceed.

Installing rbenv

Before proceeding with the actual installation, remember these points:

  1. rbenv in itself does not include the ability to install ruby versions. It only changes ruby version per directory. For installing rubies, you need to install the ruby-build tool (which is part of the rbenv project)
  2. ruby-build has to be installed as a plugin to rbenv.
  3. In case you need to learn about the tools in detail, here are the links for rbenv and ruby-build.
  4. The installation procedure for both tools (and a lot of other help) is available in the README files for the projects. Refer to those if things don’t work for you.

Installing rbenv

Run the following command to clone rbenv repo into .rbenv directory in your home directory.

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Linux still does not know where rbenv is. Add it to your path by running:

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

To initialize rbenv so that it can help you with changing rubies when you change directories, run this:

~/.rbenv/bin/rbenv init

This should tell you something like this:

# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

So run this:

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

By this point rbenv should be installed. When you run rbenv on the command line, you should get something like this:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

If you get a warning saying that rbenv is not installed and can be installed with a sudo apt install rbenv. Do not run that command. Instead just run source ~/.bash_profile. That will rerun the ~/.bash_profile script and get rbenv in your path. You should be able to run rbenv after that without trouble.

Do notice that rbenv does not give an option to install or uninstall rubies yet. For this we need to install ruby-build.

Installing ruby-build

We need to add the ruby-build package as a rbenv plugin so that we can type rbenv install <ruby version> to install rubies. All you need to do is to create the plugins directory and checkout the git repo for ruby-build in the plugins directory. Run the following:

$ mkdir -p "$(rbenv root)"/plugins
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

Testing if rbenv and ruby-build have been installed

Run rbenv without any arguments on the terminal should now show the install and uninstall commands being available. Something like this:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using ruby-build
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

That’s it, you have your Ruby version manager installed!

NOTE: Remember that you can run rbenv anytime to see the list of available options. Pay attention to the second-last line of the output - you can also get help on specific commands and their format by running rbenv help <command>, e.g. running rbenv help install will tell you what options the install command accepts.

Updated:

Leave a comment