⟵ Home

Installing and Maintaining Ruby on macOS

October 21, 2024 ∙ 2 minute read

For a while now I’ve been installing Ruby through Brew and everything is great… until a new Ruby version is out. Then, havoc ensues, installed gems cannot be found, and after getting tired of trying to fix that, the nuclear option is chosen: uninstall ruby, clean everything, and start over. I even had a completely insane portion of my .zshrc responsible for detecting Brew and setting paths to the latest Ruby installation in the system. A complete mess.

After fiddling with several options, here’s how I setup Ruby on my machines.

In case you are following this, make sure to get rid of rbenv, rvm, asdf of whatever you have installed, as this will get in the way. Also, you will need brew installed and configured on your system. Done? Awesome. Let’s continue.

The final solution to my problem is chruby and ruby-install. chruby can maintain the system environment variables, and ruby-install can compile Ruby for the target system from scratch. Compiling Ruby is not the fastest process in the world, but it really didn’t take long on Apple Silicon, so it’s fine.

That said, install chruby and ruby-install:

 λ | brew install chruby ruby-install

Then, source chruby on your shell’s RC:

autosource "/opt/homebrew/opt/chruby/share/chruby/chruby.sh"
autosource "/opt/homebrew/opt/chruby/share/chruby/auto.sh"

autosource is a function that sources files for me in case they exist. It’s convenient since what sets-up my machines is a bash script; see, I’m not a big fan of upgrading macOS, and everything I need is either on Git or iCloud, so a clean install is my preferred method of upgrading. Sourcing what is actually present is convenient for my necessities. Anyway, here’s its source in case you are wondering:

function autosource() {
  [ -f "$1" ] && source "$1";
}

Nothing fancy.

Finally, install latest available Ruby through ruby-install, and chruby into it:

 λ | ruby-install ruby
>>> Updating ruby versions ...
>>> Installing ruby 3.3.5 into /Users/vitosartori/.rubies/ruby-3.3.5 ...
>>> Installing dependencies for ruby 3.3.5 ...

# 8< snip

>>> Successfully installed ruby 3.3.5 into /Users/vitosartori/.rubies/ruby-3.3.5

 λ | chruby 3.3.5
 λ | ruby --version
 ruby 3.3.5 (2024-09-03 revision ef084cc8f4) [arm64-darwin24]

Another convenient addition to your shell’s RC file is to automatically chruby to the latest version installed (only if chruby is available)

command -v chruby 2>&1 >/dev/null && chruby 3.3.5

Way better.

Happy Rubying! ✨