错误:执行gem时出错...(Gem :: FilePermissionError)

79

我查看了所有类似的答案,但没有一个完全符合我的情况,也没有任何一种解决方案对我有效。

gem environmentsudo gem environment 的结果相同:

RubyGems Environment:
  - RUBYGEMS VERSION: 1.5.3
  - RUBY VERSION: 1.8.7 (2011-12-28 patchlevel 357) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.8
     - /home/ava/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

rvm -vrvm 1.22.3

ruby -vruby 1.8.7

OSX 10.8.4

echo $PATH /usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/home/ava/.rvm/bin:/home/ava/bin

gem install <gem-name> 的结果为

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

我可以通过sudo安装相同的软件,但为什么不能直接安装?我做错了什么吗?

更新:

根据评论和这篇文章,我运行了以下命令:

rvm implode 然后重新安装稳定版本。 rvm install 1.9.3 或其他任何Ruby版本的安装都会失败。

Error running '__rvm_make -j24',
please read /home/ava/.rvm/log/log/1378418790_ruby-1.9.3-p194/make.log
There has been an error while running make. Halting the installation.

make.log

    [2013-09-05 22:06:48] make
current path: /home/ava/.rvm/src/ruby-1.9.3-p194
command(2): make -j24
        CC = gcc
        LD = ld
        LDSHARED = gcc -shared
        CFLAGS = -O3 -ggdb -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Werror-implicit-function-declaration  -fPIC
        XCFLAGS = -include ruby/config.h -include ruby/missing.h -fvisibility=hidden -DRUBY_EXPORT
        CPPFLAGS =   -I. -I.ext/include/x86_64-linux -I./include -I.
        DLDFLAGS = -Wl,-soname,libruby.so.1.9
        SOLIBS = -lpthread -lrt -ldl -lcrypt -lm
compiling main.c
compiling dmydln.c
compiling dmyencoding.c
compiling version.c
compiling miniprelude.c
compiling array.c
compiling bignum.c
compiling class.c
compiling compar.c
compiling complex.c
compiling dir.c
compiling dln_find.c
compiling enum.c
compiling enumerator.c
compiling error.c
compiling eval.c
compiling load.c
compiling proc.c
compiling file.c
:
:
:
In file included from ossl.h:213,
                 from ossl_pkcs5.c:5:
openssl_missing.h:71: error: conflicting types for ‘HMAC_CTX_copy’
/usr/local/include/openssl/hmac.h:102: error: previous declaration of ‘HMAC_CTX_copy’ was here
openssl_missing.h:95: error: conflicting types for ‘EVP_CIPHER_CTX_copy’
/usr/local/include/openssl/evp.h:459: error: previous declaration of ‘EVP_CIPHER_CTX_copy’ was here
make[2]: *** [ossl_pkcs5.o] Error 1
In file included from ossl.h:213,
                 from ossl_x509req.c:11:
:
:
:

rvm listwhich ruby 的输出是什么?基本上你的电脑上安装了两个 Ruby 1.8,你需要使用安装在 /home/ava/.gem/ruby/1.8 路径下的那个,而不是 /usr/local/lib/ruby/gems/1.8。设置 rvm 使用正确的 Ruby 应该可以解决这个问题。 - Lavixu
rvm列表ext-ree-1.8.7-2011.12所使用的ruby是/usr/local/bin/ruby - Ava
尝试执行以下命令: rvm remove ext-ree-1.8.7-2011.12 . rvm install 1.8.2 . 然后使用 rvm use '新安装的Ruby版本' --default 设置默认Ruby版本。 - Lavixu
rvm install 1.8.2 失败,并出现以下错误提示:http://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.2.tar.gzruby-1.8.2.tar.gz 没有校验和,因此无法验证。 - Ava
我只是在遵循上面的评论。我想安装1.8.7,但是遇到了更新答案中提到的错误。 - Ava
显示剩余2条评论
19个回答

122

错误:执行gem命令时出错...(Gem::FilePermissionError) 您没有对 /Library/Ruby/Gems/2.3.0 目录的写入权限。

  1. 安装RVM(可选)
  2. 在您的shell资源文件中设置gem目录,例如 .zshrc.bashrc 等。
export GEM_HOME="$HOME/.gem"
  1. 安装你的gem包 gem i LIBRARY_NAME

3
在MacOSX High Sierra上为我工作。 - abkrim
1
对于使用 RVM 的人来说,这是一个安全使用系统 Ruby 的好方法。不要忘记将 $HOME/.gem/bin 添加到您的路径中。 - cyber_dave
我无法安装LIB,它到底是什么?没有安装LIB,它却完美地工作了。 - roger
如果你需要在shell会话期间暂时解决问题,可以将@AlexanderSaenko放入Ternimal中。如果你需要永久解决问题,请将该命令设置到你的shell配置中。 - dimpiax
1
@StefanKrüger 不知道,但如果谈论 gem 的话,我相信 ~/.gem 文件夹更合适。 - dimpiax
显示剩余4条评论

60

解决错误:

错误:执行 gem 时出错... (Gem::FilePermissionError) 您没有写入 /usr/local/lib/ruby/gems/1.8 目录的权限。

以下解决方法适用于我:

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

10
使用sudo安装Ruby gem吗?不确定是否有必要,更不用说从安全和稳定性等方面来看是否明智了。 - ILMostro_7
10
永远不要使用sudo安装依赖! - Iulian Onofrei
3
我不知道为什么这个答案有很多赞,最佳实践是永远不要使用sudo安装gems。 - Adriano Resende
1
只需按照另一个答案所建议的方法添加~/.gemrc文件即可,这对我来说是可行的,无需使用sudo - Nagev
@Nagev 这对我解决了问题。我在Catalina上。 - LondonGuy

45

你的全局系统配置文件可能已经设置了 --no-user-install 标志。 创建/编辑本地的 ~/.gemrc 文件并添加以下行:

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

注意
gemdir选项的实际目录取决于您的系统/希望,可能需要也可能不需要,但最好指定所需安装目录在您的home文件夹(gemdir)中,而不是假设它会自动处理。

ArchLinux Wiki提供了一些有用/组织良好的信息。


chruby

或者,与已建议的rvm解决方案类似,您可以尝试使用chruby来维护、配置和使用本地版本的ruby。然而,要安装其他版本的ruby,您需要使用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

来自项目的 README:

更改当前的 Ruby。功能包括:

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.

反功能特性

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.

要求

bash >= 3 or zsh

非常感谢!chruby 对我很有用,我终于能够在我的 Mac 上重新安装 bundler 了,而不会因为 Ruby 版本不匹配而导致讨厌的 PermissionDenied 错误!!谢谢 :).. 你知道吗,一般来说,在 MacOS 上更新 Ruby 版本是个好主意吗?如果是,你更喜欢 chruby 还是 ruby-install - iKK
你可以使用 ruby-install 安装新版本的 Ruby。然后,使用 chruby 切换到新版本。无需处理项目的系统范围内的 Ruby 版本。 - ILMostro_7
~/.gemrc 的编辑解决方案对我很有效。谢谢! - wzso

26
如果您以root用户身份安装了rvm,那么必须使用sudo来安装任何东西。
您可以以普通用户的身份在用户模式下安装rvm,这样所有内容都存储在您的家目录中,不需要sudo
如果您不想继续进行此特定安装,请键入sudo rvm implode并重新开始。

@IulianOnofrei 这是为了删除RVM,而不是安装依赖项。如果它是以root身份安装的,则必须以root身份卸载。 - tadman
"sudo gem install xcodeproj" 对我有效。谢谢! - rooxane.mac
"sudo gem install xcodeproj" 对我有效 谢谢! - undefined

13

Macbook M1和更新版本:

打开终端 curl -L https://get.rvm.io | bash -s stable

重新打开终端

rvm install ruby-3.1.1 
rvm use ruby-3.1.1 
rvm --default use 3.1.1

然后,如预期安装了CocoaPods。

sudo gem install cocoapods

sudo gem install cocoapods 可以正常工作,但 bundle install 无法正常工作。 - Deepak Ghadi

8
如果您已安装了rbenv并且遇到此错误,请查看下面的答案(这是我的解决方法)。
问题:
$ gem install bundler
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

请勿使用sudo

  1. 检查rbenv版本
$ rbenv versions
* system (set by /Users/user/.rbenv/version)
  2.3.8

在这里,默认情况下它选择了系统。将其更改为您的 Ruby 版本。

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

接下来尝试安装bundler,它会工作的:

$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

看起来这也是必需的:https://dev59.com/4WMl5IYBdhLWcg3wMkko#67115739 - Jonny

6
这里提到的“写入权限”错误和Gem::FilePermissionError是由于Ruby环境配置不当而引起的。无论在Stack Overflow上看到多少次将sudo用作安装gems的可接受答案,您都不应该需要使用它。这是原因
从高层次来看,在Mac上设置适当的Ruby开发环境涉及六个步骤:
  1. 安装Homebrew(也会安装必备的Apple命令行工具)
  2. 安装Ruby管理器(如chruby、rbenv、asdf、RVM)- 大多数可以使用Homebrew安装
  3. 通过向shell文件(~/.bash_profile~/.zshrc)添加适当的行来配置Ruby管理器 - 每个管理器都有执行此操作的说明,这是人们通常忽略的一步
  4. 重新启动终端(或打开新选项卡)以使shell变更生效 - 这是另一个经常被忽略的步骤
  5. 使用Ruby管理器安装特定版本的Ruby
  6. 使用Ruby管理器切换到该版本的Ruby
我个人偏好chrubyruby-install。有关更多细节和可以自动化整个过程的脚本,请查看我的回答:https://dev59.com/llUK5IYBdhLWcg3w4jQv#54873916

对于 rbenv 用户而言,一般情况是由于您没有安装版本(终端命令 open ~/.rbenv/versions/ 应该会显示每个已安装版本的文件夹),或者您没有全局/本地设置要使用的版本(例如,rbenv global 3.1.0)。其他可能的原因是未能正确配置您的 shims 路径。 - Allison

4
假设已经安装了rvm,您可以执行以下操作:
$ rvm install 2.1.1
$ rvm @global do gem install compass

无需使用sudo。


2
如果您使用的是rbenv,那么可能您忘记启用它了,例如:
> rbenv init
# Load rbenv automatically by appending
# the following to ~/.zshrc:

eval "$(rbenv init -)"

0
如果您刚刚安装了rvm,并且遇到以下错误:

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

请记得调用:
source ~/.bash_profile

确保gems已安装在~/.rvm中

然后你就可以简单地执行

rvm use 2.6.6
gem install yourgem:2.0.1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接