21

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.
pkamb
  • 33,281
  • 23
  • 160
  • 191
Chars Davy
  • 1,553
  • 3
  • 14
  • 14
  • 4
    Does this answer your question? [You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user)](https://stackoverflow.com/questions/51126403/you-dont-have-write-permissions-for-the-library-ruby-gems-2-3-0-directory-ma) – monfresh Dec 31 '20 at 21:57

4 Answers4

41

This is basically a duplicate of this question, where I already posted a detailed answer that solves the problem. Instead of maintaining two separate answers, I thought it would be better to just link to my answer here since the same solution applies: https://stackoverflow.com/a/54873916/928191

monfresh
  • 7,974
  • 1
  • 25
  • 22
  • 2
    Thanks for the detailed answer. For me, using zsh, the ruby install recommended the command `echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc`. After that, I ran `source ~/.zshrc`. – absynce Jul 01 '19 at 14:20
  • I wish I could accept this answer on the asker's behalf. A whole day spent trying to diagnose "mkmf.rb can’t find header files for ruby at /usr/lib/ruby/include/ruby.h", "Permission denied @ dir_s_mkdir - /Library/Ruby/Gems", take ownership of the /System/Library folder, reset ruby to a blank state, restore with time machine, and finally wipe ruby from my Mac altogether, and I finally come across a solution – Jason Oct 16 '19 at 23:10
23

As previously noted, on a Mac the system ruby is owned by root and it's not safe to install things against that version with sudo. If you do every gem runs as root and that's a security nightmare. DO NOT DO THAT

I'll walk you through my steps since previous answers assume a bit of command line foo and the added details might be of some use to someone.

Double-check we're running the old, system provided ruby

which ruby
/usr/bin/ruby

(that's the system path)

ruby -v
ruby 2.3.7

(the old version)

brew install ruby

or install brew first

at the end of which, the install says:

/usr/local/Cellar/ruby/2.6.3

Make that show up in the path first

PATH=/usr/local/Cellar/ruby/2.6.3/bin:$PATH

Double-check

which ruby
/usr/local/Cellar/ruby/2.6.3/bin/ruby

Double-check version

ruby --version
ruby 2.6.3p62

Make the path update permanent (otherwise you'll have to update the path each time you want to use ruby)

echo  PATH=/usr/local/Cellar/ruby/2.6.3/bin:$PATH >> ~/.bash_profile
Jas
  • 794
  • 12
  • 22
joar
  • 473
  • 3
  • 7
7
sudo gem install -n /usr/local/bin bundler
Zoe
  • 27,060
  • 21
  • 118
  • 148
Chars Davy
  • 1,553
  • 3
  • 14
  • 14
  • This is a bad practice and not recommended. Read more [here.](https://stackoverflow.com/a/54873916/928191) – Boris Y. May 02 '21 at 16:12
1

Best not to sudo all the way.

Proper way is to source ~/.zshrc works for me after you install the latest ruby in your Mac via brew install ruby

Bryan
  • 46
  • 4