Mac OSX系統homebrew update Fetching failed問題解決方案

coffee_tea_or_me發表於2021-05-20

1. brew update error

(i) 問題出現及現象描述

昨天換了臺電腦,有些軟體需要重新安裝或更新一下,遇到了下面的問題

cv@xys-MacBook-Pro ~ % brew update
touch: /usr/local/Homebrew/.git/FETCH_HEAD: Permission denied
error: cannot open .git/FETCH_HEAD: Permission denied
touch: /usr/local/Homebrew/Library/Taps/caskroom/homebrew-cask/.git/FETCH_HEAD: Permission denied
/usr/local/Homebrew/Library/Homebrew/cmd/update.sh: line 522: /usr/local/Homebrew/.git/UPDATE_FAILED: Permission denied
error: cannot open .git/FETCH_HEAD: Permission denied
touch: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask/.git/FETCH_HEAD: Permission denied
/usr/local/Homebrew/Library/Homebrew/cmd/update.sh: line 522: /usr/local/Homebrew/.git/UPDATE_FAILED: Permission denied
error: cannot open .git/FETCH_HEAD: Permission denied
/usr/local/Homebrew/Library/Homebrew/cmd/update.sh: line 522: /usr/local/Homebrew/.git/UPDATE_FAILED: Permission denied
touch: /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/.git/FETCH_HEAD: Permission denied
error: cannot open .git/FETCH_HEAD: Permission denied
/usr/local/Homebrew/Library/Homebrew/cmd/update.sh: line 522: /usr/local/Homebrew/.git/UPDATE_FAILED: Permission denied
fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': Permission denied
fatal: Unable to create '/usr/local/Homebrew/.git/index.lock': Permission denied
error: could not lock config file .git/config: Permission denied
Already up-to-date.

(ii) 解決方案

在StackOverflow上找到了一個類似的問題,用高讚的回答解決了這個問題。

直接通過改變 /usr/local 的所屬使用者進而解決許可權問題。

不同的版本使用的指令略有不同,如果是 macOS High Sierra 或更高版本的系統的使用者,使用下面的指令可以修改所屬權,

