633

I'm trying to install a gem using gem install mygem or update RubyGems using gem update --system, and it fails with this error:

ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

Does anyone have an idea how to solve this?

Michael
  • 8,362
  • 6
  • 61
  • 88
Gaurav
  • 8,227
  • 4
  • 34
  • 55
  • 6
    Here is my solution: http://stackoverflow.com/questions/34071868/gem-install-git-up-no-longer-working-in-el-capitan/34071869#34071869 – LanceP Dec 03 '15 at 18:06
  • 80
    `sudo chown -R $USER /Library/Ruby/Gems/` – vaskort Jan 23 '17 at 20:56
  • 1
    i was using zsh shell and it was path problem in ~/.zshrc. i used this line at the end of the file and it worked for me: export PATH="$HOME/.rbenv/shims:$PATH" – rosnk Jan 05 '19 at 16:54

32 Answers32

529

Try adding --user-install instead of using sudo:

gem install mygem --user-install
Michael
  • 8,362
  • 6
  • 61
  • 88
nathanwhy
  • 5,884
  • 1
  • 12
  • 13
  • 4
    I don't know why your answer wasn't posted before. The `--user-install` option has apparently [been around for a while.](http://www.binarytides.com/quick-tip-installing-ruby-gems-in-the-users-home-directory/) I just wish you had written this earlier. =) – Michael Aug 19 '16 at 14:01
  • 8
    This needs to go to the top! Installing with sudo or su is a bad idea as you're enforcing a permissions system on the modules and possibly the globally installed packages could collide with otherwise installed packages or other projects' dependencies. Using rbenv requires you to install another software and adds a layer of complexity. – Hubert Grzeskowiak Jan 09 '17 at 14:46
  • 8
    This works, but warns: 'WARNING: You don't have /Users//.gem/ruby/2.0.0/bin in your PATH, gem executables will not run.' The following link provides useful instructions on how to update your path (without having to use vi) http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac – dawid Jan 12 '17 at 06:18
  • @dawid I got the same warning, however I can confirm that the gem was installed, and sass (including watch) work well. – Liran H Feb 25 '17 at 14:36
  • While this has been available for a while, it's only a partial solution. [Ruby still has to be told to use those alternate gems](http://guides.rubygems.org/faqs/#user-install) or nothing will be improved. It also doesn't do anything about updating Ruby itself, which is nicely handled by rbenv or RVM. Which to choose is dependent on the needs. As a developer I want multiple Ruby versions to test for current and future compatibility. For a server I might want a single version with additional gems. – the Tin Man May 12 '17 at 18:23
  • 4
    This is simple and logical. Add ruby path if you haven't in your bashrc `if which ruby >/dev/null && which gem >/dev/null; then PATH="$(ruby -rubygems -e 'puts Gem.user_dir')/bin:$PATH" fi`
    Source:http://guides.rubygems.org/faqs/#user-install
    – Inder Kumar Rathore Aug 22 '17 at 10:42
  • Simply perfect!! - This ought to be the number one answer although the RBENV answer also sounds attractive as it means you're not messing about with the Mac system itself, it just feels fiddly using something else to achieve what's already installed. This is good for me: `--user-install` – Mr. Benedict Nov 12 '17 at 12:12
  • 1
    Can this screw up with Apple's (ruby installed) system? –  May 24 '18 at 17:42
  • Better yet, put --user-install in your ~/.gemrc file so you don't have to type it every time...https://stackoverflow.com/a/33651045/4145420 – MGY Sep 17 '18 at 08:09
  • What does it actually do? Does it have the potential to interfere with anything? – PlsWork May 11 '19 at 14:11
  • This didn't work at first. I had to add the following to the end of my `.bash_profile` file (located in the home directory): `[shell] export PATH=$PATH:/Users/yourUserName/.gem/ruby/2.3.0/bin [/shell]`. Then it worked. – PlsWork May 11 '19 at 14:35
335

You don't have write permissions into the /Library/Ruby/Gems/1.8 directory.

means exactly that, you don't have permission to write there.

That is the version of Ruby installed by Apple, for their own use. While it's OK to make minor modifications to that if you know what you're doing, because you are not sure about the permissions problem, I'd say it's not a good idea to continue along that track.

Instead, I'll strongly suggest you look into using either rbenv or RVM to manage a separate Ruby, installed into a sandbox in your home directory, that you can modify/fold/spindle/change without worrying about messing up the system Ruby.

Between the two, I use rbenv, though I used RVM a lot in the past. rbenv takes a more "hands-off" approach to managing your Ruby installation. RVM has a lot of features and is very powerful, but, as a result is more intrusive. In either case, READ the installation documentation for them a couple times before starting to install whichever you pick.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • 185
    You don't need to use a separate Ruby library though. Just put 'sudo' in front of your as Michael suggested above. – serraosays Apr 13 '13 at 22:19
  • 12
    I think a 'chown' for that directory would be a better idea. Since doing 'sudo' would allow gem install to do whatever the heck it wants anywhere. If it were a nefarious install script this could be bad. The question is who do I 'chown' to, and where? – Breedly Dec 24 '13 at 20:31
  • 37
    Changing ownership of Apple's installation of Ruby isn't a good idea because System installs/upgrades, and Disk Utility can automatically revert/repair the ownership and permissions of the directory. That will frustrate the user and can frustrate the OS and code that expects to have write permissions/ownership of those directories. Apple installs apps that use its Ruby and could expect a certain version or behavior, so those could break. Why bother when it's easier and safer to use RVM or rbenv and not worry about it. – the Tin Man Dec 24 '13 at 22:54
  • 211
    For those like me who don't actually do any ruby dev and don't want to mess around for half an hour trying to figure out what all these crystal thingies are. `brew install ruby` will solve your dependency issues in a clean way which doesn't nuke the apple install. If you've already `sudo`'d an update on the initial install and are worried about what you've done, you should be able to undo it with this: `cd /System/Library/Frameworks/Ruby.framework/Versions;sudo rm Current; sudo ln -s 1.8 Current;` – lsl Feb 15 '14 at 03:23
  • 2
    @Louis I did the sudo tangle and wound up modifying my pre-installed Ruby. I did your steps to try to fix the problem, but now when I do irb or ruby -v, I get command not found. What's going on and is there a fix? – noobsmcgoobs Mar 22 '14 at 07:34
  • 2
    is /System/Library/Frameworks/Ruby.framework/Versions/Current pointing to anything? You may have a different version to me installed. Current is just a symlink to the current version it should be using, in my case apples install was 1.8 before i upgraded so I just pointed the symlink back to it. – lsl Mar 22 '14 at 10:39
  • 24
    "`I did the sudo tangle and wound up modifying my pre-installed Ruby. I did your steps to try to fix the problem, but now when I do irb or ruby -v, I get command not found. What's going on and is there a fix`" and this just adds more weight to why people shouldn't use `sudo` or `chown`. Sigh.... – the Tin Man Mar 24 '14 at 15:41
  • 1
    Ubuntu 14.04 LTS notifying: gem update --system is disabled on Debian, because it will overwrite the content of the rubygems Debian package, and might break your Debian system in subtle ways. The Debian-supported way to update rubygems is through apt-get, using Debian official repositories. If you really know what you are doing, you can still update rubygems by setting the REALLY_GEM_UPDATE_SYSTEM environment variable, but please remember that this is completely unsupported by Debian. – support_ms Aug 06 '14 at 13:53
  • Tin Man is wearing a tin foil hat. You will likely not mess up your Ruby install by using SUDO. If you do, then know your computer can be restored. Apply will reinstall the OS for you. Sure, not the best way to go in a HIGHLY UNLIKELY event of failure, but it won't be the end of the world. – Ricardo Parker Dec 31 '14 at 03:17
  • 2
    Never use SUDO, if you have to do this, you have done something wrong in your setup. Please use rbenv or rvm. – Chris Hough Jan 06 '15 at 08:14
  • 16
    Doing the `brew install ruby` (which took 5 minutes to run) did not fix the permissions problem for me. I'll try the other suggestion of using a Ruby version manager... – Erik van der Neut Mar 19 '15 at 00:02
  • 3
    I installed ruby with homebrew and am still dealing with this issue. Cant seem to figure it out... – Taylor Dec 05 '15 at 23:28
  • If you're using rvm I think you want: $ `rvm all do gem install _gemname_` – Ford Davis Mar 04 '16 at 23:18
  • 55
    Solved: on El Captain after doing `brew install ruby` simply close the Terminal and reopen it again. – Fmessina Jun 15 '16 at 12:50
  • 2
    @Fmessina your solution didn't work. Still getting same error even after closing and opening terminal again – vikramvi Jan 04 '17 at 14:57
  • 1
    On Sierra, `brew install ruby` doesn't mess up the existing Apple installation, worked for me. – webmaniacgr Jun 25 '17 at 17:16
  • FYI: after installed ruby via rbenv, you should re-install your gems installed via system ruby version. For that just type `gem install` for your gem, which has required permission or sudo before. And don't forget make `rbenv rehash`. After that you can easily run `bundle install`. – hamsternik Dec 26 '18 at 12:07
  • Check my answer below for MacOs Mojave: https://stackoverflow.com/a/54034953/598200 – Fmessina Jan 04 '19 at 07:51
  • @staypuftman Just add sudo and run these 2 commands as below sudo gem install sass then sudo gem install cocoapods – Govind Wadhwa Feb 20 '19 at 12:58
  • 2
    @Fmessina my `which gem` still points to `/usr/bin/gem` after running `brew install ruby`. Do I have to do anything to change the default `ruby`, `gem`, and `irb` commands? – sleighty Apr 02 '19 at 16:50
  • rbenv + `echo 'export GEM_HOME="$HOME/.gem"' >> ~/.bash_profile` + `echo 'export PATH="$GEM_HOME/bin:$PATH"' >> ~/.bash_profile` saved my day – Maksim Kostromin Jun 16 '19 at 14:35
  • 1
    @BrunoEly my `gem` still pointed to the incorrect spot as well. What I did (following this answer https://stackoverflow.com/questions/6482738/installing-ruby-gems-not-working-with-home-brew) was to add `export PATH=/usr/local/opt/ruby/bin:$PATH # so newer gem from brew install is there` to my `.bash_profile` to update my environment so gem referenced my `brew` installation. – Nick Brady Nov 01 '19 at 20:26
96

You really should be using a Ruby version manager.

Using one properly would prevent and can resolve your permission problem when executing a gem update command.

I recommend rbenv.

However, even when you use a Ruby version manager, you may still get that same error message.

If you do, and you are using rbenv, just verify that the ~/.rbenv/shims directory is before the path for the system Ruby.

$ echo $PATH will show you the order of your load path.

If you find that your shims directory comes after your system Ruby bin directory, then edit your ~/.bashrc file and put this as your last export PATH command: export PATH=$HOME/.rbenv/shims:$PATH

$ ruby -v shows you what version of Ruby you are using

This shows that I'm currently using the system version of Ruby (usually not good)

$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

$ rbenv global 1.9.3-p448 switches me to a newer, pre-installed version (see references below).

This shows that I'm using a newer version of Ruby (that likely won't cause the Gem::FilePermissionError)

$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]

