Homebrew 在 macOS 上因 GitHub 连接超时导致 brew update 失败,本质是默认源访问不稳定;需依次替换 brew 本体、homebrew-core、homebrew-cask 等仓库的 Git 远程地址为清华镜像,并配置 HOMEBREW_BOTTLE_DOMAIN 和 HOMEBREW_CORE_GIT_REMOTE 环境变量以实现长期加速。

Homebrew 在 macOS 上因 GitHub 连接超时导致 brew update 失败,本质是 Homebrew 的默认源(https://github.com/Homebrew/brew 和 https://github.com/Homebrew/homebrew-core)在国内访问不稳定。解决核心是替换为国内镜像源,而非单纯“科学上网”或反复重试。
切换 Homebrew 本体(brew)的 Git 远程地址
Homebrew 命令本身由 Git 仓库管理,需先修改其远程 origin:
- 进入 Homebrew 安装目录(通常为
/opt/homebrew,Intel 芯片为/usr/local/Homebrew) - 执行以下命令替换 brew 仓库源(以清华镜像为例):
cd /opt/homebrew<br>git -C . remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
验证是否生效:git -C . remote -v 应显示清华地址。
切换 homebrew-core 等核心配方仓库源
Homebrew 的公式(formulae)托管在 homebrew-core、homebrew-cask 等独立仓库,需分别设置:
- 切换
homebrew-core:
cd /opt/homebrew/Library/Taps/homebrew/homebrew-core<br>git -C . remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
- 如已启用 cask(常用 GUI 软件),同样切换:
cd /opt/homebrew/Library/Taps/homebrew/homebrew-cask<br>git -C . remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
其他 Tap(如 homebrew-services)可按相同路径和命令处理。
配置 Homebrew 镜像加速环境变量(推荐长期使用)
避免每次手动切源,可全局启用镜像加速。在 shell 配置文件(~/.zshrc 或 ~/.bash_profile)中添加:
export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles<br>export HOMEBREW_CORE_GIT_REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
保存后运行 source ~/.zshrc 生效。该配置让 Homebrew 自动从镜像下载二进制包(bottles)并识别 core 仓库新地址。
执行更新并验证
完成上述任一方式后,运行:
brew update
首次可能仍需几秒拉取,但不再卡在 GitHub timeout。成功后可测试安装一个常用工具(如 brew install wget)确认 bottle 下载也走镜像。
若某次更新失败,优先检查当前目录是否为 /opt/homebrew 或对应 Tap 目录,再确认 git remote -v 输出是否为镜像地址——多数问题源于只改了 brew 本体却漏掉 homebrew-core。

















