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

时间:2022-08-22 22:47:29

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

我正在安装杰基尔。运行gem安装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 list so I'm thoroughly confused:

我在运行gem列表时看到了Jekyll的安装,所以我非常困惑:

*** 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,或者至少通过这个权限错误?

6 个解决方案

#1


46  

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

你在/usr/local/Cellar/ruby/.... 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 can use 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安装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安装到您自己的主目录中,在那里您将能够自动安装红宝石和gems而无需权限问题。请参阅“单用户安装”中安装到本地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 sudo with that configuration you've done something wrong.

对于大多数用户来说,“单用户安装”是一种方法。如果必须对该配置使用sudo,那么您做错了什么。

#2


29  

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 homebrew to install Ruby.

我一直遇到的问题(与OP相同)可能与使用homebrew安装Ruby有关。

To fix this, first I updated homebrew:

为了解决这个问题,我首先更新了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 NOT installed 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 rbenv to 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

#3


4  

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

在使用export GEM_HOME=/home/ /gems将gems目录设置为运行gem安装的用户目录之后,问题就解决了。

#4


2  

I had the same problem using rvm on Ubuntu, was fixed by setting the source on my terminal as a short-term solution:

我在Ubuntu上使用rvm时遇到了同样的问题,通过在我的终端上设置源代码作为短期解决方案来解决:

source $HOME/.rvm/scripts/rvm

源$ HOME / .rvm /脚本/区

or

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

源/home/$用户/ .rvm /脚本/区

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

并配置一个默认的Ruby版本,在我的例子中是2.3.3。

rvm use 2.3.3 --default

rvm使用2.3.3——违约


And a long-term Solution is to add your source to your .bashrc file to permanently make Ubuntu look in .rvm for all the Ruby files.

长期的解决方案是将源代码添加到.bashrc文件中,以永久地使Ubuntu在.rvm中查找所有Ruby文件。

Add:

添加:

source .rvm/scripts/rvm

源.rvm /脚本/区

into

$HOME/.bashrc file.

$ HOME /。bashrc文件。(

#5


0  

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

通过homebrew将rbenv或rvm安装为您的Ruby版本管理器(我更喜欢rbenv)。brew更新和brew安装rbenv)但是,例如在rbenv的例子中,请确保按照这里和这里的指示,将rbenv添加到您的$PATH中。

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

对于rbenv如何工作的更深入的解释,我建议这样做。

#6


0  

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/

#1


46  

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

你在/usr/local/Cellar/ruby/.... 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 can use 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安装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安装到您自己的主目录中,在那里您将能够自动安装红宝石和gems而无需权限问题。请参阅“单用户安装”中安装到本地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 sudo with that configuration you've done something wrong.

对于大多数用户来说,“单用户安装”是一种方法。如果必须对该配置使用sudo,那么您做错了什么。

#2


29  

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 homebrew to install Ruby.

我一直遇到的问题(与OP相同)可能与使用homebrew安装Ruby有关。

To fix this, first I updated homebrew:

为了解决这个问题,我首先更新了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 NOT installed 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 rbenv to 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

#3


4  

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

在使用export GEM_HOME=/home/ /gems将gems目录设置为运行gem安装的用户目录之后,问题就解决了。

#4


2  

I had the same problem using rvm on Ubuntu, was fixed by setting the source on my terminal as a short-term solution:

我在Ubuntu上使用rvm时遇到了同样的问题,通过在我的终端上设置源代码作为短期解决方案来解决:

source $HOME/.rvm/scripts/rvm

源$ HOME / .rvm /脚本/区

or

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

源/home/$用户/ .rvm /脚本/区

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

并配置一个默认的Ruby版本,在我的例子中是2.3.3。

rvm use 2.3.3 --default

rvm使用2.3.3——违约


And a long-term Solution is to add your source to your .bashrc file to permanently make Ubuntu look in .rvm for all the Ruby files.

长期的解决方案是将源代码添加到.bashrc文件中,以永久地使Ubuntu在.rvm中查找所有Ruby文件。

Add:

添加:

source .rvm/scripts/rvm

源.rvm /脚本/区

into

$HOME/.bashrc file.

$ HOME /。bashrc文件。(

#5


0  

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

通过homebrew将rbenv或rvm安装为您的Ruby版本管理器(我更喜欢rbenv)。brew更新和brew安装rbenv)但是,例如在rbenv的例子中,请确保按照这里和这里的指示,将rbenv添加到您的$PATH中。

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

对于rbenv如何工作的更深入的解释,我建议这样做。

#6


0  

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/