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:
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:
- 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)
- ruby-build has to be installed as a plugin to rbenv.
- In case you need to learn about the tools in detail, here are the links for rbenv and ruby-build.
- 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.
Linux still does not know where rbenv is. Add it to your path by running:
To initialize rbenv so that it can help you with changing rubies when you change directories, run this:
This should tell you something like this:
So run this:
By this point rbenv should be installed. When you run rbenv
on the command line, you should get something like this:
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.
{: .notice—warning}
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:
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:
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.
{: .notice—warning}