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

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

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

gem install mygem --user-install

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.