跳轉到內容

Homebrew 國內鏡像源 | 清華大學/中科大加速配置教程

Homebrew Mirror Configuration

對於中國大陸的用戶來說,由於網絡環境的限制,直接從 GitHub 下載 Homebrew 及其軟件包可能會非常緩慢甚至失敗。使用國內鏡像源可以顯著提升下載速度,改善使用體驗。本文將介紹幾種常用的鏡像源配置方法。

為什麼需要鏡像源?

問題現狀

  • GitHub 訪問慢:raw.githubusercontent.com 經常被牆或限速
  • 軟件包下載慢:homebrew-bottles 在國外 CDN
  • Git 克隆慢:homebrew-core 倉庫體積大,下載耗時

解決方案

使用國內鏡像源可以:

  • ✅ 提升下載速度 10-100 倍
  • ✅ 避免連接超時
  • ✅ 節省流量和時間
  • ✅ 提高穩定性

常用鏡像源對比

鏡像源速度穩定性更新頻率推薦度
清華大學⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐實時同步⭐⭐⭐⭐⭐
中科大⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐實時同步⭐⭐⭐⭐⭐
阿里雲⭐⭐⭐⭐⭐⭐⭐⭐每小時⭐⭐⭐⭐
騰訊雲⭐⭐⭐⭐⭐⭐⭐⭐每小時⭐⭐⭐⭐

使用中科大的鏡像

sh
cd "$(brew --repo)"
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin git://mirrors.ustc.edu.cn/homebrew-core.git

完整配置步驟

步驟 1:設置 Git 遠程地址

bash
# 切換到 Homebrew 根目錄
cd "$(brew --repo)"

# 設置 brew 倉庫鏡像
git remote set-url origin git://mirrors.ustc.edu.cn/brew.git

# 設置 homebrew-core 倉庫鏡像
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin git://mirrors.ustc.edu.cn/homebrew-core.git

# 如果有 homebrew-cask
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-cask"
git remote set-url origin git://mirrors.ustc.edu.cn/homebrew-cask.git

步驟 2:設置 Bottle 鏡像

bash
# 臨時設置(當前終端有效)
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"

# 永久設置(添加到 shell 配置文件)
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc

步驟 3:驗證配置

bash
# 檢查 Git 遠程地址
cd "$(brew --repo)"
git remote -v
# 應該顯示:
# origin  git://mirrors.ustc.edu.cn/brew.git (fetch)
# origin  git://mirrors.ustc.edu.cn/brew.git (push)

# 測試更新速度
brew update

使用清華大學的鏡像

sh
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

完整配置步驟

步驟 1:設置 Git 遠程地址

bash
# 設置 brew 倉庫鏡像
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 設置 homebrew-core 倉庫鏡像
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 如果有 homebrew-cask
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

步驟 2:設置 Bottle 鏡像

bash
# 永久設置
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc

步驟 3:刷新配置

bash
# 更新 Homebrew
brew update

# 驗證速度提升
time brew search nginx

其他鏡像源配置

阿里雲鏡像

bash
# 設置 Git 遠程
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

# 設置 Bottle 域名
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"' >> ~/.zshrc

騰訊雲鏡像

bash
# 設置 Git 遠程
git -C "$(brew --repo)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.cloud.tencent.com/homebrew/homebrew-core.git

# 設置 Bottle 域名
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.cloud.tencent.com/homebrew/homebrew-bottles"
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.cloud.tencent.com/homebrew/homebrew-bottles"' >> ~/.zshrc

一鍵配置腳本

清華大學鏡像配置腳本

#!/bin/bash
# setup-tsinghua-mirror.sh

echo "🚀 配置清華大學 Homebrew 鏡像源..."

# 備份當前配置
echo "📦 備份當前配置..."
cp ~/.zshrc ~/.zshrc.backup.$(date +%Y%m%d)

# 設置 Git 遠程
echo "🔧 設置 Git 遠程倉庫..."
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 檢查是否有 cask
if [ -d "$(brew --repo homebrew/cask)" ]; then
  git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
fi

# 設置 Bottle 域名
echo "🌐 設置 Bottle 鏡像域名..."
if ! grep -q "HOMEBREW_BOTTLE_DOMAIN" ~/.zshrc; then
  echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
fi

# 應用配置
source ~/.zshrc

# 測試更新
echo "🔄 測試更新..."
brew update

echo "✅ 配置完成!"
echo "💡 提示:如果遇到問題,可以恢復備份:cp ~/.zshrc.backup.* ~/.zshrc"

使用方法

# 下載腳本
curl -fsSL https://example.com/setup-tsinghua-mirror.sh -o setup-mirror.sh

# 執行腳本
chmod +x setup-mirror.sh
./setup-mirror.sh

恢復官方源

如果需要恢復到官方源:

# 恢復 Git 遠程地址
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git

# 移除 Bottle 域名配置
sed -i '' '/HOMEBREW_BOTTLE_DOMAIN/d' ~/.zshrc
unset HOMEBREW_BOTTLE_DOMAIN

# 重新加載配置
source ~/.zshrc

# 更新
brew update

Linux 用戶配置

Linux 上的 Homebrew(Linuxbrew)配置類似:

# 清華大學鏡像
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"

# 添加到配置文件
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.bashrc
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.bashrc
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.bashrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.bashrc

source ~/.bashrc

常見問題排查

問題 1:配置後仍然很慢

bash
# 檢查配置是否生效
echo $HOMEBREW_BOTTLE_DOMAIN

# 檢查 Git 遠程地址
cd "$(brew --repo)"
git remote -v

# 清除緩存重試
brew cleanup
brew update

問題 2:鏡像源同步延遲

bash
# 手動強制更新
cd "$(brew --repo)"
git fetch origin
git reset --hard origin/master

cd "$(brew --repo homebrew/core)"
git fetch origin
git reset --hard origin/master

問題 3:SSL 證書錯誤

bash
# 臨時禁用 SSL 驗證(不推薦)
export GIT_SSL_NO_VERIFY=1

# 更好的方案:更新 CA 證書
sudo apt-get install ca-certificates  # Debian/Ubuntu
sudo update-ca-certificates

問題 4:部分軟件仍然從官方下載

bash
# 某些軟件的 bottle 可能不在鏡像中
# 可以從源碼編譯
brew install --build-from-source package_name

# 或者等待鏡像同步
brew update

性能對比測試

測試腳本

#!/bin/bash
# test-mirror-speed.sh

echo "測試不同鏡像源的速度..."

# 測試 GitHub(官方)
echo -n "GitHub: "
time curl -s -o /dev/null https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh

# 測試清華大學
echo -n "Tsinghua: "
time curl -s -o /dev/null https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 測試中科大
echo -n "USTC: "
time curl -s -o /dev/null git://mirrors.ustc.edu.cn/brew.git

典型結果

GitHub:   0m15.234s  (慢,經常超時)
Tsinghua: 0m0.523s   (快 30 倍)
USTC:     0m0.487s   (快 31 倍)

最佳實踐

1. 選擇合適的鏡像源

bash
# 根據地理位置選擇
# 北方用戶:清華大學
# 南方用戶:中科大
# 全國用戶:兩者皆可,定期切換測試速度

2. 定期更新配置

bash
# 每月檢查一次鏡像源狀態
brew update

# 如果某個鏡像變慢,切換到另一個

3. 組合使用

bash
# Git 使用一個鏡像,Bottle 使用另一個
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"

4. 自動化檢測

bash
# 創建自動檢測腳本
cat > ~/check-mirror.sh << 'EOF'
#!/bin/bash
start_time=$(date +%s%N)
curl -s -o /dev/null https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
end_time=$(date +%s%N)
tsinghua_time=$(( (end_time - start_time) / 1000000 ))

start_time=$(date +%s%N)
curl -s -o /dev/null git://mirrors.ustc.edu.cn/brew.git
end_time=$(date +%s%N)
ustc_time=$(( (end_time - start_time) / 1000000 ))

echo "Tsinghua: ${tsinghua_time}ms"
echo "USTC: ${ustc_time}ms"

if [ $tsinghua_time -lt $ustc_time ]; then
  echo "推薦使用清華大學鏡像"
else
  echo "推薦使用中科大鏡像"
fi
EOF

chmod +x ~/check-mirror.sh
~/check-mirror.sh

總結

配置國內鏡像源可以顯著提升 Homebrew 的使用體驗:

  1. 清華大學鏡像:速度快,穩定性好,推薦首選
  2. 中科大鏡像:同樣優秀,可作為備選
  3. 一鍵配置:使用腳本快速完成配置
  4. 靈活切換:根據實際情況選擇最優鏡像

關鍵命令速查:

bash
# 查看當前配置
git -C "$(brew --repo)" remote -v
echo $HOMEBREW_BOTTLE_DOMAIN

# 切換到清華鏡像
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

# 恢復官方源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
unset HOMEBREW_BOTTLE_DOMAIN

下一步學習:

享受快速的 Homebrew 體驗吧!🚀

最後更新於: