本文会提供详细的配置步骤,通过国内镜像源加速 Homebrewbrew install 操作。🎯

正文

为什么需要配置国内镜像源?

在国内,使用 Homebrew 时,由于网络原因,往往会遭遇下载速度极慢或者根本无法连接的问题。😓 这不仅浪费时间,还可能影响工作进度。幸运的是,Homebrew 支持自定义镜像源,因此我们可以通过设置国内镜像来提高下载速度。

通过设置 国内镜像源,你可以在 brew install 时享受到更快的下载速度。💨

配置步骤:国内镜像源选择与设置

1. 选择国内镜像源

目前,许多国内开源社区提供了 Homebrew 镜像源,最常用的几个是:

2. 配置 Homebrew 镜像

在终端中执行以下命令,将 Homebrew 的源切换为 国内镜像源

清华大学镜像:
 1# 配置 Homebrew 核心镜像
 2git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
 3
 4# 配置 Homebrew 公式镜像
 5git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
 6
 7# 配置 Homebrew cask 镜像
 8git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
 9
10# 配置 Homebrew Bottles 镜像
11export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
中科大镜像:
 1# 配置 Homebrew 核心镜像
 2git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
 3
 4# 配置 Homebrew 公式镜像
 5git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
 6
 7# 配置 Homebrew cask 镜像
 8git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
 9
10# 配置 Homebrew Bottles 镜像
11export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles
阿里云镜像:
 1# 配置 Homebrew 核心镜像
 2git -C "$(brew --repo)" remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
 3
 4# 配置 Homebrew 公式镜像
 5git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
 6
 7# 配置 Homebrew cask 镜像
 8git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-cask.git
 9
10# 配置 Homebrew Bottles 镜像
11export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew-bottles
3. 更新与检查配置

设置完镜像源后,运行以下命令更新 Homebrew 配置并检查镜像源是否生效:

如果一切配置正确,你会看到国内镜像源正在被使用,而且速度会明显提升!⚡️

如何验证配置是否成功?

你可以通过运行以下命令来确认你的镜像源是否已经成功配置:

查看输出内容中的 HOMEBREW_GIT_REMOTEHOMEBREW_BOTTLE_DOMAIN,它们应该指向你设置的国内镜像源地址。

Warm Tips

常见问题

为什么镜像源切换后仍然很慢? 可能是因为 Homebrew 的缓存未清除。你可以尝试使用 brew cleanup 命令清除旧的缓存。

如何切换回官方源? 如果你想恢复使用官方源,只需执行以下命令:

 1git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git
 2git -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.git
 3git -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git
 4export HOMEBREW_BOTTLE_DOMAIN=https://homebrew.bintray.com

总结:加速你的开发之路

通过配置 国内镜像源,你可以大幅度提升 Homebrew 的安装速度,解决由于网络问题带来的困扰。🥳 你只需要按照上面的步骤,切换到合适的镜像源,便能享受稳定且快速的安装体验。

个人笔记记录 2021 ~ 2025