While executing gem... (Gem::FilePermissionError)You don't have write permission问题
很多时候使用ruby
安装插件都会爆出这个无权限的错误,这个时候需要我们重新使用homebrew
来重新安装一个ruby
,首先在终端使用命令查看现在有几个ruby
which -a ruby
出现以下结果说明你的电脑中可能只有一个ruby
,或者你有两个ruby
但是并未把非系统安装的加入环境变量。
imac@iMac ~ % which -a ruby
/usr/bin/ruby
没关系,可以使用homebrew
继续安装,一种情况是你已经安装,会出现如下提示:
imac@iMac ~ % brew install ruby
Warning: ruby 3.2.2_1 is already installed and up-to-date.
To reinstall 3.2.2_1, run:
brew reinstall ruby
就按照提示执行brew reinstall ruby
imac@iMac ~ % brew reinstall ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/manifests/3.2.2_1
Already downloaded: /Users/imac/Library/Caches/Homebrew/downloads/b7df479895630ebb7fdc5e0915854094e1f610797d6eff15202cebf668cbad53--ruby-3.2.2_1.bottle_manifest.json
==> Fetching ruby
==> Downloading https://ghcr.io/v2/homebrew/core/ruby/blobs/sha256:dd4528e4e2fad
Already downloaded: /Users/imac/Library/Caches/Homebrew/downloads/bff065d898a221bdacfa4f9b920b691b257f2928ca78377178685a2a2ee144fb--ruby--3.2.2_1.arm64_ventura.bottle.tar.gz
==> Reinstalling ruby
==> Pouring ruby--3.2.2_1.arm64_ventura.bottle.tar.gz
🍺 /opt/homebrew/Cellar/ruby/3.2.2_1: 16,605 files, 48.3MB
==> Running `brew cleanup ruby`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
此时执行which -a ruby
大概率还是显示只有一个,这个时候就需要把新的ruby
加入环境变量
imac@iMac ~ % echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
imac@iMac ~ % source ~/.zshrc
⚠️注意⚠️
这个路径可能是因人而异的,取决于你使用系统的终端还是-zsh,比如我安装的过程中即将安装结束的时候显示的路径如下:
🍺 /opt/homebrew/Cellar/ruby/3.2.2_1: 16,605 files, 48.3MB
那么就要使用echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
有一些同学的路径可能会是🍺 /usr/local/Cellar/ruby/3.1.2: 15,996 files, 41MB
这种情况就要使用echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
此时再次执行which -a ruby
就会显示有两个ruby
了
imac@iMac ~ % which -a ruby
/opt/homebrew/opt/ruby/bin/ruby
/usr/bin/ruby
如果你的电脑中此前真的只有一个ruby
,那么执行brew install ruby
,即将安装完成时会出现如下打印
If you need to have ruby first in your PATH, run:
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
For compilers to find ruby you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby/lib"
export CPPFLAGS="-I/usr/local/opt/ruby/include"
就根据提示给的这个路径执行添加变量的命令即可,和上面的情况一样,这个PATH也可能因人而异
imac@iMac ~ % echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
imac@iMac ~ % source ~/.zshrc
最后我们使用Homebrew
安装的ruby
来安装我们想要的插件即可
sudo gem install -n /opt/homebrew/bin xcpretty
当然,如果你的PATH是export PATH="/usr/local/opt/ruby/bin:$PATH
,这里的路径也要修改
sudo gem install -n /usr/local/bin cocoapods