我试图使用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.

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


当前回答

检查你的Ruby版本是否正确。如果不是,那就改变它。

这对我来说很管用:

$ rbenv global 1.9.3-p547
$ gem update --system

其他回答

有两种路由:使用rbenv或RVM。下面是这两种食物的食谱。在此之前,您可能希望关闭gem本地文档的安装。

echo "gem: --no-ri --no-rdoc" >> ~/.gemrc

然后:

安装rbenv

安装ruby-build

运行:

rbenv install 2.1.2 (or whatever version you prefer)
rbenv global 2.1.2
gem update --system

这将在本地目录中安装gem系统的最新版本。这意味着您不会干扰系统配置。如果您正在问这个问题,那么您不应该破坏系统安全性,并且您将花费更长的时间来了解可能遇到的问题,而不仅仅是找到一种简单的方法来避免您开始时遇到的问题。了解更多关于操作系统和编程的知识后,再学习InfoSec。

使用'RVM'替代:

rvm install 2.1.2
rvm use 2.1.2
gem update --system

这有同样的结果,您最终得到的是一个本地Ruby和Gem系统,它不会影响系统版本。不需要Homebrew,也不需要覆盖系统库等等。

检查你的Ruby版本是否正确。如果不是,那就改变它。

这对我来说很管用:

$ rbenv global 1.9.3-p547
$ gem update --system

在MacOS Mojave上成功测试:

卸载所有旧的ruby版本(假设你有2.00和2.3.0): $ RVM卸载2.0.0 $ RVM卸载2.3.0 安装全新的ruby版本: 安装ruby 设置默认别名为您的版本: $ RVM别名创建默认ruby 重新启动您的系统,因为这是您的计算机加载最新安装的新ruby版本的最安全的方式。

完成以上步骤后,您可以成功地运行任何gem命令。

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

gem install mygem --user-install

正如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...