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

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


当前回答

安装rbenv;

然后在~/的末尾加上eval "$(rbenv init -)"。Bash_profile(或~/.zshrc MacOS);

打开一个新终端,运行gem install ***将工作!

其他回答

2021年方案(使用rvm):

如果您在终端中输入哪种ruby,并显示/usr/bin/ruby,您可以尝试此解决方案。

安装区 curl -L https://get.rvm.io | bash -s稳定 使用RVM安装ruby RVM安装ruby-3.0.0 使用您安装的ruby版本 RVM使用ruby-3.0.0 再次输入哪个ruby,这将显示/Users/mac_user_name/.rvm/ ruby /ruby-3.0.0/bin/ruby。 这是使用ruby的新途径。

你为什么不这样做:

sudo gem update --system

该错误的原因是您没有以root用户登录终端。

如果你已经在终端类型的mac上启用了根用户

$ su

如果您没有root用户,您需要使用以下步骤启用它

From the Apple menu choose System Preferences…. From the View menu choose Users & Groups. Click the lock and authenticate as an administrator account. Click Login Options…. Click the “Edit…” or “Join…” button at the bottom right. Click the “Open Directory Utility…” button. Click the lock in the Directory Utility window. Enter an administrator account name and password, then click OK. Choose Enable Root User from the Edit menu. Enter the root password you wish to use in both the Password and Verify fields, then click OK.

更多内容请登录http://support.apple.com/kb/ht1528

在被困了几个小时后,至少它对我有用。

你可以使用:gem install cocoapods——pre——user

您可以更改GEM_HOME。您还在您的主目录下有一个gem文件夹来检查它的使用

$ gem env

结果如下。不相关的部分省略。

...
  - GEM PATHS:
     - /Users/xxx/.gem/ruby/2.6.0
     - /Library/Ruby/Gems/2.6.0
     - /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0
...
 

你可以使用你的/Users/xxx/.gem/ruby/2.6.0文件夹。

vim ~/.bash_profile

添加以下行

export GEM_HOME=~/.gem/ruby/2.6.0/

之后你就可以使用了

source ~/.bash_profile