由于 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 切换到国内镜像源,从而加快软件包的下载速度。希望这个教程对您有所帮助!如果有任何问题,请随时提问。


架构师专栏
6.2k 声望7k 粉丝

引用和评论

0 条评论