由於 Homebrew 默認的源是國外的,有時會導致下載速度較慢,特別是在中國大陸。為了提高下載速度,可以將 Homebrew 切換到國內鏡像。
以下是如何配置 Homebrew 使用國內源的教程。
1. 備份 Homebrew 的默認源
在更改 Homebrew 的源之前,最好先備份一份默認源的配置,以防需要恢復。您可以通過以下命令備份配置文件:
cp -r /usr/local/Homebrew /usr/local/Homebrew_backup
2. 切換 Homebrew 的國內鏡像
國內有幾個常用的 Homebrew 鏡像源,比如:清華大學、中科大、阿里雲 等。這裏以 清華大學 的鏡像源為例。
2.1 替換 Homebrew 的 Git 倉庫源
首先,打開終端並執行以下命令,替換 Homebrew 默認的 Git 倉庫源為清華鏡像:
# 替換 Homebrew 的 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
git -C "$(brew --repo)/homebrew-cask" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
2.2 替換 Homebrew 下載源
接着,替換 Homebrew 的下載源為清華鏡像:
# 替換 Homebrew 的下載源
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc
如果你使用的是 Bash,則應修改 ~/.bash_profile 或 ~/.bashrc 文件。
2.3 更新並驗證
完成源替換後,更新 Homebrew 並驗證是否成功切換:
brew update
brew doctor
如果輸出沒有錯誤,説明您的 Homebrew 已經成功切換到國內鏡像。
3. 恢復為默認的 Homebrew 源
如果您想恢復到默認的 Homebrew 源,可以通過以下命令來恢復:
# 恢復 Homebrew 默認的 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
git -C "$(brew --repo)/homebrew-cask" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
# 恢復 Homebrew 默認的下載源
unset HOMEBREW_BOTTLE_DOMAIN
然後,更新 Homebrew:
brew update
brew doctor
4. 其他國內鏡像源
除了清華大學的鏡像源,您還可以選擇其他國內鏡像源,以下是一些常用的國內源:
中科大鏡像:
# Git 倉庫源
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew/brew.git
git -C "$(brew --repo)/homebrew-core" remote set-url origin https://mirrors.ustc.edu.cn/homebrew/homebrew-core.git
git -C "$(brew --repo)/homebrew-cask" remote set-url origin https://mirrors.ustc.edu.cn/homebrew/homebrew-cask.git
# 下載源
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc
阿里雲鏡像:
# Git 倉庫源
git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew.git
git -C "$(brew --repo)/homebrew-core" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
git -C "$(brew --repo)/homebrew-cask" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git
# 下載源
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc
總結
通過以上步驟,您可以將 Homebrew 切換到國內鏡像源,從而加快軟件包的下載速度。希望這個教程對您有所幫助!如果有任何問題,請隨時提問。