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?


Solution 1:

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

gem install mygem --user-install

Solution 2:

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.

Solution 3:

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:

  • https://github.com/sstephenson/rbenv
  • http://robots.thoughtbot.com/post/47273164981/using-rbenv-to-manage-rubies-and-gems

Solution 4:

Why don't you do:

sudo gem update --system

Solution 5:

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