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
$ 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
$ 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.
Thank you for writing this write up! I was trying to install CocoaPods on a new Mac running macOS Catalina and did not want to use
sudo
as well. I also noticed zsh on Catalina, which I’m not too familiar with, but wanted to try out since it’s replacing bash. Your write up was very helpful!I did notice once small typo. You wrote
.zhrc
instead of.zshrc
in one spot.Great catch, Anthony. I’m glad this was useful.
This was a huge help! Thank you
Thank you so much, Antonio! I literally spent 24 hours trying to find what’s the problem installing Rails. None of the solutions helped. Your approach is clear, elegant, and worked for me! Greatly appreciate it.
Thank you for stopping by, Russell.
work like a charm! thank you!
I don’t see what’s the point in adding .rbenv/bin to the PATH. This directory is not created during the installation of rbenv via homebrew, and if I really need it one day, I should at least created it before adding it to the PATH.
Do you know where to find the .zshenv file to add the export PATH line? I can’t seem to find it. I’m running into the same issue trying to install a ruby gem on Catalina. I’m using ZSH as well. Thank you!
Hi Blake, the .zshenv file should be located within your home folder. From your terminal, do a
ls -la ~
to see the file if it exists.If it’s not there then it’s possible Zsh is not installed correctly on your system.Unless you create the file, it is unlikely that a correct zsh installation will create by itself in its home directory. If we need certain zsh startup files, we create them.
Thanks, Ronald. It’s been a while since I installed it last and I thought it was automatically generated. If the file is not there, executing a
touch ~/.zshenv
will create the file for you, Blake.Thank you SO much for this! Still getting used to zsh instead of bash over here.