1. 程式人生 > 實用技巧 >Mac下如何更改brew地址源

Mac下如何更改brew地址源

轉自:Mac下如何更改brew地址源

Brew install xxxx長期停留在Updating Homebrew...

由於國內某種特殊原因,我們訪問很多repo源是有一定難度的,比如在Mac上非常好用的包管理軟體Brew預設是沒有安裝的,所以需要我們首先安裝Brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

此時Brew已經安裝成功了,但當我們滿心歡喜以為大快朵頤的安裝各種工具時發現居然報錯,下載不了這,連線那又timeout,什麼也安裝不了,心肝迷茫和無助。

eric@EricdeMacBook-Pro ~ % brew install python3 
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
aws-sdk-cpp    fonttools      hsd            memcached      mpd            okteto         pspg           sqlmap
bmake          gmsh           hwloc          mercurial      nethogs        onscripter     pueue          strongswan
cfn-
lint goreleaser libvirt minio nfdump openimageio quantlib cimg grpc libzdb minio-mc nim pagmo shunit2 cjson haproxy mdbook monolith nomad phpmd spim ==> Installing dependencies for
python: openssl@1.1, sqlite and xz ==> Installing python dependency: openssl@1.1 ==> Downloading https://homebrew.bintray.com/bottles/[email protected] -=O=- # # # # curl: (7) Failed to connect to akamai.bintray.com port 443: Operation timed out Error: Failed to download resource "[email protected]" Download failed: https://homebrew.bintray.com/bottles/[email protected] Warning: Bottle installation failed: building from source. ==> Downloading https://www.openssl.org/source/openssl-1.1.1f.tar.gz ######################################################################## 100.0% ==> perl ./Configure --prefix=/usr/local/Cellar/openssl@1.1/1.1.1f --openssldir=/usr/local/etc/openssl@1.1 no-ssl3 no- ==> make ==> make test ==> make install MANDIR=/usr/local/Cellar/openssl@1.1/1.1.1f/share/man MANSUFFIX=ssl ==> Caveats A CA file has been bootstrapped using certificates from the system keychain. To add additional certificates, place .pem files in /usr/local/etc/openssl@1.1/certs and run /usr/local/opt/openssl@1.1/bin/c_rehash openssl@1.1 is keg-only, which means it was not symlinked into /usr/local, because openssl/libressl is provided by macOS so don't link an incompatible version.

問題解決方案

其實這一切的問題是在於我們安裝的brew預設repo是官方的repo,出於國內特色原因無法連線;所以使用 Alibaba 的 Homebrew 映象源進行加速後問題就迎刃而解。
平時我們執行 brew 命令安裝軟體的時候,跟以下 3 個倉庫地址有關:

  1. brew.git
  2. homebrew-core.git
  3. homebrew-bottles

1. 替換brew.git 倉庫地址:

cd "$(brew --repo)"
git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git

還原

cd "$(brew --repo)"
git remote set-url origin https://github.com/Homebrew/brew.git

2. 替換homebrew-core.git 倉庫地址:

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git

還原

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://github.com/Homebrew/homebrew-core.git

3. 替換homebrew-bottles 訪問地址

這個步驟跟你的 macOS 系統使用的 shell 版本有關係,先來檢視當前使用的 shell 版本

echo $SHELL
/bin/zsh

3.1 zsh替換成阿里巴巴的 homebrew-bottles 訪問地址:

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

還原

vi ~/.zshrc
# 然後,刪除 HOMEBREW_BOTTLE_DOMAIN 這一行配置
source ~/.zshrc

3.2 bash替換成阿里巴巴的 homebrew-bottles 訪問地址:

echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile

還原

vi ~/.bash_profile
# 然後,刪除 HOMEBREW_BOTTLE_DOMAIN 這一行配置
source ~/.bash_profile

配置完後再去安裝下某些工具和module時就能發現暢快無比了

eric-Pro homebrew-core % brew install python3 
==> Downloading https://mirrors.aliyun.com/homebrew/homebrew-bottles/bottles/python-3.7.7.catalina.bottle.tar.gz
######################################################################## 100.0%
==> Pouring python-3.7.7.catalina.bottle.tar.gz
==> /usr/local/Cellar/python/3.7.7/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/
==> /usr/local/Cellar/python/3.7.7/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/
==> /usr/local/Cellar/python/3.7.7/bin/python3 -s setup.py --no-user-cfg install --force --verbose --install-scripts=/
==> Caveats
Python has been installed as
  /usr/local/bin/python3

Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
  /usr/local/opt/python/libexec/bin

You can install Python packages with
  pip3 install <package>
They will install into the site-package directory
  /usr/local/lib/python3.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary