mac在代理环境下配置homebrew镜像的关键是关闭代理对国内镜像域名的干扰,并分别设置brew主程序、homebrew-core、homebrew-cask和homebrew-bottles四类仓库的清华镜像地址,再通过brew config、update和install验证生效。
mac 在代理环境下配置 homebrew 镜像,关键不是“绕过代理”,而是让 homebrew 明确知道该用哪个镜像源、并且确保它能稳定访问这些国内地址——因为多数代理(尤其是公司级或全局代理)反而会拖慢甚至阻断对清华、中科大等高校镜像站的直连。真正有效的做法是:**关闭代理对国内镜像域名的干扰,再精准设置四类仓库地址**。
确认当前代理是否影响 brew
先判断问题根源:执行 curl -v https://mirrors.tuna.tsinghua.edu.cn。如果返回 200 或 301,说明镜像站可直连;若超时或被重定向到代理认证页,则代理正在拦截国内域名。此时不建议让 brew 走代理,而应排除代理干扰。
- 检查终端是否设置了 http_proxy/https_proxy:运行 env | grep -i proxy
- 若存在且你不需要全局代理(比如只是浏览器或特定工具需要),临时取消:unset http_proxy https_proxy
- 如必须保留代理,可在 ~/.zshrc 中添加例外规则(以 zsh 为例):
export no_proxy="localhost,127.0.0.1,mirrors.tuna.tsinghua.edu.cn,mirrors.ustc.edu.cn,mirrors.aliyun.com"
一次性设置四类核心仓库镜像
Homebrew 实际由四个独立 Git 仓库组成,需分别配置。以下以清华大学镜像为例(稳定、同步及时,2026年仍可用):
-
brew 主程序仓库:
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git -
homebrew-core(命令行软件):
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-core.git -
homebrew-cask(GUI 应用):
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-cask.git -
homebrew-bottles(预编译二进制包):
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc && source ~/.zshrc
验证与收尾
执行以下命令确认全部生效:
- brew config → 查看 Core tap URL 和 Cask tap URL 是否已变更为清华地址
- brew update → 观察是否快速拉取(几秒内完成),而非卡在 “Updating Homebrew”
- brew install wget → 测试安装,确认下载速度达 MB/s 级别
若仍慢,检查 DNS 是否污染(可临时改用 114.114.114.114 或 223.5.5.5),或换用中科大源(mirrors.ustc.edu.cn)作为备选。










