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

南楼画角 提交于 2019-11-28 06:47:58
Amandeep Singh

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

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.

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

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

First install RVM and then set user gem folder in your .zshrc or .bashrc or else:

export GEM_HOME="$HOME/.gem"

Now gem i LIB will put gems inside safe location.

assuming that rvm is installed you could do something like:

$ rvm install 2.1.1
$ rvm @global do gem install compass

No need to do sudo.

You probably don't have write permissions to somewhere along that path, (most likely /usr/local/lib), you need to change the location of your RVM storage in order to not need sudo

Be aware that if you chose to perform a multi-user install of rvm, you must add each user to the rvm group via

usermod -a -G rvm <USERNAME>

Relevant info from rvm's FAQ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!