Install Ruby: A Step-By-Step Guide

David Morales David Morales
/
Miners extracting vivid red rubies from rocky walls in a dimly lit underground mine, surrounded by tools and wooden beams.

Ruby is widely used for web development thanks to the Ruby on Rails framework. This guide covers my recommended method to install Ruby on different operating systems, and how to maintain different versions effectively.

Installing Ruby

If you use macOS, the instructions below apply to both Intel and Apple Silicon (M1 and up) Macs.

Install rbenv

rbenv is a Ruby version manager that lets you easily switch among multiple Ruby versions and ensure each project uses the appropriate one.

You can install it through Homebrew or by cloning the Git repository. I recommend you to visit the repository website where you can follow the instructions.

Install ruby-build

ruby-build is used to download, compile and install Ruby automatically. It is not included in rbenv by default, so you must install it now to continue.

Visit their repository website to follow the instructions.

Installing a Ruby version

The first thing to consider is which version of Ruby you want to install.

If your project has a .ruby-version file, open it and there you will find the version you need.

If you want to install the latest and you don’t know which one it is, visit the official Ruby Releases page to see which one is the latest.

Another way to find out is by listing the latest stable versions:

Terminal window
rbenv install -l

Now install a version. If for example you wanted to install version 3.3.6, you would do it this way:

Terminal window
rbenv install 3.3.6

rbenv will use ruby-buld to install this version of Ruby from source, so it will take a while.

That’s it, your projects that include a .ruby-version file with this version set can now access to it.

If you need more versions you can re-run rbenv install specifying the one you are interested in.

If you want to see a list of the versions you have installed, you can do it with this command:

Terminal window
rbenv versions

The Ruby installation includes the gem command to manage your libraries. It is a package manager that communicates with the RubyGems repository.

Don’t forget to install bundler globally to manage the dependencies of your projects:

Terminal window
gem install bundler

Setting a default version

I recommend you to set one version as the default one, which will be the one to run outside of project directories. This is usually done with the latest one. For example:

Terminal window
rbenv global 3.3.6

Now when you call the ruby command from anywhere (except directories with a .ruby-version file) this version will be executed.

Running Ruby

Now you can place yourself in some directory and check which version would respond:

Terminal window
rbenv version

You can also run ruby -v to see more data, such as build information or the architecture.

Installing Ruby on Windows

Although Ruby has native support on Windows, I recommend you to enable WSL (Windows Subsystem for Linux), so you can use the embedded Linux alongside your Windows apps.

It is as easy as opening a terminal in administrator mode and running the following:

Terminal window
wsl --install

Reboot to finish the installation. Windows will install Ubuntu by default, but if you prefer another system you can have a look at the official guide by Microsoft.

Now that you have WSL ready, you can follow the instructions above to install rbenv and ruby-build, and finally install Ruby.

Updating your Ruby tools

Updating rbenv and ruby-build

When a new version of Ruby is released, you have to update rbenv and ruby-build first to support its installation.

If you have installed rbenv with a package manager (such as Homebrew on macOS or apt on Ubuntu Linux), just updating the packages will be enough to keep rbenv and ruby-build up to date.

On macOS:

Terminal window
brew update && brew upgrade

On Ubuntu:

Terminal window
sudo apt update && sudo apt upgrade

Updating Ruby

With the tools already updated, you can now start the installation of the new version:

Terminal window
rbenv install VERSION

Where VERSION is the new version you want to install.

Note that when you install gems, they are saved in the directory of the corresponding Ruby version, so you will have to reinstall them for the new Ruby version you just installed.

First reinstall bundler globally:

Terminal window
gem install bundler

Then, for each project, update the .ruby-version file with the version you just installed. Then run the following command:

Terminal window
bundle install

This will reinstall the gems and you will have the project updated to the new Ruby version.

Don’t forget to remove the previous Ruby version. You can do it with this command:

Terminal window
rbenv uninstall VERSION

Where VERSION is the previous version.

Updating RubyGems

I recommend updating the gems manager with the latest changes from the RubyGems repository:

Terminal window
gem update --system

There is no need to upgrade Ruby for this, as it is version independent.