While executing gem … (Gem::FilePermissionError)
When using gem, the package manager for Ruby, you can encounter the following error:
$ gem install bundler
...
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
You can solve this quickly with sudo, but changing permissions is not recommended even when the command itself is clear.
Instead, solve the problem through rbenv, the Ruby version manager.
Resolve the Error
Install via Homebrew. If you need to install Homebrew first, run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
First, update Homebrew and install rbenv by following the guide in the GitHub repository.
$ brew update
$ brew install rbenv ruby-build
After installation, use rbenv to check available Ruby versions:
$ rbenv install -l
2.7.7
3.0.5
3.1.3
// omitted...
As of November 2022, install the latest version, 3.1.3.
$ rbenv install 3.1.3
Downloading ruby-3.1.3.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.3.tar.gz
Installing ruby-3.1.3...
ruby-build: using readline from homebrew
Installed ruby-3.1.3 to /Users/madplay/.rbenv/versions/3.1.3
After installation, set 3.1.3 as the global version with rbenv and verify that it is applied correctly:
# Set global version
$ rbenv global 3.1.3
# Check version
$ rbenv versions
system
* 3.1.3 (set by /Users/madplay/.rbenv/version)
Then add the rbenv PATH to your shell profile. Depending on your shell, add the following to .bashrc for bash or .zshrc for zsh.
$ vi ~/.zshrc
[[ -d ~/.rbenv ]] && \
export PATH=${HOME}/.rbenv/bin:${PATH} && \
eval "$(rbenv init -)"
Apply the configuration with source.
$ source ~/.zshrc
Finally, run the command that previously failed. It now succeeds.
gem install bundler
Fetching bundler-2.3.26.gem
Successfully installed bundler-2.3.26
Parsing documentation for bundler-2.3.26
Installing ri documentation for bundler-2.3.26
Done installing documentation for bundler after 2 seconds
1 gem installed