You typically should not need to preface a gem command with sudo. If you feel the need to do so, something is probably misconfigured.

For details about rbenv see the following:

CupawnTae
  • 14,192
  • 3
  • 29
  • 60
l3x
  • 30,760
  • 1
  • 55
  • 36
  • 9
    You shouldn't have to mess around with the pass yourself if you're using `rbenv`, if you follow the [Homebrew installation instructions](https://github.com/sstephenson/rbenv#homebrew-on-mac-os-x), all you have to do is add `eval "$(rbenv init -)"` to your shell config, and `rbenv` should take care of setting up your `PATH` for you. –  Mar 01 '14 at 01:49
  • Thanks. I'd forgotten the 'rbenv global' so I was still using the system ruby, and gem install was failing. – Graham Perks Apr 06 '14 at 01:55
77

This will fix the issue on MacOS Mojave and Catalina in a clean way:

brew install ruby

Then set GEM_HOME to your user directory. On the terminal:

  • Bash:

    echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
    echo 'export GEM_HOME=$HOME/gems' >> ~/.bashrc
    echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc
    
  • OR if on Zsh:

    echo '# Install Ruby Gems to ~/gems' >> ~/.zshrc
    echo 'export GEM_HOME=$HOME/gems' >> ~/.zshrc
    echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.zshrc
    source ~/.zshrc
    
Michael
  • 8,362
  • 6
  • 61
  • 88
Fmessina
  • 3,751
  • 3
  • 31
  • 34
  • 1
    This didn't work for me until I ran `xcode-select --install`. This is required to compile extensions. – Marc Perrin-Pelletier Mar 11 '19 at 16:40
  • 1
    I ran this and I want to be able to reset the changes done by these, do you know how to revert it? @Fmessina – KarenAnne Oct 10 '19 at 03:35
  • this should work: edit your `~/.bashrc` or `~/.zshrc` file, remove the 3 lines added, and then run again the `source ~/.bashrc` command – Fmessina Oct 10 '19 at 11:48
  • 3
    Note that after you do this, you should use "gem install ..." instead of "sudo gem install ..." as instructed by many install guides – sagism Jul 16 '20 at 07:51
74

Why don't you do:

sudo gem update --system
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Bobz
  • 2,345
  • 2
  • 18
  • 22
  • 43
    Installing gems as root is not a good idea. http://stackoverflow.com/questions/2119064/sudo-gem-install-or-gem-install-and-gem-locations – spuder May 27 '14 at 21:28
  • Had issues with `brew upgrade` `Error: Permission denied...`. This fixed it. Thanks! – Ostap Andrusiv Dec 15 '14 at 11:05
  • 13
    Never use SUDO, if you have to do this, you have done something wrong in your setup. Please use rbenv or rvm. – Chris Hough Jan 06 '15 at 08:13
  • 14
    Why is everyones answer to permissions problems to just always throw sudo in front of a command? How is this best-practice? – Steven Apr 07 '15 at 04:57
  • hyh:qzc.xcodeproj ylgwhyh$ gem install cocoapods ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory. – ylgwhyh Mar 03 '16 at 03:21
  • Nope. "ERROR: While executing gem ... (Errno::EPERM)" – JGallardo Aug 07 '16 at 23:28
  • In my case not the solution, it says: "Latest version already installed. Done." – leopinzon Mar 31 '18 at 16:33
36

For me the problem was due to using rbenv and forgetting to set the proper version globally.

So I had to set it with rbenv global xxx

In my case I installed 2.0.0-p247 so I had to issue the command:

rbenv global 2.0.0-p247
rbenv rehash

Then all was working fine.

Hendrik
  • 4,849
  • 7
  • 46
  • 51
  • 4
    For those of you already using rbenv and started receiving a Permission error when installing gems after upgrading OSX to a newer version: You should start by setting the rbenv version `rbenv global 2.1.1` for example and then follow with `rbenv rehash`. Now re-running your `gem install` command. – njappboy Nov 12 '14 at 01:00
  • 1
    ^This + restart terminal. – XåpplI'-I0llwlg'I - May 07 '15 at 05:04
  • 1
    future explorers: rbenv rehash has been deprecated https://github.com/rbenv/rbenv-gem-rehash – TCannadySF Jun 04 '16 at 21:50
  • Worked for me perfectly! It was the `rbenv rehash` I was missing. – James Jun 04 '17 at 17:57
  • Worth noting that it's system ruby that would have been lacking permissions and rbenv would have been using system ruby if no global ruby was set. This be verified with `rbenv versions`. – Dennis Apr 12 '18 at 09:57
26

You need to correct your paths.

To determine if this fix will work, run the following:

which gem

This should output a directory you do not have permissions to:

/usr/bin/gem

To fix this perform the following steps:

  1. Determine the path you need to copy to your profile:

    rbenv init -
    

    The first line of the output is the line you need to copy over to your profile:

    export PATH="/Users/justin/.rbenv/shims:${PATH}" #path that needs to be copied
    source "/usr/local/Cellar/rbenv/0.4.0/libexec/../completions/rbenv.zsh"
    rbenv rehash 2>/dev/null
    rbenv() {
        typeset command
        command="$1"
        if [ "$#" -gt 0 ]; then
            shift
        fi
    
        case "$command" in
            rehash|shell)
                eval `rbenv "sh-$command" "$@"`;;
            *)
                command rbenv "$command" "$@";;
        esac
    }
    
  2. Copy the path to your profile and save it.

  3. Reload your profile (source ~/.zshenv for me).

  4. Run rbenv rehash.

Now when you run which gem you should get a local path that you have permissions to:

/Users/justin/.rbenv/shims/gem
Michael
  • 8,362
  • 6
  • 61
  • 88
Justin Leveck
  • 2,308
  • 2
  • 20
  • 22
  • That was the problem in my situation. You should be able to include `eval "$(rbenv init -)"` in your `~/.bash_profile` to make all the necessary changes. – nyi Feb 07 '16 at 11:38
  • best answer. my profile was named .bash_profile and you can open the file like this: touch ~/.bash_profile; open ~/.bash_profile and add the line you mentioned at point 1. – CarmenA Feb 11 '17 at 22:09
17

Try nathanwhy's answer before using my original answer below. His recommendation of --user-install should accomplish the same purpose without having to muck with your .bash_profile or determine your Ruby version.


If you are not concerned about a specific ruby version, you can skip the heavy-lift Ruby environment manager options, and just add these lines to ~/.bash_profile:

export GEM_HOME="$HOME/.gem/ruby/2.0.0"
export GEM_PATH="$HOME/.gem/ruby/2.0.0"

The path is stolen from the original output of gem env:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.14
  - RUBY VERSION: 2.0.0
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
  - RUBY EXECUTABLE: /System/Library/.../2.0/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-14
  - GEM PATHS:
     - /Library/Ruby/Gems/2.0.0
     - /Users/mylogin/.gem/ruby/2.0.0  #  <---- This line, right here. -----
     - /System/Library/.../usr/lib/ruby/gems/2.0.0
  ...

No sudoing is required, and you can use the already-installed Ruby, courtesy of Apple.

Michael
  • 8,362
  • 6
  • 61
  • 88
  • 3
    +1: This worked great for me. Not everybody is the system administrator of the machine they are using, and hence not everybody has access to 'sudo', 'chown', or similar tools. In a similar vein, people should be aware of "gem install --user-install" which installs the gem to a directory in the user's home directory rather than to the one shared by all users. – Some Guy Mar 09 '16 at 01:30
17

This worked for me. Plus, if you installed gems as root before, it fixes that problem by changing ownership back to you (better security-wise).

sudo chown -R `whoami` /Library/Ruby/Gems
thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30
  • 1
    *Big NO!* never change the permission on `/Library` folder, the system may break in future updates. (sorry for late reply) – Raptor Apr 29 '19 at 03:14
  • @Raptor I'm modifying permissions on a subfolder, not "/Library". In my case, I mistakenly installed the gems as root, so I wanted to undo that. There are no negative consequences that I can think of theoretically nor have there been any that I've observed. I'm curious - why do you think this is a bad idea? – thebiggestlebowski May 09 '19 at 04:26
  • The path is for system. Further updates of macOS may break. – Raptor May 09 '19 at 04:34
  • The gem is installed by default to this location. It was unusable because it was mistakenly installed as root (sudo) instead of my normal user. The outcome of what I did was the same as if I'd installed as $USER in the first place. I don't see the harm. Most of the other answers here suggest the same outcome/solution, but different ways to achieve it. I still don't see the problem. Are you saying gems should not be installed under /Library? – thebiggestlebowski May 10 '19 at 00:49
15

There are two routes: Use either rbenv or RVM. There are recipes for both below. Before you do, you probably want to turn off the installation of local documents for gems.

echo "gem: --no-ri --no-rdoc" >> ~/.gemrc

Then:

install rbenv

install ruby-build

run:

rbenv install 2.1.2 (or whatever version you prefer)
rbenv global 2.1.2
gem update --system

This installs an up-to-date version of the gem system in your local directories. That means you don't interfere with the system configuration. If you're asking this question, you shouldn't be messing with system security, and you'll spend longer understanding what issues you may run into, than just having an easy way to avoid the problem you started with. Learn InfoSec later, when you know more about the operating system and programming.

For an alternative use 'RVM' instead: To install rvm run:

rvm install 2.1.2
rvm use 2.1.2
gem update --system

This has the same result, you end up with a local Ruby and Gem system that doesn't interfere with the system versions. There is no need for Homebrew, or over-riding system libs, etc.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
JezC
  • 1,868
  • 17
  • 19
14
sudo gem update --system
sudo gem install (gemfile)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Eliot Arntz
  • 401
  • 4
  • 6
14

sudo chown -R $USER /Library/Ruby/Gems/

vaskort
  • 2,591
  • 2
  • 20
  • 29
13

Older and wiser

Don't do what I say here, just know to be wary any time you use sudo. You probably want to use something like rbenv to isolate whatever work you're doing.


a way

learn about chown

I don't know if you like the command line, but this will make working on any project with any tool that installs packages to your system a breeze.

chown as far as I can tell, stands for change ownership.

The reason I came looking for this answer is because gem install threw this error at me today:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /var/lib/gems/1.9.1 directory.

This is a perfect opportunity to use chown. You see Ruby has given us the directory it needs access to, and it seems like it's a directory it will use pretty often.

In this case, there are only three things one needs to know to solve the problem, but chown is much more powerful, and grants you a lot more flexibility than I will demonstrate now. Please refer to the source at the bottom for more information.

The Two Things

  1. Username
  2. Directory

If you're in a shell finding the username is easy. Just look at the prompt. Mine looks like:

breadly@breadly-desktop:~\Desktop

The current user is just the name before the @. We know the directory from the error messages, but you have two choices. You can either limit your permission to the current version by using ../gems/1.9.1, or give yourself write permission for gems of all version by using ../gems.

The command to actually change ownership would look like this.

chown -R $(whoami) /absolute/path/to/directory

The -R is known as a flag and the -R flag typically tells a command to do something recursively, or in other words perform the command on every thing that is contained in the directory, and all the things contained in the directories contained within, and so on till there isn't anything else.

Breedly
  • 12,838
  • 13
  • 59
  • 83
  • Also, if you want to find out what group your user belongs in you can refer to this. http://stackoverflow.com/questions/350141/in-unix-linux-how-to-you-find-out-what-group-a-given-user-is-in-via-command-line – Breedly Dec 24 '13 at 20:57
  • 3
    However, you might want to be careful about changing ownerships if it has to install to the /usr/local/bin directory. – Breedly Dec 24 '13 at 21:02
  • 16
    Changing ownership of anything in the /usr, /Library or /var paths, among others, is a bad idea. The system can halt due to files and drivers being no longer accessible, and future updates can break if the installer senses that the paths are no longer owned by root:wheel. Fixing the damage can be really hard and is usually best done by reinstalling the OS. Naive/inexperienced users don't know enough about the OS to understand what files/directories could possibly be safe and should be cautioned to not try this at home. – the Tin Man Jul 31 '14 at 18:41
  • 1
    Lol this is an embarrassing answer. :) Older and wiser now I guess. – Breedly Feb 08 '19 at 17:16
13

I found this how-to for sudoless gem:

  1. brew install rbenv ruby-build
  2. sudo gem update --system
  3. add exports to .bashrc:

    export RBENV_ROOT="$(brew --prefix rbenv)"
    export GEM_HOME="$(brew --prefix)/opt/gems"
    export GEM_PATH="$(brew --prefix)/opt/gems"
    
  4. And finally add this to your ~/.gemrc:

    gem: -n/usr/local/bin
    
  5. gem update --system

Michael
  • 8,362
  • 6
  • 61
  • 88
gopek
  • 686
  • 7
  • 20
  • I used `brew install rebenv ruby-build`, then there is a `system` after my directory? Any idea what caused this, and how to get rid of that `system`? – 7537247 Jan 08 '16 at 04:30
12

I needed to do a rbenv rehash so it would point to my local Gem library.

It looks like you've got your gem manager pointing to the System Library, so, instead of messing with permissions, do the equivalent of "rehash" for your manager to get things pointing locally.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
bobbdelsol
  • 996
  • 6
  • 21
12

I had formatted my Mac and many suggested solutions did not work for me. What worked for me are these commands in the correct order:

  1. Install Homebrew:

    /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    
  2. Install Ruby:

    brew install ruby
    
  3. Install Compass:

    sudo gem install compass
    
Herbi Shtini
  • 2,002
  • 29
  • 34
  • You should never need to use `sudo` to install something with gem. Using `sudo` installs it into the Ruby owned by the system, not into one you own. Using a sandboxed Ruby is much easier to manage and use when programming because you can create multiple disparate instances and try different things then destroy them when you're done. – the Tin Man Apr 08 '19 at 18:49
5

Work for me:

sudo gem uninstall cocoapods

sudo gem install cocoapods
evya
  • 3,381
  • 1
  • 25
  • 28
  • Amazing Worked for me aswel! Thanks. Saved effort of installing rbenv or RVM – NaXir Nov 12 '15 at 10:36
  • 5
    Downvoting because, as has been noted in other comments, installing gems as root is a dangerous practice. The installer could do literally anything to your system. – Some Guy Mar 09 '16 at 01:34
  • @SomeGuy: "The installer could do literally anything"... it's more likely the user themselves could fat-finger the command and mangle the system. I can't think of a time an installer messed up my system, but I can clearly remember times I did. `sudo` is a command that newbies love because it makes it possible to do the wrong thing so easily, and it takes years to learn to be appropriately paranoid when using it. – the Tin Man Apr 08 '19 at 18:52
  • 1
    The best answer!!!! After run these 2 command, please restart the Terminal and, that's all!!! Thanks a lot!!! – nosequeweaponer Dec 06 '19 at 14:46
5

Install rbenv by brew install rbenv;

Then put eval "$(rbenv init -)" at the end of ~/.bash_profile (or ~/.zshrc of MacOS);

Open a new terminal and run gem install *** will work!

zhao
  • 1,094
  • 9
  • 9
  • Did not work for me ``❯ brew install rbenv -q Warning: rbenv 1.1.1 is already installed and up-to-date To reinstall 1.1.1, run `brew reinstall rbenv` ~/.gem/ruby/2.3.0/gems/colorls-1.1.1 ❯ eval "$(rbenv init -)" ~/.gem/ruby/2.3.0/gems/colorls-1.1.1 ❯ gem update Updating installed gems Updating CFPropertyList ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.`` – Max Coplan Dec 10 '18 at 23:07
4

Check to see if your Ruby version is right. If not, change it.

This works for me:

$ rbenv global 1.9.3-p547
$ gem update --system
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Zen
  • 49
  • 3
4

Steps to resolve issue:

Step 1: check and install cocoapods with home brew

brew install cocoapods

Step 2: This is most important, to resolve all the issues, installing rvm package with stable ruby version, without this you will be accessing ruby version inside System folder where you don't have permission.

\curl -sSL https://get.rvm.io | bash -s stable --ruby

Step 3: Add rvm command path to .zshrc file to access it globally

export PATH="$PATH:$HOME/.rvm/scripts/rvm"

Step 4: check if rvm is running properly inside command line globally

rvm --version 

Step 5: Now you can install cocoapods package inside user bin as this will access ruby file from rvm folder, without any permission needed.

sudo gem install -n /usr/local/bin cocoapods

Step 6: Now you can check pod also by using below command

pod install
CodingEra
  • 1,313
  • 10
  • 20
  • This is the only answer that allowed me to install cocoapods on MacOS 13 Ventura. Thank you. – Matt May 10 '23 at 10:59
3

A 2021 solution (using rvm):

If you type which ruby in terminal, and it shows /usr/bin/ruby, you can try this solution.

  1. install rvm

    curl -L https://get.rvm.io | bash -s stable
    
  2. install ruby using rvm

    rvm install "ruby-3.0.0"
    
  3. use your installed version of ruby

    rvm use ruby-3.0.0
    
  4. type which ruby again, which will show /Users/mac_user_name/.rvm/rubies/ruby-3.0.0/bin/ruby.

    It's a new path to use ruby.

Michael
  • 8,362
  • 6
  • 61
  • 88
Bomi Chen
  • 81
  • 4
1

As pointed out by bobbdelsol, rehash worked for me :

==> which ruby
/usr/bin/ruby

==> rbenv install 1.9.3-p551
Downloading ruby-1.9.3-p551.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p551.tar.bz2
Installing ruby-1.9.3-p551...
Installed ruby-1.9.3-p551 to /Users/username/.rbenv/versions/1.9.3-p551


==> which ruby
/Users/username/.rbenv/shims/ruby

==> which gem
/Users/username/.rbenv/shims/gem

==> gem install compass
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.


==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]


==> rbenv global 1.9.3-p551


==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]


==> rbenv global 1.9.3-p551


==> rbenv rehash


==> ruby -v
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin15.4.0]


==> gem install compass
Fetching: sass-3.4.22.gem (100%)
Fetching: multi_json-1.11.3.gem (100%)
Fetching: compass-core-1.0.3.gem (100%)
Fetching: compass-import-once-1.0.5.gem (100%)
Fetching: chunky_png-1.3.5.gem (100%)
Fetching: rb-fsevent-0.9.7.gem (100%)
Fetching: ffi-1.9.10.gem (100%)
Building native extensions.  This could take a while...
Fetching: rb-inotify-0.9.7.gem (100%)
Fetching: compass-1.0.3.gem (100%)
    Compass is charityware. If you love it, please donate on our behalf at http://umdf.org/compass Thanks!
Successfully installed sass-3.4.22
Successfully installed multi_json-1.11.3
Successfully installed compass-core-1.0.3
Successfully installed compass-import-once-1.0.5
Successfully installed chunky_png-1.3.5
Successfully installed rb-fsevent-0.9.7
Successfully installed ffi-1.9.10
Successfully installed rb-inotify-0.9.7
Successfully installed compass-1.0.3
9 gems installed
Installing ri documentation for sass-3.4.22...
Installing ri documentation for multi_json-1.11.3...
Installing ri documentation for compass-core-1.0.3...
Installing ri documentation for compass-import-once-1.0.5...
Installing ri documentation for chunky_png-1.3.5...
Installing ri documentation for rb-fsevent-0.9.7...
Installing ri documentation for ffi-1.9.10...
Installing ri documentation for rb-inotify-0.9.7...
Installing ri documentation for compass-1.0.3...
Installing RDoc documentation for sass-3.4.22...
Installing RDoc documentation for multi_json-1.11.3...
Installing RDoc documentation for compass-core-1.0.3...
Installing RDoc documentation for compass-import-once-1.0.5...
Installing RDoc documentation for chunky_png-1.3.5...
Installing RDoc documentation for rb-fsevent-0.9.7...
Installing RDoc documentation for ffi-1.9.10...
Installing RDoc documentation for rb-inotify-0.9.7...
Installing RDoc documentation for compass-1.0.3...
Michael
  • 8,362
  • 6
  • 61
  • 88
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
  • sooo close. yet "ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory." – JGallardo Aug 08 '16 at 00:45
1

