ERROR: While executing gem ... (Gem::FilePermissionError)

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

  1. Install RVM
  2. Set the gem folder in your shell resource file. I.e. .zshrc, .bashrc etc
export GEM_HOME="$HOME/.gem"
  1. Install you gem gem i LIBRARY_NAME

To resolve the error:

ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions into the /usr/local/lib/ruby/gems/1.8 directory.

the following solution worked for me:

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

Your global (system-wide) config file probably has the --no-user-install flag set. Create/edit your local ~/.gemrc file and append the following line(s):

:gemdir:
    - ~/.gem/ruby
install: --user-install

Note
The actual directory for the gemdir option will vary depending on your system/desire; it may or may not be needed, but it's probably better to designate the desired installation directory within your home folder (gemdir) than to assume it'll be taken care of by itself.

The ArchLinux Wiki has some useful/well-organized information pertaining to this.


chruby

Alternatively, similar to the rvm solution already suggested, you can try using chruby to maintain, configure, and use local versions of ruby. To install additional versions of ruby, however, you need to use ruby-install.

$ brew install chruby ruby-install
$ ruby-install ruby 2.4.0

$ echo "source /usr/local/opt/chruby/share/chruby/chruby.sh" >> ~/.bash_profile
$ echo "chruby ruby" >> ~/.bash_profile

# Do this to select the default alternative ruby installation, assuming
# there is no other version installed.
$ chruby ruby

## Otherwise, list the available ruby versions installed, and select
$ chruby
ruby-2.3.0
ruby-2.4.0

$ chruby ruby-2.4.0

From the project's README:

Changes the current Ruby. Features

Updates $PATH.
    Also adds RubyGems bin/ directories to $PATH.
Correctly sets $GEM_HOME and $GEM_PATH.
    Users: gems are installed into ~/.gem/$ruby/$version.
    Root: gems are installed directly into /path/to/$ruby/$gemdir.
Additionally sets $RUBY_ROOT, $RUBY_ENGINE, $RUBY_VERSION and $GEM_ROOT.
Optionally sets $RUBYOPT if second argument is given.
Calls hash -r to clear the command-lookup hash-table.
Fuzzy matching of Rubies by name.
Defaults to the system Ruby.
Optionally supports auto-switching and the .ruby-version file.
Supports bash and zsh.
Small (~100 LOC).
Has tests.

Anti-Features

Does not hook cd.
Does not install executable shims.
Does not require Rubies be installed into your home directory.
Does not automatically switch Rubies by default.
Does not require write-access to the Ruby directory in order to install gems.

Requirements

bash >= 3 or zsh

If you have installed rvm as the root user, then you must sudo to install anything.

You can install rvm in user-mode as a regular user, in which case everything is stored in your home directory and no sudo is required.

If you are not committed to this particular install, type sudo rvm implode and start over.


If you have installed rbenv & facing this error, below answer will help (this is how it worked for me)

Issue:

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

DO NOT USE SUDO

  1. check the rbenv versions
$ rbenv versions
* system (set by /Users/user/.rbenv/version)
  2.3.8

Here, you can see, by default it selected system. change it to your ruby version.

$ rbenv global 2.3.8
$ rbenv versions
  system
* 2.3.8 (set by /Users/user/.rbenv/version)

Then try installing the bundler, it will work:

$gem install bundler -v 1.17.3
Fetching: bundler-1.17.3.gem (100%)
Successfully installed bundler-1.17.3
Parsing documentation for bundler-1.17.3
Installing ri documentation for bundler-1.17.3
Done installing documentation for bundler after 5 seconds
1 gem installed