本文翻译自:How to install a gem or update RubyGems if it fails with a permissions error
I'm trying to install a gem using gem install mygem
or update RubyGems using gem update --system
, and it fails with this error: 我正在尝试使用gem install mygem
或使用gem update --system
更新RubyGems,但失败并显示以下错误:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Does anyone have an idea how to solve this? 有谁知道如何解决这个问题?
参考:https://stackoom.com/question/zHzt/如果gem因权限错误而失败-如何安装gem或更新RubyGems
You don't have write permissions into the /Library/Ruby/Gems/1.8 directory. 您没有对/Library/Ruby/Gems/1.8目录的写许可权。
means exactly that, you don't have permission to write there. 就是说,您没有在那儿写的权限。
That is the version of Ruby installed by Apple, for their own use. 这是Apple安装的供自己使用的Ruby版本。 While it's OK to make minor modifications to that if you know what you're doing , because you are not sure about the permissions problem, I'd say it's not a good idea to continue along that track. 如果您知道自己在做什么 , 可以对它进行较小的修改,但是由于您不确定权限问题,所以我认为继续沿着这条路走不是一个好主意。
Instead, I'll strongly suggest you look into using either rbenv or RVM to manage a separate Ruby, installed into a sandbox in your home directory, that you can modify/fold/spindle/change without worrying about messing up the system Ruby. 相反,我强烈建议您考虑使用rbenv或RVM来管理安装在主目录中沙箱中的单独Ruby,以便您可以修改/折叠/旋转/更改而不必担心弄乱系统Ruby。
Between the two, I use rbenv, though I used RVM a lot in the past. 在这两者之间,我使用了rbenv,尽管过去我经常使用RVM。 rbenv takes a more "hands-off" approach to managing your Ruby installation. rbenv采用了一种更为“轻松”的方法来管理Ruby安装。 RVM has a lot of features and is very powerful, but, as a result is more intrusive. RVM具有很多功能并且非常强大,但是因此更具侵入性。 In either case, READ the installation documentation for them a couple times before starting to install whichever you pick. 无论哪种情况,在开始安装之前,请先阅读几次安装文档,然后再选择安装哪种。
The reason of the error is because you are not logged in as the root user on terminal. 该错误的原因是因为您没有以root用户身份在终端上登录。
If you already have root use enable on your mac in terminal type 如果您已经在终端类型的Mac上启用了root使用权限
$ su
If you dont have root user, you need to enable it using the following steps 如果您没有root用户,则需要使用以下步骤启用它
More at the same on http://support.apple.com/kb/ht1528 有关更多信息,请访问http://support.apple.com/kb/ht1528
Atleast it works for me after getting stuck for couple of hours. 卡住几个小时后,它对我有用。
你为什么不这样做:
sudo gem update --system
sudo gem update --system
sudo gem install (gemfile)
You really should be using a Ruby version manager. 您确实应该使用Ruby版本管理器。
Using one properly would prevent and can resolve your permission problem when executing a gem update
command. 正确使用一个可以防止执行执行gem update
命令时的权限问题,并且可以解决该问题。
I recommend rbenv . 我推荐rbenv 。
However, even when you use a Ruby version manager, you may still get that same error message. 但是,即使使用Ruby版本管理器,也可能会收到相同的错误消息。
If you do, and you are using rbenv, just verify that the ~/.rbenv/shims
directory is before the path for the system Ruby. 如果这样做,并且您正在使用rbenv,只需验证~/.rbenv/shims
目录在系统Ruby路径之前即可。
$ echo $PATH
will show you the order of your load path. $ echo $PATH
将显示加载路径的顺序。
If you find that your shims directory comes after your system Ruby bin directory, then edit your ~/.bashrc
file and put this as your last export PATH command: export PATH=$HOME/.rbenv/shims:$PATH
如果发现shims目录export PATH=$HOME/.rbenv/shims:$PATH
系统Ruby bin目录之后,请编辑~/.bashrc
文件,并将其作为最后一个export PATH命令: export PATH=$HOME/.rbenv/shims:$PATH
$ ruby -v
shows you what version of Ruby you are using $ ruby -v
向您显示您正在使用的Ruby版本
This shows that I'm currently using the system version of Ruby (usually not good) 这表明我当前正在使用Ruby的系统版本(通常不好)
$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
$ rbenv global 1.9.3-p448
switches me to a newer, pre-installed version (see references below). $ rbenv global 1.9.3-p448
将我切换到更新的预安装版本(请参见下面的参考)。
This shows that I'm using a newer version of Ruby (that likely won't cause the Gem::FilePermissionError) 这表明我正在使用较新版本的Ruby(这可能不会导致Gem :: FilePermissionError)
$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
You typically should not need to preface a gem command with sudo. 通常,您不需要在sudo之前添加gem命令。 If you feel the need to do so, something is probably misconfigured. 如果您需要这样做,则可能是配置错误。
For details about rbenv see the following: 有关rbenv的详细信息,请参见以下内容: