解決Homebrew下載更新極慢的問題

近期使用Homebrew去下載安裝軟件的時候總是卡在update階段,時間非常久,難以忍受。記錄一下解決方法,

症狀

使用Homebrew安裝軟件的時候一直卡在Update階段。同時發現從github.com下載文件也極度緩慢(總是卡住不動了)。

問題定位

使用brew update --verbose觀察update過程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
brew update --verbose
Checking if we need to fetch /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
Fetching /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
remote: Enumerating objects: 337, done.
remote: Counting objects: 100% (337/337), done.
remote: Compressing objects: 100% (88/88), done.
remote: Total 298 (delta 221), reused 287 (delta 210), pack-reused 0
Receiving objects: 100% (298/298), 50.91 KiB | 39.00 KiB/s, done.
Resolving deltas: 100% (221/221), completed with 39 local objects.
From https://github.com/Homebrew/homebrew-core
   65a45a9..583b7f1  master     -> origin/master
remote: Enumerating objects: 179429, done.
remote: Counting objects: 100% (179429/179429), done.
remote: Compressing objects: 100% (56607/56607), done.
Receiving objects:   4% (7628/177189), 1.48 MiB | 8.00 KiB/s

 

發現update卡在從github倉庫獲取文件的過程。這個結果與手動從github下載文件慢的症狀相互印證。

解決

由於問題主要是在國內網絡環境github下載慢,因此嘗試:

  1. 更換使用國內的homebrew鏡像源;
  2. 使用代理訪問github.com。

更換Homebrew源

使用以下命令更換國內阿里雲上的homebrew鏡像:

1
2
3
4
5
6
7
8
9
10
# 替換brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
# 替換homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 替換homebrew-bottles:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc

 

替換後,問題依舊,繼續查看日誌:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
brew update --verbose
Checking if we need to fetch /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
Fetching /usr/local/Homebrew...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Checking if we need to fetch /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core...
From https://mirrors.aliyun.com/homebrew/homebrew-core
 + 583b7f1...8435590 master     -> origin/master  (forced update)
Fetching /usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask...
Fetching /usr/local/Homebrew/Library/Taps/caskroom/homebrew-fonts...
remote: Enumerating objects: 179429, done.
remote: Counting objects: 100% (179429/179429), done.
remote: Compressing objects: 100% (56607/56607), done.
Receiving objects:   6% (11170/177189), 2.16 MiB | 30.00 KiB/s

 

可以看到由於homebrew-cask的倉庫依然指向了Github,這個過程還是慢。阿里雲的鏡像站沒有提供homebrew-cask,進一步搜索找到USTC鏡像站,該站提供了homebrew-cask的源。使用上述同樣的命令更換源:

1
2
3
# 替換homebrew-cask.git:
cd "$(brew --repo)"/Library/Taps/homebrew/homebrew-cask
git remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git

 

測試發現問題解決。

Note: Homebrew Bottles源的更換方法詳見這裏

官方源地址:
https://github.com/Homebrew/brew.git
https://github.com/Homebrew/homebrew-core.git
https://github.com/Homebrew/homebrew-cask

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章