[ruby] How to install a gem or update RubyGems if it fails with a permissions error

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?

This question is related to ruby macos rubygems

The answer is


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


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
    

I used this and worked.

$ sudo chown myuser /var/lib/gems


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


Why don't you do:

sudo gem update --system

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.


Work for me:

sudo gem uninstall cocoapods

sudo gem install cocoapods

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

gem install mygem --user-install

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.


give the user $whoami to create somethin in those folder

sudo chown -R user /Library/Ruby/Gems/2.0.0

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.


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

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:


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.


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...

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.


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

sudo gem install cocoapods

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


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 guy, 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.


sudo gem update --system
sudo gem install (gemfile)

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

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

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.


cd /Library/Ruby/Gems/2.0.0

open .

right click get info

click lock

place password

make everything read and write.


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
    

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!


Examples related to ruby

Uninitialized Constant MessagesController Embed ruby within URL : Middleman Blog Titlecase all entries into a form_for text field Ruby - ignore "exit" in code Empty brackets '[]' appearing when using .where find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to update Ruby Version 2.0.0 to the latest version in Mac OSX Yosemite? How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? How to update Ruby with Homebrew?

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to rubygems

You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException) How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting You don't have write permissions for the /var/lib/gems/2.3.0 directory Can't install gems on OS X "El Capitan" How to avoid "cannot load such file -- utils/popen" from homebrew on OSX How can I set a proxy server for gem? How to install CocoaPods? Error while installing json gem 'mkmf.rb can't find header files for ruby' How to downgrade or install an older version of Cocoapods