使用ruby安裝插件無(wú)權(quán)限問(wèn)題

While executing gem... (Gem::FilePermissionError)You don't have write permission問(wèn)題

很多時(shí)候使用ruby安裝插件都會(huì)爆出這個(gè)無(wú)權(quán)限的錯(cuò)誤,這個(gè)時(shí)候需要我們重新使用homebrew來(lái)重新安裝一個(gè)ruby,首先在終端使用命令查看現(xiàn)在有幾個(gè)ruby

which -a ruby

出現(xiàn)以下結(jié)果說(shuō)明你的電腦中可能只有一個(gè)ruby,或者你有兩個(gè)ruby但是并未把非系統(tǒng)安裝的加入環(huán)境變量。

imac@iMac ~ % which -a ruby
/usr/bin/ruby

沒(méi)關(guān)系,可以使用homebrew繼續(xù)安裝,一種情況是你已經(jīng)安裝,會(huì)出現(xiàn)如下提示:

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

就按照提示執(zhí)行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`).

此時(shí)執(zhí)行which -a ruby大概率還是顯示只有一個(gè),這個(gè)時(shí)候就需要把新的ruby加入環(huán)境變量

imac@iMac ~ % echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc 
imac@iMac ~ % source ~/.zshrc 

??注意??
這個(gè)路徑可能是因人而異的,取決于你使用系統(tǒng)的終端還是-zsh,比如我安裝的過(guò)程中即將安裝結(jié)束的時(shí)候顯示的路徑如下:
?? /opt/homebrew/Cellar/ruby/3.2.2_1: 16,605 files, 48.3MB
那么就要使用echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
有一些同學(xué)的路徑可能會(huì)是?? /usr/local/Cellar/ruby/3.1.2: 15,996 files, 41MB
這種情況就要使用echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc

此時(shí)再次執(zhí)行which -a ruby就會(huì)顯示有兩個(gè)ruby

imac@iMac ~ % which -a ruby    
/opt/homebrew/opt/ruby/bin/ruby
/usr/bin/ruby

如果你的電腦中此前真的只有一個(gè)ruby,那么執(zhí)行brew install ruby,即將安裝完成時(shí)會(huì)出現(xiàn)如下打印

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"

就根據(jù)提示給的這個(gè)路徑執(zhí)行添加變量的命令即可,和上面的情況一樣,這個(gè)PATH也可能因人而異

imac@iMac ~ % echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
imac@iMac ~ % source ~/.zshrc 

最后我們使用Homebrew安裝的ruby來(lái)安裝我們想要的插件即可

sudo gem install -n /opt/homebrew/bin xcpretty

當(dāng)然,如果你的PATHexport PATH="/usr/local/opt/ruby/bin:$PATH,這里的路徑也要修改

sudo gem install -n /usr/local/bin cocoapods
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容