Install Ruby on macos using rbenv

Thu, 18 Mar. 2021     Thomas Bendler     ~ 3 min to read

Although Ruby is part of macOS, its version is quite often not what you need in your development project. Recently I had the same problem and I searched for a solution to use multiple Ruby installations with different versions on my MacBook. The outcome was a combination of HomeBrew and rbenv which I would like to share in this blog post.

As mentioned, I use Homebrew as my package manager on macOS to install, manage and update a variety of applications on my MacBook. To use the package manager you need to install Homebrew first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you run macOS on an M1, you need to add the new root directory for Homebrew to your shell

# Apple M1 homebrew settings
eval $(/opt/homebrew/bin/brew shellenv)

If you run macOS on traditional Apple hardware, don’t forget to add /usr/local/bin and /usr/local/sbin to your PATH variable. The next step is to install rbenv:

brew install rbenv

rbenv is the tool that manages and controls the local ruby installations. To use rbenv I created a small snippet for my personal profile.d directory. To use this method, add the following snippet to the end of your .zshrc:

# Local custom snippets
for item in $(ls -1 ${HOME}/.profile.d/*.plugin.zsh); do
  [ -e "${item}" ] && source "${item}"
done

In the .profile.d directory I created a script to make use of rbenv:

# Custom plugin for zsh
#
# Ruby functions
#
# Author: Thomas Bendler <code@thbe.org>
# Date:   Thu Mar 18 16:37:23 CET 2021
#

### Path for rbenv installed ruby version ###
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

After restarting the terminal the new settings are in place and active. To list the available Ruby versions you can use:

rbenv install -l

This command shows you what stable Ruby versions are available. The installation of a specific Ruby version can be performed with:

rbenv install 2.7.2

Per default, rbenv uses the system Ruby installation which is the one that comes with macOS. To check which versions are available and which versions could be used, enter:

rbenv versions

To set a version as default, you need the global command. Although the command sounds like your setting something global, it is only to set it for your user ID:

rbenv global 2.7.2

The next time you open your terminal again your working on the Ruby version you’ve set globally:

rbenv versions
  system
* 2.7.2 (set by /Users/thbe/.rbenv/version)
  3.0.0


Share on: