我试图使用gem install mygem或使用gem update——system更新RubyGems安装一个gem,它失败了,错误如下:

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

有人知道怎么解决这个问题吗?


当前回答

尝试添加——user-install而不是使用sudo:

gem install mygem --user-install

其他回答

对我来说,问题是由于使用rbenv,忘记全局设置正确的版本。

所以我必须用rbenv global xxx来设置它

在我的情况下,我安装了2.0.0-p247,所以我必须发出命令:

rbenv global 2.0.0-p247
rbenv rehash

然后一切都运转正常。

我需要做一个rbenv rehash,这样它就会指向我的本地Gem库。

看起来您已经让您的gem管理器指向系统库,因此,与其混淆权限,不如为您的管理器执行等效的“rehash”以获得指向本地的内容。

我找到了这个无苏多尔宝石的指南:

Brew install rbenv ruby-build Sudo gem update—system 向.bashrc添加导出: export RBENV_ROOT="$(brew -prefix rbenv)" export GEM_HOME="$(brew -prefix)/opt/gems" export GEM_PATH="$(brew -prefix)/opt/gems" 最后将这个添加到~/.gemrc中: 宝石:- n / usr /地方/ bin Gem更新——system

你没有写/Library/Ruby/Gems/1.8目录的权限。

也就是说,你没有权限在那里写东西。

这是苹果安装的Ruby版本,供他们自己使用。虽然如果您知道自己在做什么,可以对其进行一些小的修改,因为您不确定权限问题,但我认为继续这样做并不是个好主意。

相反,我强烈建议您考虑使用rbenv或RVM来管理一个单独的Ruby,将其安装到您的主目录中的沙箱中,这样您就可以修改/折叠/主轴/更改,而不必担心会弄乱系统Ruby。

在这两者之间,我使用rbenv,尽管我过去经常使用RVM。rbenv采用了一种更加“不干涉”的方法来管理Ruby安装。RVM有很多特性并且非常强大,但是,结果是更具有侵入性。无论哪种情况,在开始安装之前,都要多阅读几次安装文档。

正如bobbdelsol所指出的,rehash对我来说很管用:

==> which ruby
/usr/bin/ruby

==> rbenv install 1.9.3-p551
Downloading ruby-1.9.3-p551.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p551.tar.bz2
Installing ruby-1.9.3-p551...
Installed ruby-1.9.3-p551 to /Users/username/.rbenv/versions/1.9.3-p551


==> which ruby
/Users/username/.rbenv/shims/ruby

==> which gem
/Users/username/.rbenv/shims/gem

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


==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]


==> rbenv global 1.9.3-p551


==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]


==> rbenv global 1.9.3-p551


==> rbenv rehash


==> ruby -v
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin15.4.0]


==> gem install compass
Fetching: sass-3.4.22.gem (100%)
Fetching: multi_json-1.11.3.gem (100%)
Fetching: compass-core-1.0.3.gem (100%)
Fetching: compass-import-once-1.0.5.gem (100%)
Fetching: chunky_png-1.3.5.gem (100%)
Fetching: rb-fsevent-0.9.7.gem (100%)
Fetching: ffi-1.9.10.gem (100%)
Building native extensions.  This could take a while...
Fetching: rb-inotify-0.9.7.gem (100%)
Fetching: compass-1.0.3.gem (100%)
    Compass is charityware. If you love it, please donate on our behalf at http://umdf.org/compass Thanks!
Successfully installed sass-3.4.22
Successfully installed multi_json-1.11.3
Successfully installed compass-core-1.0.3
Successfully installed compass-import-once-1.0.5
Successfully installed chunky_png-1.3.5
Successfully installed rb-fsevent-0.9.7
Successfully installed ffi-1.9.10
Successfully installed rb-inotify-0.9.7
Successfully installed compass-1.0.3
9 gems installed
Installing ri documentation for sass-3.4.22...
Installing ri documentation for multi_json-1.11.3...
Installing ri documentation for compass-core-1.0.3...
Installing ri documentation for compass-import-once-1.0.5...
Installing ri documentation for chunky_png-1.3.5...
Installing ri documentation for rb-fsevent-0.9.7...
Installing ri documentation for ffi-1.9.10...
Installing ri documentation for rb-inotify-0.9.7...
Installing ri documentation for compass-1.0.3...
Installing RDoc documentation for sass-3.4.22...
Installing RDoc documentation for multi_json-1.11.3...
Installing RDoc documentation for compass-core-1.0.3...
Installing RDoc documentation for compass-import-once-1.0.5...
Installing RDoc documentation for chunky_png-1.3.5...
Installing RDoc documentation for rb-fsevent-0.9.7...
Installing RDoc documentation for ffi-1.9.10...
Installing RDoc documentation for rb-inotify-0.9.7...
Installing RDoc documentation for compass-1.0.3...