ruby 如果因权限错误而失败,如何安装 gem 或更新 RubyGems

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14607193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:42:47  来源:igfitidea点击:

How to install a gem or update RubyGems if it fails with a permissions error

rubymacosrubygems

提问by Gaurav

I'm trying to install a gem using gem install mygemor update RubyGems using gem update --system, and it fails with this error:

我正在尝试使用gem install mygemRubyGems安装 gem或更新 RubyGems gem update --system,但失败并显示以下错误:

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?

有谁知道如何解决这个问题?

采纳答案by the Tin Man

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

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

Instead, I'll strongly suggest you look into using either rbenvor RVMto 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.

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

Between the two, I use rbenv, though I used RVM a lot in the past. rbenv takes a more "hands-off" approach to managing your Ruby installation. RVM has a lot of features and is very powerful, but, as a result is more intrusive. In either case, READthe installation documentation for them a couple times before starting to install whichever you pick.

在两者之间,我使用 rbenv,尽管我过去经常使用 RVM。rbenv 采用更“不干涉”的方法来管理您的 Ruby 安装。RVM 有很多特性并且非常强大,但结果是更具侵入性。在任何一种情况下,在开始安装您选择的任何一个之前,请阅读它们的安装文档几次。

回答by nathanwhy

Try adding --user-installinstead of using sudo:

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

gem install mygem --user-install

回答by l3x

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 updatecommand.

正确使用一个可以防止并可以解决您在执行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/shimsdirectory is before the path for the system Ruby.

如果这样做,并且您正在使用 rbenv,只需验证该~/.rbenv/shims目录是否位于系统 Ruby 的路径之前。

$ echo $PATHwill 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 ~/.bashrcfile and put this as your last export PATH command: export PATH=$HOME/.rbenv/shims:$PATH

如果您发现您的 shims 目录位于您的系统 Ruby bin 目录之后,请编辑您的~/.bashrc文件并将其作为最后一个导出 PATH 命令: export PATH=$HOME/.rbenv/shims:$PATH

$ ruby -vshows 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-p448switches 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. If you feel the need to do so, something is probably misconfigured.

您通常不需要在 gem 命令前加上 sudo。如果您觉得有必要这样做,则可能是某些配置错误。

For details about rbenv see the following:

有关 rbenv 的详细信息,请参阅以下内容:

回答by Bobz

Why don't you do:

你为什么不这样做:

sudo gem update --system

回答by Fmessina

This will fix the issue on MacOs Mojaveand MacOs Catalinain a clean way:

这将以干净的方式解决MacOs MojaveMacOs Catalina上的问题:

brew install ruby

Then set GEM_HOME to your user directory. On the terminal:

然后将 GEM_HOME 设置为您的用户目录。在终端上:

Bash:

Bash

echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME=$HOME/gems' >> ~/.bashrc
echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.bashrc
source ~/.bashrc

OR if on Zsh:

或者如果打开Zsh

echo '# Install Ruby Gems to ~/gems' >> ~/.zshrc
echo 'export GEM_HOME=$HOME/gems' >> ~/.zshrc
echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.zshrc
source ~/.zshrc

回答by Hendrik

For me the problem was due to using rbenvand forgetting to set the proper version globally.

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

So I had to set it with rbenv global xxx

所以我不得不设置它 rbenv global xxx

In my case I installed 2.0.0-p247so I had to issue the command:

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

rbenv global 2.0.0-p247
rbenv rehash

Then all was working fine.

然后一切正常。

回答by Justin Leveck

You need to correct your paths.

你需要更正你的路径。

To determine if this fix will work, run the following:

要确定此修复程序是否有效,请运行以下命令:

which gem

This should output a directory you do not have permissions to:

这应该输出一个您无权访问的目录:

/usr/bin/gem

To fix this perform the following steps:

要解决此问题,请执行以下步骤:

  1. 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=""
      if [ "$#" -gt 0 ]; then
        shift
      fi
    
      case "$command" in
      rehash|shell)
        eval `rbenv "sh-$command" "$@"`;;
      *)
        command rbenv "$command" "$@";;
      esac
    }
    
  2. Copy the path to your profile and save it.

  3. Reload your profile (source ~/.zshenvfor me).

  4. Run rbenv rehash.

  1. 确定需要复制到个人资料的路径:

    rbenv init -
    

    输出的第一行是您需要复制到您的个人资料的行:

    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=""
      if [ "$#" -gt 0 ]; then
        shift
      fi
    
      case "$command" in
      rehash|shell)
        eval `rbenv "sh-$command" "$@"`;;
      *)
        command rbenv "$command" "$@";;
      esac
    }
    
  2. 将路径复制到您的个人资料并保存。

  3. 重新加载您的个人资料(source ~/.zshenv对我而言)。

  4. 运行rbenv rehash

Now when you run which gemyou should get a local path that you have permissions to:

现在,当您运行时,which gem您应该获得一个您有权执行的本地路径:

/Users/justin/.rbenv/shims/gem

回答by Eliot Arntz

sudo gem update --system
sudo gem install (gemfile)

回答by thebiggestlebowski

This worked for me. Plus, if you installed gems as root before, it fixes that problem by changing ownership back to you (better security-wise).

这对我有用。另外,如果您之前以 root 身份安装了 gems,它会通过将所有权改回给您来解决该问题(更好的安全性)。

sudo chown -R `whoami` /Library/Ruby/Gems

回答by JezC

There are two routes: Use either rbenv or RVM. There are recipes for both below. Before you do, you probably want to turn off the installation of local documents for gems.

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

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

Then:

然后:

install rbenv

install rbenv

install ruby-build

install ruby-build

run:

跑:

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

This installs an up-to-date version of the gem system in your local directories. That means you don't interfere with the system configuration. If you're asking this question, you shouldn't be messing with system security, and you'll spend longer understanding what issues you may run into, than just having an easy way to avoid the problem you started with. Learn InfoSec later, when you know more about the operating system and programming.

这会在您的本地目录中安装最新版本的 gem 系统。这意味着您不会干扰系统配置。如果你问这个问题,你不应该搞乱系统安全,你会花更长的时间来了解你可能遇到的问题,而不是仅仅有一个简单的方法来避免你开始遇到的问题。稍后,当您对操作系统和编程有更多了解时,再学习 InfoSec。

For an alternative use 'RVM' instead: To install rvmrun:

对于替代使用“RVM”:要安装 rvm,请运行:

rvm install 2.1.2
rvm use 2.1.2
gem update --system

This has the same result, you end up with a local Ruby and Gem system that doesn't interfere with the system versions. There is no need for Homebrew, or over-riding system libs, etc.

这具有相同的结果,您最终会得到一个不会干扰系统版本的本地 Ruby 和 Gem 系统。不需要 Homebrew 或覆盖系统库等。