ruby 为什么在安装 gem 时会出现“权限被拒绝”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17550903/
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 06:04:00  来源:igfitidea点击:

Why do I get a "permission denied" error while installing a gem?

rubymacosjekyll

提问by okTalk

I'm attempting to install Jekyll. After running gem install jekyllI get this error:

我正在尝试安装 Jekyll。运行后gem install jekyll我收到此错误:

ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied - /usr/local/lib/ruby/gems/2.0.0/gems/jekyll-1.0.3/CONTRIBUTING.md

I can see that Jekyll is installed when I run gem listso I'm thoroughly confused:

我可以看到运行时安装了 Jekyll,gem list所以我很困惑:

*** LOCAL GEMS ***

bigdecimal (1.2.0)
classifier (1.3.3)
colorator (0.1)
commander (4.1.3)
directory_watcher (1.4.1)
fast-stemmer (1.0.2)
highline (1.6.19)
io-console (0.4.2)
jekyll (1.0.3)
json (1.7.7)
kramdown (1.0.2)
liquid (2.5.0)
maruku (0.6.1)
minitest (4.3.2)
posix-spawn (0.3.6)
psych (2.0.0)
pygments.rb (0.5.1)
rake (0.9.6)
rdoc (4.0.0)
rubygems-update (2.0.3)
safe_yaml (0.7.1)
syntax (1.0.0)
test-unit (2.0.0.0)
yajl-ruby (1.1.0)

I've had a lot of problems with my user paths in the past, so I'm wondering if this error could have something to do with that?

过去我的用户路径有很多问题,所以我想知道这个错误是否与此有关?

Here is the output of gem env:

这是输出gem env

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.3
  - RUBY VERSION: 2.0.0 (2013-02-24 patchlevel 0) [x86_64-darwin12.3.0]
  - INSTALLATION DIRECTORY: /usr/local/Cellar/ruby/2.0.0-p0/lib/ruby/gems/2.0.0
  - RUBY EXECUTABLE: /usr/local/Cellar/ruby/2.0.0-p0/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/Cellar/ruby/2.0.0-p0/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-12
  - GEM PATHS:
     - /usr/local/Cellar/ruby/2.0.0-p0/lib/ruby/gems/2.0.0
     - /Users/me/.gem/ruby/2.0.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/

Here is my ".bash_profile":

这是我的“.bash_profile”:

export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:above/path/to/gems

source ~/.rvm/scripts/rvm

Could somebody please help me get Jekyll installed, or at least get past this permissions error?

有人可以帮我安装 Jekyll,或者至少解决这个权限错误吗?

回答by the Tin Man

Your Ruby is installed in /usr/local/Cellar/ruby/....

您的 Ruby 安装在/usr/local/Cellar/ruby/....

That is a restricted path and can only be written to when you use elevated privileges, either by running as root or by using sudo. I won't recommend you run things as root since you don't understand how paths and permissions work. You canuse sudo gem install jekyll, which will temporarily elevate your permissions, giving your command the rights needed to write to that directory.

这是一个受限制的路径,只能在您使用提升的权限时写入,以 root 身份运行或使用sudo. 我不建议您以 root 身份运行,因为您不了解路径和权限的工作原理。您可以使用sudo gem install jekyll,这将暂时提升您的权限,为您的命令提供写入该目录所需的权限。

However, I'd recommend you give serious thought into NOT doing that, and instead use your RVM to install Ruby into your own home directory, where you'll automatically be able to install Rubies and gems without permission issues. See the directions for installing into a local RVM sandbox in "Single-User installations".

但是,我建议您认真考虑不要这样做,而是使用您的 RVM 将 Ruby 安装到您自己的主目录中,在那里您将能够在没有权限问题的情况下自动安装 Ruby 和 gem。请参阅“单用户安装”中有关安装到本地 RVM 沙箱的说明

Because you have RVM in your ~/.bash_profile, but it doesn't show up in your Gem environment listing, I suspect you either haven't followed the directions for installing RVM correctly, or you haven't used the all-important command:

因为你有 RVM ~/.bash_profile,但它没有出现在你的 Gem 环境列表中,我怀疑你要么没有按照正确安装 RVM 的说明进行操作,要么你没有使用最重要的命令:

rvm use 2.0.0 --default

to configure a default Ruby.

配置默认的 Ruby。

For most users, the "Single-User installation" is the way to go. If you have to use sudowith that configuration you've done something wrong.

对于大多数用户来说,“单用户安装”是要走的路。如果您必须使用sudo该配置,那么您就做错了。

回答by Mike Lane

I wanted to share the steps that I followed that fixed this issue for me in the hopes that it can help someone else (and also as a reminder for me in case something like this happens again)

