Installing rbenv on Zsh (on MacOS)

Recently, I updated my Mac to the latest beta of macOS Catalina. 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:

$ rbenv install 2.6.5
$ rbenv global 2.6.5

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

$ ruby -v
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19]

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

Finally, I was able to install htmlbeautifier:

$ 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.

%d bloggers like this: