你可能有過這樣糟糕的經歷,當敲下 brew install 應用名稱,靜靜的等待安裝結果時,Homebrew 在 Updating 的地方卡死了。
Homebrew 是一款 Mac OS 平台下的軟件包管理工具,擁有安裝、卸載、更新、查看、搜索等很多實用的功能。簡單的一條指令,就可以實現包管理,而不用你關心各種依賴和文件路徑的情況,十分方便快捷。
Homebrew 主要分兩部分:git repo(位於 GitHub)和二進制 bottles,這兩者在國內訪問都不太順暢。接下來演示如何使用阿里雲來加速這兩部分的下載。
使用 阿里雲 的 Homebrew 鏡像源進行加速
如果你沒有更換過鏡像源,執行 brew 命令安裝應用的時候,跟以下 3 個倉庫地址有關:
- brew.git
- homebrew-core.git
- homebrew-bottles
更換 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
執行上述命令之后,請執行更新命令:
brew update
此時可以執行 brew config
命令,查看配置信息:
MacBook-Pro :: ~ » brew config
HOMEBREW_VERSION: 2.5.11
ORIGIN: https://mirrors.aliyun.com/homebrew/brew.git
HEAD: 07b3aa69f8dc5098437cf2562864558bae2d45aa
Last commit: 10 days ago
Core tap ORIGIN: https://mirrors.aliyun.com/homebrew/homebrew-core.git
Core tap HEAD: deda5a05551ab74d5313af112d3bb2054f9b6af8
Core tap last commit: 17 hours ago
Core tap branch: master
HOMEBREW_PREFIX: /usr/local
HOMEBREW_BOTTLE_DOMAIN: https://mirrors.aliyun.com/homebrew/homebrew-bottles
HOMEBREW_CASK_OPTS: []
HOMEBREW_MAKE_JOBS: 8
Homebrew Ruby: 2.6.3 => /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
CPU: octa-core 64-bit icelake
Clang: 12.0 build 1200
Git: 2.24.3 => /Applications/Xcode.app/Contents/Developer/usr/bin/git
Curl: 7.64.1 => /usr/bin/curl
macOS: 10.15.7-x86_64
CLT: 1103.0.32.62
Xcode: 12.2
更換 homebrew-bottles
接下來是二進制文件下載的設置。
這與你當前 macOS 系統使用的 shell 版本有關系,執行以下命令查看 Shell 版本:
$ echo $SHELL
根據版本不同,會輸出 2 種結果,/bin/zsh 或 /bin/bash,根據類型進行操作即可
/bin/zsh
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
/bin/bash
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
到這里,更換 Homebrew 默認源的所有操作啦,盡情地去 brew install 吧!
恢復默認配置
出於某些場景,可能需要回退到默認配置,你可以通過下述方式回退到默認配置。
首先執行下述命令:
# 重置brew.git:
cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git
# 重置homebrew-core.git:
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git
然后刪掉 HOMEBREW_BOTTLE_DOMAIN 環境變量,將你終端文件 ~/.bash_profile
或者 ~/.zshrc
中
HOMEBREW_BOTTLE_DOMAIN
行刪掉,並執行
source ~/.bash_profile
或者
source ~/.zshrc
原文作者:Summer
轉自鏈接:https://learnku.com/mac/wikis/39228
版權聲明:著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請保留以上作者信息和原文鏈接。