外观
Homebrew 安装 / 更新慢?换国内镜像源加速
mac 上 brew install 卡在 Updating Homebrew...、下载 bottle 只有几十 KB/s——因为 Homebrew 默认从 GitHub 和海外 CDN 拉取。换成国内镜像即可解决。Homebrew 有三处需要换源:brew 本体、core 仓库、bottles(预编译包)。
一、一次性设置镜像环境变量(推荐)
把下面几行写进 ~/.zshrc(mac 默认 zsh):
bash
# 以清华 TUNA 镜像为例
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"让配置生效并更新:
bash
source ~/.zshrc
brew update
BOTTLE_DOMAIN最关键——它决定预编译包(bottle)从哪下,直接影响brew install的速度。
二、切换已有仓库的 remote(如果之前装过)
如果你早就装了 Homebrew,除了设环境变量,还要把已有 git 仓库指向镜像:
bash
# brew 本体
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# homebrew-core(旧版结构才有;新版用 API 可跳过)
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git 2>/dev/null
brew update三、换其他镜像
除清华外,常用的还有中科大(ustc)、阿里云。把上面 URL 里的 mirrors.tuna.tsinghua.edu.cn 换成:
| 镜像 | 域名 |
|---|---|
| 清华 TUNA | mirrors.tuna.tsinghua.edu.cn |
| 中科大 USTC | mirrors.ustc.edu.cn |
| 阿里云 | mirrors.aliyun.com |
四、验证效果
bash
brew update # 应很快完成,不再卡 Updating
brew install wget # 观察 bottle 下载速度五、恢复官方源
想改回官方源,删掉 ~/.zshrc 里那几行 export,并把 remote 改回:
bash
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.git常见问题
Q:设了还是卡 Updating Homebrew?
- 确认
source ~/.zshrc已执行、变量已生效(echo $HOMEBREW_BOTTLE_DOMAIN)。 - 偶发可临时
export HOMEBREW_NO_AUTO_UPDATE=1跳过每次自动更新。
Q:cask(GUI 应用)下载慢? cask 的 app 通常从各软件官网下,镜像帮不上;这类需要 代理。
小结
- 换三处:brew 本体 + core + bottles,其中
HOMEBREW_BOTTLE_DOMAIN最关键。 - 老用户别忘了
remote set-url切换已有仓库。 - cask 下 GUI 应用慢是另一回事,走 代理。
本文仅供技术学习与研究,请遵守所在国家/地区法律法规。