我试图使用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.

有人知道怎么解决这个问题吗?


当前回答

你需要修正你的路径。

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

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

其他回答

错误:当执行gem…(Gem:: FilePermissionError) 你没有写/Library/Ruby/Gems/2.6.0的权限

(2022方案) 这里已经添加了问题的详细原因

问题概述->该问题与ruby访问有关,M1 MAC自带ruby。我们没有被允许为我们的目的使用它。相反,我们安装了一个单独的ruby实例,并将其用于我们的目的。

下面的步骤帮助我解决了这个问题,希望这可能会有所帮助

我们不需要用rvn或chruby安装ruby。 我的解决方案使用自制程序来安装ruby。

打开终端 使用自制程序安装ruby

安装ruby

[用于重新安装]brew重装ruby

使用下面的命令检查ruby的路径 这红宝石 它应该安装在下面的路径 /usr/bin/ruby 将ruby路径更改为用户路径

检查您的系统使用了哪个shell

echo $0

-zsh

对于zshrc

echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/.zshrc . bin:$PATH

在bash

echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/~/.bashrc . bin:$PATH

退出并重新启动终端 用步骤5改变路径后 再次检查ruby的路径(执行步骤3 -请确保路径如下所示)

/usr/local/opt/ruby/bin/ruby

[如果您不退出并启动终端,将显示步骤4路径]

这一步可能并不适用于所有人,如果你安装了正确的Cocoapods版本,可以跳过第10和11步

检查安装的pod的版本

豆荚,版本

使用下面的命令卸载特定版本的Cocoapods

如果安装的版本是1.11.0

卸载cocoapods 1.11.0

安装特定版本的Cocoapods 安装cocoapods 1.11.0 将路径更改为Project目录cd {Project目录的路径} 在项目目录中安装捆绑程序

包安装

执行pod安装

正在安装

你需要修正你的路径。

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

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

在使用下面我的原始答案之前,试试nathanwhy的答案。他推荐的——user-install应该可以达到同样的目的,而不必破坏.bash_profile或确定Ruby版本。


如果您不关心特定的ruby版本,您可以跳过繁重的ruby环境管理器选项,只需将这些行添加到~/.bash_profile:

export GEM_HOME="$HOME/.gem/ruby/2.0.0"
export GEM_PATH="$HOME/.gem/ruby/2.0.0"

路径是从gem env的原始输出中窃取的:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.14
  - RUBY VERSION: 2.0.0
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
  - RUBY EXECUTABLE: /System/Library/.../2.0/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-14
  - GEM PATHS:
     - /Library/Ruby/Gems/2.0.0
     - /Users/mylogin/.gem/ruby/2.0.0  #  <---- This line, right here. -----
     - /System/Library/.../usr/lib/ruby/gems/2.0.0
  ...

不需要进行任何操作,并且您可以使用已经安装好的Ruby,这是Apple提供的。

给用户$whoami在这些文件夹中创建一些东西

sudo chown -R user /Library/Ruby/Gems/2.0.0

我找到了这个无苏多尔宝石的指南:

Brew install rbenv ruby-build Sudo gem update—system 向.bashrc添加导出: export RBENV_ROOT="$(brew -prefix rbenv)" export GEM_HOME="$(brew -prefix)/opt/gems" export GEM_PATH="$(brew -prefix)/opt/gems" 最后将这个添加到~/.gemrc中: 宝石:- n / usr /地方/ bin Gem更新——system