Homebrew是什么
Homebrew 是一個MacOS 上的包管理器,你可以用它來安裝管理軟件,功能非常強大,基本上只要是MacOS上能安裝的軟件,你通過Homebrew 都能夠安裝。類似于Linux上的yum,或者nodejs的npm。
有人可能會說,我要安裝軟件,直接到App Store上下載不就行了嗎?但是并不是所有,或者說,大部分軟件你在App Store上找不到的,特別是對于開發(fā)人員所需的軟件更是,基本在App Store找不到。
什么是Homebrew的源
其實就是一個git的url,對應(yīng)的是git倉庫。
每次通過Homebrew安裝軟件,Homebrew會根據(jù)源,會比對、下載(或者merge)源倉庫到本地,然后根據(jù)要下載的軟件找到對應(yīng)的xxx.rb文件,然后再根據(jù)rb文件中內(nèi)容一步步的校驗、下載、安裝軟件。
為什么要替換Homebrew的源
因為慢慢慢慢啊!Homebrew原始源是以下:
# homebrew源
https://github.com/Homebrew/brew.git
# homebrew-core源
https://github.com/Homebrew/homebrew-core.git
# homebrew-cask源
https://github.com/Homebrew/homebrew-cask.git
大家可以看到,都是github,國內(nèi)訪問這些源時,經(jīng)常性的慢出翔,所以必須替換為國內(nèi)源,加快速度。
開始替換(4個地方)
國內(nèi)源有幾個不錯的:
- 中科大 https://mirrors.ustc.edu.cn/
- 清華 https://mirrors.tuna.tsinghua.edu.cn/
- 阿里 https://developer.aliyun.com/mirror/ (這個還是不推薦了,連
home-cask都沒有)
以下是替換清華的源
# 替換brew.git源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替換 homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 替換 homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
其實替換這三個源就可以了,另外就是homebrew-bottles,這個配置個HOMEBREW_BOTTLE_DOMAIN環(huán)境變量,不過我發(fā)現(xiàn)我不配置也沒啥問題:
# 使用bash的話
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
# 使用zsh的話
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
以下是替換為中科大的源
# brew.git源
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
# homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
# homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
# 配置homebrew-bottles
## bash用戶
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.bash_profile
source ~/.bash_profile
## zsh用戶
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
source ~/.zshrc
復(fù)核配置是否成功
設(shè)置完之后,可以執(zhí)行以下命令,查看配置是否已經(jīng)替換成功
# 查看brew.git源
git -C "$(brew --repo)" config --get remote.origin.url
# 查看homebrew-core.git源
git -C "$(brew --repo homebrew/core)" config --get remote.origin.url
# 查看homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" config --get remote.origin.url
最后執(zhí)行brew update來嘗試一下看是否速度更快。
重置為官方源
如果你想重置為官方的源,可以如下操作:
# brew.git源
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
# homebrew-core.git源
git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core
# homebrew-cask.git源
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask
另外記住刪除 HOMEBREW_BOTTLE_DOMAIN 環(huán)境變量