Installing rbenv on Zsh (on MacOS)

Recently, I updated my Mac to the latest beta of macOS. It’s not a polished product yet, but overall it’s been a fairly enjoyable operating system.

One of the changes that will impact you as a developer is that Apple switched the default shell from Bash to Zsh (Z shell). You can, of course, change it back to Bash, but I don’t mind Zsh (or Fish) so I decided to keep it.

I needed to install htmlbeautifier, a gem used by some prettifier extensions within Visual Studio Code. If you try it with the default system installation of Ruby, you’ll get a permission error:

$ gem install htmlbeautifier
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

At this point, you might be tempted to simply sudo it, but that’s not a smart approach. Instead, I like to have multiple versions through an environment management system such as rbenv or asdf.

For this particular machine, I picked rbenv. Unfortunately, following the usual setup instructions didn’t really work, so I’m sharing the setup that worked for me here.

I updated brew and used it to install rbenv and ruby-build. The latter allows us to select a series of Ruby versions to chose from.

$ brew update && brew install rbenv ruby-build

To ensure that rbenv is correctly loaded when we start a new shell, I had to edit a couple of files. I added the following line to .zshenv:

export PATH="$HOME/.rbenv/bin:$PATH"

This adds gem-specific binaries to the PATH so that they are accessible to the shell without fully qualifying them with their path.

Then I added the following lines to .zshrc:

source $HOME/.zshenv
eval "$(rbenv init - zsh)"

This ensures that the previous file is sourced when a new session is started and that rbenv is initialized for Zsh.

At this point, you’ll want to source .zshrc (or simply restart the terminal of your choice like iTerm2, instead):

$ source ~/.zshrc

With rbenv ready to go, I installed the latest version of Ruby and set it as my global default for this machine.

You can determine the latest stable version by running:

$ rbenv install -l

Go ahead and run:

$ rbenv install 2.7.2
$ rbenv global 2.7.2

After restarting the terminal again, do a quick sanity test to ensure that the right version of Ruby is being selected:

$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin20]

$ ruby -e "puts (1..100).reduce(:+)"
5050

Finally, you’ll be able to install htmlbeautifier, like I did:

$ gem install htmlbeautifier

No need for sudo.

I hope this helps anyone who is trying to install rbenv on zsh. It did the trick for me.

Get more stuff like this

Subscribe to my mailing list to receive similar updates about programming.

Thank you for subscribing. Please check your email to confirm your subscription.

Something went wrong.

12 Comments

  1. Anthony September 8, 2019
  2. Nick January 15, 2020
  3. Russell February 24, 2020
  4. froveroto April 9, 2020
  5. Ronald June 14, 2020
  6. bkclerke July 14, 2020
    • Antonio Cangiano August 9, 2020
      • Ronald August 9, 2020
        • Antonio Cangiano August 11, 2020
  7. Chris Hart August 12, 2020

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.