When I use command gem install bundler
in MacOS 10.13.x, the error is:
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
$ gem install bundler
Fetching: bundler-1.16.2.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
Update: I've since written a very detailed explanation of the various ways you can install Ruby gems on a Mac. My original recommendation to use a script still stands, but my article goes into more detail: https://www.moncefbelyamani.com/the-definitive-guide-to-installing-ruby-gems-on-a-mac/
I'm not sure if you answered your own question because that's the solution you recommend, or one you found while searching online. Either way, I don't recommend using sudo
to install gems because it can lead to issues that you might not be able to undo.
The reason why you are getting that permissions error is because macOS won't let you modify the Ruby version that comes installed with your Mac. The specific version of open source tools that come preinstalled on a Mac are meant to be administered by Apple only. The permissions error is there for a reason. You don't want to override it with sudo
.
The good news is that you can install a separate version of Ruby that does not interfere with the one that came with your Mac. Once that's done, your PATH
will need to be updated
such that the location of the new Ruby version is first in the PATH
. The PATH
refers to the list of directories, and the order in which the computer looks them up to find executable programs. If you type echo $PATH
in Terminal, you will see the list of directories, separated by a colon. It might look something like this:
/usr/bin:/bin:/usr/sbin:/sbin
This means that when you type gem install bundler
, your computer looks for the program called gem
in /usr/bin
first, and since that's where the Apple-provided version of Ruby lives, it gives you that permission error.
Once you install a new version of Ruby, if you update your PATH
such that the location of the new Ruby is first in the list, then your computer will look there first when you run gem install bundler
, and it will work.
There are several ways to install Ruby on a Mac. The best way that I recommend, and that I wish was more prevalent in the various installation instructions out there, is to use an automated script that will set up a proper Ruby environment for you, including updating the PATH
. This drastically reduces the chances of running into an error due to inadequate instructions that make the user do a bunch of stuff manually and leaving it up to them to figure out all the necessary steps.
The other route you can take is to spend extra time doing everything manually and hoping for the best. First, you will want to install Homebrew, which makes it easy to install other tools and macOS apps.
Then, the 4 most popular ways to install a separate version of Ruby are:
brew install ruby
, then update your PATH
by running echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
, followed by source ~/.bash_profile
chruby and ruby-install - my personal recommendations and the ones that are automatically installed by the aforementioned script. These can be installed with Homebrew.
rbenv - can be installed with Homebrew
To check that you're now using the non-system version of Ruby, you can run the following commands:
which ruby
It should be something other than /usr/bin/ruby
ruby -v
It should be something other than 2.3.7. As of today, 2.6.1 is the latest Ruby version.
Once you have this new version of Ruby installed, you can now install bundler:
gem install bundler