我想分享我为我解决这个问题所遵循的步骤,希望它可以帮助其他人(也作为提醒我,以防再次发生类似的事情)

The issues I'd been having (which were the same as OP's) may have to do with using homebrewto install Ruby.

我遇到的问题(与 OP 相同)可能与使用自制软件安装 Ruby 有关。

To fix this, first I updated homebrew:

为了解决这个问题,首先我更新了自制软件:

brew update && brew upgrade
brew doctor

(If brew doctor comes up with any issues, fix them first.) Then I uninstalled ruby

(如果 brew doctor 出现任何问题,请先修复它们。)然后我卸载了 ruby

brew uninstall ruby

If rbenv is NOTinstalled at this point, then

如果rbenv是不是在这一点上安装的话,

brew install rbenv
brew install ruby-build
echo 'export RBENV_ROOT=/usr/local/var/rbenv' >> ~/.bash_profile
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile

Then I used rbenvto install ruby. First, find the desired version:

然后我使用rbenv安装ruby。首先,找到所需的版本:

rbenv install -l

Install that version (e.g. 2.2.2)

安装该版本(例如 2.2.2)

rbenv install 2.2.2

Then set the global version to the desired ruby version:

然后将全局版本设置为所需的 ruby​​ 版本:

rbenv global 2.2.2

At this point you should see the desired version set for the following commands:

此时,您应该会看到为以下命令设置的所需版本:

rbenv versions

and

ruby --version

Now you should be able to install bundler:

现在你应该可以安装 bundler:

gem install bundler

And once in the desired project folder, you can install all the required gems:

一旦进入所需的项目文件夹,您就可以安装所有必需的 gem:

bundle
bundle install

回答by Bryan Norden

Seems like a permissions issue. This is what worked for me

好像是权限问题。这对我有用

sudo chown -R $(whoami) /Library/Ruby/Gems/*

or in your case

或者在你的情况下

sudo chown -R $(whoami) /usr/local/lib/ruby/gems/2.0.0/gems/

What does this do:

这是做什么的:

This is telling the system to change the files to change the ownership to the current user. Something must have gotten messed up when something got installed. Usually this is because there are multiple accounts or users are using sudo to install when they should not always have to.

这告诉系统更改文件以将所有权更改为当前用户。当某些东西被安装时,一定是有些东西搞砸了。通常这是因为有多个帐户或用户在不应该总是必须使用 sudo 进行安装时。

回答by 030

After setting the gems directory to the user directory that runs the gem install, using export GEM_HOME=/home/<user>/gems, the issue has been solved.

将 gems 目录设置为运行gem install, using的用户目录后,export GEM_HOME=/home/<user>/gems问题已解决。

回答by David Ben Dahan

I had the same problem using rvmon Ubuntu, was fixed by setting the sourceon my terminal as a short-termsolution:

我在使用rvmon 时遇到了同样的问题Ubuntu,通过将source终端上的设置为短期解决方案来解决:

source $HOME/.rvm/scripts/rvm

source $HOME/.rvm/scripts/rvm

or

或者

source /home/$USER/.rvm/scripts/rvm

source /home/$USER/.rvm/scripts/rvm

and configure a default Ruby Version, 2.3.3in my case.

并配置一个默认的 Ruby 版本,2.3.3就我而言。

rvm use 2.3.3 --default

rvm use 2.3.3 --default



And a long-termSolution is to add your sourceto your .bashrcfile to permanentlymake Ubuntu look in .rvmfor all the Ruby files.

一个长期的解决方案是将您source.bashrc文件添加到您的文件中,以使 Ubuntu永久查找.rvm所有 Ruby 文件。

Add:

添加:

source .rvm/scripts/rvm

source .rvm/scripts/rvm

into

进入

$HOME/.bashrcfile.

$HOME/.bashrc文件。

回答by giapnh

I think the problem happened when you use rbenv. Try the below commands to fix it.

我认为问题发生在您使用 rbenv 时。尝试以下命令来修复它。

rbenv shell {rb_version}
rbenv global {rb_version}
or 
rbenv local {rb_version}

回答by urubuz

Install rbenv or rvm as your Ruby version manager (I prefer rbenv) via homebrew (ie. brew update & brew install rbenv) but then for example in rbenv's case make sure to add rbenv to your $PATH as instructed hereand here.

通过自制软件(即brew update & brew install rbenv)安装 rbenv 或 rvm 作为您的 Ruby 版本管理器(我更喜欢 rbenv),但是例如在 rbenv 的情况下,请确保按照此处此处的说明将 rbenv 添加到您的 $PATH 中。

For a deeper explanation on how rbenv works I recommend this.

为了更深入地解释 rbenv 如何工作,我推荐这个