直接换源可显著提速,关键在于同步修改Homebrew本体、homebrew-core、homebrew-cask三个Git仓库地址,并配置HOMEBREW_BOTTLE_DOMAIN环境变量指向国内镜像(如清华源),再根据shell类型(zsh/bash)和芯片架构(arm64/x86_64)正确写入配置文件并重载。
直接换源就能明显提速,重点是改对三个地方:homebrew 自身、核心软件仓库(homebrew-core)、图形应用仓库(homebrew-cask),再配上 bottle 二进制包的镜像地址。
确认你的 shell 类型和芯片架构
打开终端,先运行这两条命令:
-
echo $SHELL —— 看输出是
/bin/zsh还是/bin/bash,决定后续改哪个配置文件 -
uname -m —— 输出
arm64是 M1/M2/M3/M4 芯片,x86_64是 Intel 芯片,关系到 brew 安装路径,但换源操作本身不受影响
一键替换为清华或中科大镜像源
推荐清华源(速度和稳定性更优),以下命令按顺序执行即可:
- 替换 Homebrew 本体仓库:
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 - 设置 bottle 镜像地址(关键!直接影响安装速度):
如果是 zsh:echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc && source ~/.zshrc
如果是 bash:echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.bash_profile && source ~/.bash_profile
验证是否生效
执行更新命令,观察响应速度和日志内容:
- 运行
brew update,如果看到类似remote: Counting objects: ...且几秒内完成,说明已连上清华镜像 - 运行
brew install wget(小体积工具),看下载速度是否达到 MB/s 级别 - 检查当前源地址:
git -C "$(brew --repo)" config --get remote.origin.urlgit -C "$(brew --repo homebrew/core)" config --get remote.origin.url
遇到问题怎么快速回退
如果换源后出现异常(如 brew doctor 报错或命令失效),可立即还原为官方源:
- 恢复三个 Git 仓库地址(依次执行):
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew.gitgit -C "$(brew --repo homebrew/core)" remote set-url origin https://github.com/Homebrew/homebrew-core.gitgit -C "$(brew --repo homebrew/cask)" remote set-url origin https://github.com/Homebrew/homebrew-cask.git - 删掉配置文件中那行
HOMEBREW_BOTTLE_DOMAIN,然后重新加载:
zsh 用户:用编辑器打开~/.zshrc删除对应行,再运行source ~/.zshrc
bash 用户同理处理~/.bash_profile - 最后执行
brew update完成还原


















