我试图使用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.
有人知道怎么解决这个问题吗?
我试图使用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.
有人知道怎么解决这个问题吗?
当前回答
该错误的原因是您没有以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
在被困了几个小时后,至少它对我有用。
其他回答
更老更睿智
不要做我在这里说的,只要知道在你使用sudo的任何时候都要小心。您可能希望使用类似rbenv的东西来隔离您正在做的任何工作。
的一种方式
了解chown
我不知道您是否喜欢命令行,但这将使使用任何工具在任何项目中安装包到您的系统变得轻而易举。
据我所知,chown代表着改变所有权。
我来寻找这个答案的原因是因为gem安装今天向我抛出了这个错误:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /var/lib/gems/1.9.1 directory.
这是一个使用口香糖的绝佳机会。你看,Ruby已经给了我们它需要访问的目录,而且它似乎会经常使用这个目录。
在这种情况下,要解决这个问题只需要知道三件事,但是chown要强大得多,并且比我现在要演示的灵活性要大得多。更多信息请参考下面的资料。
两件事
用户名 目录
如果您在shell中查找用户名很容易。看看提示就知道了。我的是这样的:
breadly@breadly-desktop:~\Desktop
当前用户只是@前面的名字。我们从错误消息中知道了目录,但是您有两种选择。您可以使用../gems/1.9.1将权限限制在当前版本,或者使用../gems给自己所有版本的gems写权限。
实际更改所有权的命令如下所示。
chown -R $(whoami) /absolute/path/to/directory
-R被称为一个标志,-R标志通常告诉命令递归地执行某项操作,换句话说,对目录中包含的所有内容执行该命令,以及该目录中包含的所有内容,等等,直到没有其他内容为止。
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的新途径。
安装gem或更新RubyGems失败,出现权限错误,然后输入此命令
sudo gem install cocoapods
我用它工作。
几年前
在使用下面我的原始答案之前,试试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提供的。