Tested on MacOS Mojave WITH SUCCESS:

  1. Uninstall all your old ruby versions (let's say you have 2.00 and 2.3.0):

    $ rvm uninstall 2.0.0

    $ rvm uninstall 2.3.0

  2. Install brand new ruby version:

    $ brew install ruby

  3. Set a default alias to your version:

    $ rvm alias create default ruby

  4. Reboot your system because this is the safest way your computer loads the new ruby version, recently installed.

AFTER you done above procedure, you can successfully run any gem command.

Marcelo Gumiero
  • 1,849
  • 2
  • 14
  • 14
0

You can change GEM_HOME. You have also under your home directory a gem folder to check it use

$ gem env

result is as follows. Unrelated parts are omitted.

...
  - GEM PATHS:
     - /Users/xxx/.gem/ruby/2.6.0
     - /Library/Ruby/Gems/2.6.0
     - /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0
...
 

You can use your /Users/xxx/.gem/ruby/2.6.0 folder.

vim ~/.bash_profile

add the following line

export GEM_HOME=~/.gem/ruby/2.6.0/

After that you can use

source ~/.bash_profile 
Koray Güclü
  • 2,857
  • 1
  • 34
  • 30
0

The issue for me was that I switched from zshell to bash earlier and was not logged in:

/bin/bash --login

Although I had rvm installed, it was not able to switch to my newly rvm-installed ruby version and was still trying to use the default Mac-installed ruby binary. Hence my confusion (user error!!!) and the continued permissions issues...

allthesignals
  • 307
  • 2
  • 13
-1

ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.6.0

[2022 Solution] The detailed reason for the issues has been added here

Summary of the issue -> The issue was related to ruby access, M1 MAC comes with its own ruby. We don't have permission to use that for our purpose. Instead, we install a separate instance of ruby and use it for our purpose.

The below steps helped me resolve the problem, hope this might help some

We don't need to install ruby with rvn or chruby. My solution uses homebrew to install ruby.

  1. Open the terminal

  2. Install ruby using homebrew

[for fresh install] brew install ruby

[for reinstalling] brew reinstall ruby

  1. Check the path of ruby using the below command

    which ruby

  2. It should be installed in the below path

    /usr/bin/ruby

  3. To change the ruby path to the user path

To check which shell is used by your system

echo $0

-zsh

For zshrc

echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/.zshrc

For bash

echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/~/.bashrc

  1. Quit and relaunch the terminal

  2. After changing the path with step 5

  3. Check for the path of the ruby again (execute step 3 - please make sure the path displays as given below)

/usr/local/opt/ruby/bin/ruby

[if you don't quit and launch the terminal, step 4 path will be shown]

  1. This step may not be applicable to everyone can skip step 10 & 11, if you have the correct Cocoapods version installed

Check the version of the pod installed

pod --version

  1. Uninstall the specific version of Cocoapods using the below command

In case the version installed is 1.11.0

gem uninstall cocoapods -v 1.11.0

  1. Install the Cocoapods of the specific version

    gem install cocoapods -v 1.11.0

  2. Change the path to the Project directory cd {path of the project directory}

  3. Install the bundler in the project directory

bundle install

  1. Execute pod install

pod install

Peer Mohamed Thabib
  • 636
  • 2
  • 9
  • 29
-3

cd /Library/Ruby/Gems/2.0.0

open .

right click get info

click lock

place password

make everything read and write.

myklgee
  • 11
  • 1
  • 1
    Please take the time to format your answer for readability. The effort you put into research and explaining pays off. – the Tin Man Dec 07 '15 at 17:43
-4

Installing gem or updating RubyGems fails with permissions error Then Type This Command

sudo gem install cocoapods
Hardip Kalola
  • 196
  • 1
  • 4
  • 1
    This has been covered in the other answers. Please read the other answers before creating one to ensure yours offers something new. – the Tin Man Dec 07 '15 at 17:41
-5

You can use: gem install cocoapods --pre --user

Alex_Burla
  • 800
  • 1
  • 9
  • 9
-5

give the user $whoami to create somethin in those folder

sudo chown -R user /Library/Ruby/Gems/2.0.0
  • No. That folder is owned by the system for a good reason, the system uses it for its own purposes. It's ok to run a script that points to it, but create and use a sandboxed Ruby if you want to mess with it. – the Tin Man Apr 08 '19 at 18:54
-6

I used this and worked.

$ sudo chown myuser /var/lib/gems

-20

The reason of the error is because you are not logged in as the root user on terminal.

If you already have root use enable on your mac in terminal type

$ su

If you dont have root user, you need to enable it using the following steps

  1. From the Apple menu choose System Preferences….
  2. From the View menu choose Users & Groups.
  3. Click the lock and authenticate as an administrator account.
  4. Click Login Options….
  5. Click the “Edit…” or “Join…” button at the bottom right.
  6. Click the “Open Directory Utility…” button.
  7. Click the lock in the Directory Utility window.
  8. Enter an administrator account name and password, then click OK.
  9. Choose Enable Root User from the Edit menu.
  10. Enter the root password you wish to use in both the Password and Verify fields, then click OK.

More at the same on http://support.apple.com/kb/ht1528

Atleast it works for me after getting stuck for couple of hours.

Mohammad Arif
  • 6,987
  • 4
  • 40
  • 42