How to manage multiple Ruby versions with rbenv on Ubuntu
In this tutorial, we will install multiple Ruby versions on Ubuntu using rbenv.
Prerequisites
You need a workstation or a virtual server installed with Ubuntu OS 20.04 or a later version to complete this tutorial.
If you only have a Mac or a Windows workstation, you can quickly run an Ubuntu virtual server on it with Multipass.
Outline
- Install rbenv
- Install ruby-build plugin
- Install the primary Ruby version
- Install additional Ruby versions
- Switch between Ruby versions
- Upgrade ruby-build plugin
- Wrapping up
Install rbenv
rbenv is a Ruby version manager.
It lets you install and switch between different Ruby versions in your development workstation or production server.
rbenv
is available via package managers on several platforms. But, we can install rbenv manually from its GitHub repo.
Open a terminal and type in this command on your Ubuntu workstation to install rbenv
:
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
You can also use WSL, if you are using a Windows workstation.
Configure shell to load rbenv on startup:
$ ~/.rbenv/bin/rbenv init
Reload shell:
$ exec "$SHELL"
Install ruby-build plugin
rbenv
depends on the ruby-build plugin for installing Ruby from source.
Install the ruby-build
plugin:
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
Install the primary Ruby version
List the latest available Ruby versions:
$ rbenv install -l
ruby-build
installs a Ruby version by compiling from the source.
So we need to install the build dependencies.
$ sudo apt update
$ sudo apt install build-essential libssl-dev libreadline-dev zlib1g-dev autoconf bison libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
Install Ruby 3.3.4 which is the latest as of today:
$ rbenv install 3.3.4
This command will take several minutes to complete.
Set the installed Ruby version as the global Ruby version:
$ rbenv global 3.3.4
Install additional Ruby versions
List all known Ruby versions
$ rbenv install 3.3.3
Check the installed Ruby versions:
$ rbenv versions
3.3.3
* 3.3.4 (set by /home/ubuntu/.rbenv/version)
Switch between Ruby versions
Now, we have two Ruby versions installed in our system.
Select Ruby version 3.3.3
:
$ rbenv version 3.3.3
This will set the Ruby version to 3.3.3
for the current shell. If you log out and log in, the Ruby version will again be set to the global Ruby version.
You can set the Ruby version persistently for a working path by creating a file .ruby-version
.
Create directry my-blog
and switch to it:
$ mkdir my-blog && cd $_
Create the file .ruby-version
to select Ruby version 3.3.0
$ echo 3.3.3 > .ruby-version
Check the Ruby version:
$ ruby -v
ruby 3.3.3 (2024-06-12 revision f1c7b6f435) [x86_64-linux]
Create subdirectory logs
inside the my-blog
parent directory and check the Ruby version.
$ mkdir logs && cd $_
$ ruby -v
Still, the Ruby version would be 3.3.3
.
So the Ruby version set in .ruby-version
is applicable to all subdirectories inside the parent directory.
You can override the Ruby version by creating .ruby-version
file inside a subdirectory.
$ echo 3.3.4 > .ruby-version
Check the Ruby version now:
$ ruby -v
It will be set to 3.3.4
.
Upgrade ruby-build plugin
When a new Ruby version is released, you must upgrade the ruby-build plugin before you can install the newly released Ruby version.
Upgrade ruby-build plugin:
$ git -C "$(rbenv root)"/plugins/ruby-build pull
Wrapping up
In this tutorial, we used rbenv
to install and switch between multiple Ruby versions.
rbenv
is a simple tool that just modifies the PATH variable to set the current Ruby version. If you want more features and functions while managing multiple Ruby versions you can use rvm.