我试图使用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.
有人知道怎么解决这个问题吗?
我试图使用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.
有人知道怎么解决这个问题吗?
当前回答
在MacOS Mojave上成功测试:
卸载所有旧的ruby版本(假设你有2.00和2.3.0): $ RVM卸载2.0.0 $ RVM卸载2.3.0 安装全新的ruby版本: 安装ruby 设置默认别名为您的版本: $ RVM别名创建默认ruby 重新启动您的系统,因为这是您的计算机加载最新安装的新ruby版本的最安全的方式。
完成以上步骤后,您可以成功地运行任何gem命令。
其他回答
您确实应该使用Ruby版本管理器。
在执行gem更新命令时,正确使用它可以防止并解决权限问题。
我推荐rbenv。
但是,即使使用Ruby版本管理器,仍然可能会得到相同的错误消息。
如果这样做,并且使用的是rbenv,只需验证~/。rbenv/shims目录在系统Ruby路径之前。
$ echo $PATH将显示加载路径的顺序。
如果您发现shims目录位于系统Ruby bin目录之后,则编辑~/。export PATH=$HOME/.rbenv/shims:$PATH . bashrc文件,并将其作为最后一个导出PATH命令
$ ruby -v显示你正在使用的ruby版本
这表明我目前使用的是Ruby的系统版本(通常不太好)
$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
$ rbenv global 1.9.3-p448将我切换到更新的预安装版本(参见下面的参考资料)。
这表明我正在使用一个更新版本的Ruby(这可能不会导致Gem::FilePermissionError)
$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
通常不需要在gem命令前加上sudo。如果您觉得需要这样做,则可能是配置错误。
关于rbenv的详细信息请参见:
https://github.com/sstephenson/rbenv http://robots.thoughtbot.com/post/47273164981/using-rbenv-to-manage-rubies-and-gems
该错误的原因是您没有以root用户登录终端。
如果你已经在终端类型的mac上启用了根用户
$ su
如果您没有root用户,您需要使用以下步骤启用它
From the Apple menu choose System Preferences…. From the View menu choose Users & Groups. Click the lock and authenticate as an administrator account. Click Login Options…. Click the “Edit…” or “Join…” button at the bottom right. Click the “Open Directory Utility…” button. Click the lock in the Directory Utility window. Enter an administrator account name and password, then click OK. Choose Enable Root User from the Edit menu. Enter the root password you wish to use in both the Password and Verify fields, then click OK.
更多内容请登录http://support.apple.com/kb/ht1528
在被困了几个小时后,至少它对我有用。
正如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...
你需要修正你的路径。
要确定此修复是否有效,请运行以下命令:
which gem
这应该会输出一个你没有权限的目录:
/usr/bin/gem
要修复此问题,请执行以下步骤:
Determine the path you need to copy to your profile: rbenv init - The first line of the output is the line you need to copy over to your profile: export PATH="/Users/justin/.rbenv/shims:${PATH}" #path that needs to be copied source "/usr/local/Cellar/rbenv/0.4.0/libexec/../completions/rbenv.zsh" rbenv rehash 2>/dev/null rbenv() { typeset command command="$1" if [ "$#" -gt 0 ]; then shift fi case "$command" in rehash|shell) eval `rbenv "sh-$command" "$@"`;; *) command rbenv "$command" "$@";; esac } Copy the path to your profile and save it. Reload your profile (source ~/.zshenv for me). Run rbenv rehash.
现在当你运行哪个gem时,你应该得到一个你有权限的本地路径:
/Users/justin/.rbenv/shims/gem
更老更睿智
不要做我在这里说的,只要知道在你使用sudo的任何时候都要小心。您可能希望使用类似rbenv的东西来隔离您正在做的任何工作。
的一种方式
了解chown
我不知道您是否喜欢命令行,但这将使使用任何工具在任何项目中安装包到您的系统变得轻而易举。
据我所知,chown代表着改变所有权。
我来寻找这个答案的原因是因为gem安装今天向我抛出了这个错误:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /var/lib/gems/1.9.1 directory.
这是一个使用口香糖的绝佳机会。你看,Ruby已经给了我们它需要访问的目录,而且它似乎会经常使用这个目录。
在这种情况下,要解决这个问题只需要知道三件事,但是chown要强大得多,并且比我现在要演示的灵活性要大得多。更多信息请参考下面的资料。
两件事
用户名 目录
如果您在shell中查找用户名很容易。看看提示就知道了。我的是这样的:
breadly@breadly-desktop:~\Desktop
当前用户只是@前面的名字。我们从错误消息中知道了目录,但是您有两种选择。您可以使用../gems/1.9.1将权限限制在当前版本,或者使用../gems给自己所有版本的gems写权限。
实际更改所有权的命令如下所示。
chown -R $(whoami) /absolute/path/to/directory
-R被称为一个标志,-R标志通常告诉命令递归地执行某项操作,换句话说,对目录中包含的所有内容执行该命令,以及该目录中包含的所有内容,等等,直到没有其他内容为止。