An automated Ruby environment management is necessary for developers to easily work on multiple Ruby programs on the same machine.
In the Ruby community, there are two popular managers responsible for installing and maintaining multiple Ruby versions - RVM and Rbenv.
Table of contents
Let us learn all about Ruby Environment Management ...
...and go latest
Yes, Hix does this - so you don't have to.
If you are actively working on programs written in Ruby, at some point you are going to install multiple versions of the language on your system.
Ruby environment managers such as RVM and Rbenv allow you to do just that.
It is a generally accepted practice to include the .ruby-version file in the root of Ruby-written programs and applications, such as Ruby on Rails projects. The version file includes a specific Ruby version with which the program is supposed to work.
.ruby-version
2.6.5
Both RVM and Rbenv respect the aforementioned Ruby version, and upon navigating to the root of Ruby program that includes the file, they inform you that a particular Ruby version has to be installed in case it is missing on your system.
The short answer for developers is: it does not really matter, just pick one and be done with it - both RVM and Rbenv get the job done.
The long answer is: it depends.
One factor in favor of Rbenv over RVM is that it receives more love on Github. But let's get to the facts.
RVM pros over Rbenv:
Rbenv pros over RVM:
Rbenv main sell point is that its lightweight in comparison with RVM, while still providing just enough functionality for the Ruby development.
Now that we definitely have not decided which one to use leaving this decision to you, follow the installation instructions of the Ruby environment manager of your choice.
Depending on your operating system, follow the instructions below in order to install the RVM Ruby Environment Manager.
RVM has a dedicated package for Ubuntu. In order to use it, you are going to be able to add stuff to PPA.
sudo apt-get install software-properties-common sudo apt-add-repository -y ppa:rael-gc/rvm sudo apt-get update sudo apt-get install rvm
The next step is configuring the terminal to always perform login in order to always load the RVM.
At the terminal window:
The last step is to reboot your machine.
In order to install RVM, you are going to need the following packages: curl
and gpg2
. After confirming their presence, simply run
curl -sSL https://get.rvm.io | bash -s stable
Confirm your installation running the rvm install ruby
command.
As stated before, RVM has more features built-in that Rbenv does.
In order to browse all the RVM commands, run either rvm --help
or man rvm
in your CLI.
Basic command-line RVM usage can be boiled down to this set of commonly used commands:
rvm install <version>
installs given Ruby version on your system,rvm remove <version>
removes given Ruby version from your system,rvm list
lists all Ruby versions installed on your system,rvm list known
lists all available Ruby versions that you can install with rvm install
command,rvm current
displays currently used Ruby versionrvm use <version>
changes the currently used Ruby version to the given onervm use default <version>
sets the Ruby version that your system uses by defaultThe last two commands are useful to remember whenever you start the development of a new Ruby-written program. In most cases, it's best to use the latest stable version, which is always installable via the rvm install ruby --latest
command.
Based on your operating system, follow the instructions below in order to install the Rbenv Ruby Environment Manager.
Installing the Rbenv on macOS is really easy and can be done using Homebrew.
brew install rbenv rbenv init
After running the commands, close and open your terminal for changes to take effect.
The Ruby installation plugin is included by default, so you can now install any Ruby version using the rbenv install X.Y.Z
command.
In order to set up Rbenv on Debian based system, we are going to install all Ruby dependencies first.
sudo apt update sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev
For OSs other than Debian, the same might be done using other system package managers.
Next, we are going to clone the official Rbenv repository into the home directory and add its binary to $PATH
in order to use rbenv
command line utility.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
The next step is to add the following command to your shell configuration file in order to load Rbenv automatically when the CLI starts.
~/.zshrc or ~/.bashrc
eval "$(rbenv init -)"
The last step is installing Ruby installation Rbenv plugin by simply cloning its official Github repository into the correct path.
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
That's it, Rbenv is ready to use. You can install the latest stable Ruby version using the rbenv install -l
command.
In order to use Rbenv via CLI, you need to have the aforementioned ruby-build
command-line utility installed.
In order to browse all the Rbenv commands available, run the rbenv --help
method.
There are basic Rbenv commands that you are going to use at some point of Ruby development:
rbenv install <version>
installs given Ruby version,rbenv global <version>
sets the default system Ruby version,rbenv version
displays the currently used Ruby version of your system,rbenv install --list
option lists all available versions of Ruby.Whenever you start to develop the new Ruby program, it is worth using the latest stable Ruby version, which you can easily install using the rbenv install -l
command.
Ruby Environment Management is a must-have for every Ruby developer.
Ruby community provides two very popular Ruby environment managers, Rbenv and RVM. Both of them are pretty easy to install and get the job done.
Another option is to use chruby.
Hi Greg, very good point.
Writing up this article, I focused on the two the most popular choices, but you are obviously right,
chruby
is another valid alternative.I'll take a closer look into it and will probably update this article with a section dedicated to it - thank you!