如何给 Mac 上 brew 命令国内加速?

Mac 搭配 homebrew 安装软件非常方便,然而homebrew托管在github,对国内用户来说不仅频频被墙,而且速度也不理想。

对于这个问题,我们可以通过更换 homebrew 数据源的方法解决,下面介绍更换为国内的数据源的方法。

中科大的镜像

中科大镜像比较稳定,而且速度不错。官方网站:http://mirrors.ustc.edu.cn/。搜索brew,然后点击Help即可查看用法:

替换方法

1、替换Homebrew默认源

参考地址: https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git

# 替换brew.git:
cd "$(brew --repo)"
git remote set-url origin https://mirrors.ustc.edu.cn/brew.git

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

brew update

2、替换Homebrew Bottles源

参考地址:https://lug.ustc.edu.cn/wiki/mirrors/help/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

清华大学镜像

参考: https://mirror.tuna.tsinghua.edu.cn/help/homebrew/

切换回官方源方法:

1、重置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://github.com/Homebrew/homebrew-core.git

3、重置Homebrew Bottles源

注释掉bash配置文件里的有关Homebrew Bottles即可恢复官方源。 重启bash或让bash重读配置文件。

4、更新源信息

执行brew更新命令

brew update

引用链接

[1] http://mirrors.ustc.edu.cn/: http://mirrors.ustc.edu.cn/
[2] https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git: https://lug.ustc.edu.cn/wiki/mirrors/help/brew.git
[3] https://lug.ustc.edu.cn/wiki/mirrors/help/homebrew-bottles: https://lug.ustc.edu.cn/wiki/mirrors/help/homebrew-bottles
[4] https://mirror.tuna.tsinghua.edu.cn/help/homebrew/: https://mirror.tuna.tsinghua.edu.cn/help/homebrew/