~$ sudo chown -R $(whoami) $(brew --prefix)/*

如果是 macOS Sierra 或更低版本的系統的使用者,使用下面的指令

~$ sudo chown -R $(whoami) /usr/local

檢視本機系統版本資訊,可以通過單擊左上角蘋果 Logo ==> 關於本機,就能看到系統版本號及其名稱。版本號之間的高低可以通過WikiPedia上面記載的版本與名稱對應列表來查詢。

Public Beta: "Kodiak", Released on September 13, 2000
Version 10.0: "Cheetah", Released on March 24, 2001
Version 10.1: "Puma", Released on September 25, 2001
Version 10.2: "Jaguar", Released on August 23, 2002
Version 10.3: "Panther", Released on October 24, 2003
Version 10.4: "Tiger", Released on April 29, 2005
Version 10.5: "Leopard", Released on October 26, 2007
Version 10.6: "Snow Leopard", Released on August 28, 2009
Version 10.7: "Lion", Released on July 20, 2011
Version 10.8: "Mountain Lion", Released on July 25, 2012
Version 10.9: "Mavericks", Released on October 22, 2013
Version 10.10: "Yosemite", Released on October 16, 2014
Version 10.11: "El Capitan", Released on June 8, 2015
Version 10.12: "Sierra", Released on June 13, 2016
Version 10.13: "High Sierra", Released on June 5, 2017
Version 10.14: "Mojave", Released on June 4, 2018
Version 10.15: "Catalina", Released on June 3, 2019
Version 11: "Big Sur", Released on June 22, 2020

2. brew update "Fetching homebrew-cask failed!"

(i) 問題描述

儘管許可權問題用上述方法解決了,但後續執行時又遇到了一點問題,這次是主要是Homebrew的其中兩個組成部分homebrew-caskhomebrew-core引起的,出現瞭如下所示的 "Fetching failed" 錯誤。

cv@xys-MBP ~ % brew update
Error: Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask failed!
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core failed!
Updated 2 taps (homebrew/cask and caskroom/cask).

(ii) 解決方案

嘗試了一些方法都沒解決,最後選擇重新安裝 Homebrew,畢竟沒有什麼問題是重灌不能解決的,如過有,就連繫統一起重灌...話說回來,重灌之後確實沒有問題了(\汗)。

首先自然是解除安裝已有的安裝版本,指令是 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)",結果如下。

cv@xys-MBP ~ % /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"
Warning: The Ruby Homebrew uninstaller is now deprecated and has been rewritten in
Bash. Please migrate to the following command: 
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443

cv@xys-MBP ~ % /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)

接著當然是安裝新版本,指令是/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)",結果如下,中間有一次需要根據自己的需求選擇源並輸入 root 密碼。

cv@xys-MBP ~ % /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

               開始執行Brew自動安裝程式
              [cunkai.wang@foxmail.com]
           ['2021-05-20 17:53:17']['11.3']
        https://zhuanlan.zhihu.com/p/111014448


請選擇一個下載映象,例如中科大,輸入1回車。
源有時候不穩定,如果git克隆報錯重新執行指令碼選擇源。cask非必須,有部分人需要。
1、中科大下載源 2、清華大學下載源 3、北京外國語大學下載源  4、騰訊下載源(不推薦) 5、阿里巴巴下載源(不推薦 缺少cask源)

請輸入序號:

我選擇了中科大下載源,輸入 1 回車即可。


  你選擇了中國科學技術大學下載源

!!!此指令碼將要刪除之前的brew(包括它下載的軟體),請自行備份。
->是否現在開始執行指令碼(N/Y)

是否執行,選擇是,輸入 Y 回車繼續。接著輸入密碼後正常情況下就能一直執行到最後了,如果中間斷了根據提示進行相應的操作即可。

--> 指令碼開始執行
 Mac os設定開機密碼方法:
  (設定開機密碼:在左上角蘋果圖示->系統偏好設定->使用者與群組->更改密碼)
  (如果提示This incident will be reported. 在使用者與群組中檢視是否管理員)
==> 通過命令刪除之前的brew、建立一個新的Homebrew資料夾
請輸入開機密碼,輸入過程不顯示,輸入完後回車
開始執行
  ---備份要刪除的/usr/local/Homebrew到系統桌面....
   ---/usr/local/Homebrew 備份完成
-> 建立資料夾 /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /bin/mkdir -p /usr/local/Homebrew
此步驟成功
執行程式碼 ==> /usr/bin/sudo /bin/chmod -R a+rwx /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /usr/sbin/chown cv /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Homebrew
git version 2.20.1

下載速度覺得慢可以ctrl+c或control+c重新執行指令碼選擇下載源
==> 克隆Homebrew基本檔案

未發現Git代理(屬於正常狀態)
Cloning into '/usr/local/Homebrew'...
remote: Enumerating objects: 191263, done.
remote: Total 191263 (delta 0), reused 0 (delta 0) | 10.55 MiB/s   
Receiving objects: 100% (191263/191263), 48.84 MiB | 10.64 MiB/s, done.
Resolving deltas: 100% (142769/142769), done.
此步驟成功
--建立Brew所需要的目錄
執行程式碼 ==> /usr/bin/sudo /bin/chmod u+rwx /usr/local/bin/brew
執行程式碼 ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/bin/brew
執行程式碼 ==> /usr/bin/sudo /bin/mkdir -p /usr/local/sbin /usr/local/Caskroom
執行程式碼 ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/sbin /usr/local/Caskroom
執行程式碼 ==> /usr/bin/sudo /usr/sbin/chown cv /usr/local/sbin /usr/local/Caskroom
執行程式碼 ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/sbin /usr/local/Caskroom
執行程式碼 ==> /usr/bin/sudo /usr/sbin/chown -R cv:admin /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /bin/mkdir -p /Users/cv/Library/Caches/Homebrew
執行程式碼 ==> /usr/bin/sudo /bin/chmod g+rwx /Users/cv/Library/Caches/Homebrew
執行程式碼 ==> /usr/bin/sudo /usr/sbin/chown -R cv /Users/cv/Library/Caches/Homebrew
--依賴目錄指令碼執行完成
==> 建立brew的替身
==> 克隆Homebrew Core
此處如果顯示Password表示需要再次輸入開機密碼,輸入完後回車
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'...
remote: Enumerating objects: 960954, done.
remote: Total 960954 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (960954/960954), 380.33 MiB | 10.66 MiB/s, done.
Resolving deltas: 100% (660866/660866), done.
Checking out files: 100% (5914/5914), done.
此步驟成功
==> 克隆Homebrew Cask 圖形化軟體
  此處如果顯示Password表示需要再次輸入開機密碼,輸入完後回車

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 624249, done.
remote: Total 624249 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (624249/624249), 260.95 MiB | 9.69 MiB/s, done.
Resolving deltas: 100% (445746/445746), done.
此步驟成功
==> 克隆Homebrew services 管理服務的啟停

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 1141, done.
remote: Counting objects: 100% (1141/1141), done.
remote: Compressing objects: 100% (495/495), done.
remote: Total 1141 (delta 484), reused 1141 (delta 484), pack-reused 0
Receiving objects: 100% (1141/1141), 331.50 KiB | 330.00 KiB/s, done.
Resolving deltas: 100% (484/484), done.
此步驟成功
==> 配置國內映象源HOMEBREW BOTTLE
sed: /Users/cv/.zprofile: No such file or directory
環境變數寫入->/Users/cv/.zprofile
此步驟成功
執行程式碼 ==> /usr/bin/sudo /bin/chmod -R a+rwx /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /usr/sbin/chown cv /usr/local/Homebrew
執行程式碼 ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Homebrew

==> 安裝完成,brew版本

Homebrew 3.1.8-50-g76c4767-dirty
Homebrew/homebrew-core (git revision 832bb8b966; last commit 2021-05-20)
Homebrew/homebrew-cask (git revision f397055044; last commit 2021-05-20)
Brew前期配置成功
電腦系統版本:11.3
All user-space services OK, nothing cleaned...

  ==> brew update-reset

==> Fetching /usr/local/Homebrew...

==> Resetting /usr/local/Homebrew...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.

==> Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...

==> Resetting /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...   
Checking out files: 100% (3977/3977), done.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.

==> Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...

==> Resetting /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Checking out files: 100% (5659/5659), done.
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.

==> Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-services...

==> Resetting /usr/local/Homebrew/Library/Taps/homebrew/homebrew-services...
Branch 'master' set up to track remote branch 'master' from 'origin'.
Reset branch 'master'
Your branch is up to date with 'origin/master'.


Brew自動安裝程式執行完成
國內地址已經配置完成

  桌面的Old_Homebrew資料夾,大致看看沒有你需要的可以刪除。

初步介紹幾個brew命令
本地軟體庫列表:brew ls
查詢軟體:brew search google(其中google替換為要查詢的關鍵字)
檢視brew版本:brew -v  更新brew版本:brew update
安裝cask軟體:brew install --cask firefox 把firefox換成你要安裝的

歡迎右鍵點選下方地址-開啟URL 來給點個贊
https://zhuanlan.zhihu.com/p/111014448 

 重啟終端 或者 執行 source /Users/cv/.zprofile   否則可能無法使用

安裝完成後按照提示執行 source 命令

cv@xys-MBP ~ % source ~/.zprofile

至此重灌過程結束,開始正常使用 Homebrew。

(全文完)


參考資料

[1] brew update fails with “Permission denied” error (https://stackoverflow.com/questions/50267510/brew-update-fails-with-permission-denied-error/50268978)

[2] macOS version history (https://en.wikipedia.org/wiki/MacOS_version_history)

[3] mac下Homebrew安裝Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask failed報錯解決方法 (http://www.cxyzjd.com/article/Jiangzhiqi4551/116174524)

[4] Mac 下 brew 切換為國內源 (https://cloud.tencent.com/developer/article/1614039)

本文作者 :phillee
發表日期 :2021年5月20日
本文連結https://www.cnblogs.com/phillee/p/14788909.html
版權宣告 :自由轉載-非商用-非衍生-保持署名(創意共享3.0許可協議/CC BY-NC-SA 3.0)。轉載請註明出處!
限於本人水平,如果文章和程式碼有表述不當之處,還請不吝賜教。

相關文章