mac上可通过设置homebrew_brew_git_remote、homebrew_core_git_remote、homebrew_bottle_domain等环境变量临时指定homebrew镜像,支持命令前加变量、export导出或子shell封装等方式,仅当前会话生效。
mac 上可以通过环境变量临时指定 homebrew 镜像,无需修改配置文件或永久生效,适合测试、调试或单次加速场景。关键在于设置一组特定的 homebrew_* 变量,覆盖默认的 git 仓库地址和二进制包域名。
必须设置的核心环境变量
Homebrew 自 2024 年起正式支持以下环境变量,它们在每次命令执行时优先级高于内置配置:
-
HOMEBREW_BREW_GIT_REMOTE:指向 brew 主程序自身的 Git 仓库(如清华镜像:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git) -
HOMEBREW_CORE_GIT_REMOTE:指向 homebrew-core 公式仓库(如:
https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git) -
HOMEBREW_BOTTLE_DOMAIN:指定预编译二进制包(bottles)下载地址(如:
https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles) - HOMEBREW_INSTALL_FROM_API=1(可选但推荐):启用新版 API 安装流程,提升稳定性
临时生效的几种方式
所有方式都只对当前终端会话或当前命令有效,关闭终端即失效:
- 直接在命令前加变量(最轻量):
HOMEBREW_BREW_GIT_REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git HOMEBREW_CORE_GIT_REMOTE=https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles brew update - 在当前终端中逐条导出(便于后续多条 brew 命令):
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"<br> export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"<br> export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"<br> export HOMEBREW_INSTALL_FROM_API=1
然后运行brew update或其他 brew 命令即可 - 用子 shell 封装(避免污染当前环境):
(export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew-bottles; brew install wget)
验证是否生效
执行以下命令检查当前生效的远程地址:
-
brew --repo→ 应显示你设的 HOMEBREW_BREW_GIT_REMOTE 地址 -
brew --repository homebrew/core→ 应匹配 HOMEBREW_CORE_GIT_REMOTE -
brew tap-info homebrew/core | grep "mirror"→ 若显示镜像域名,说明 bottle 源已识别
常见镜像地址参考(2026年稳定可用)
清华大学 TUNA(推荐):
brew: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
core: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
bottles: https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles
阿里云(部分地区更快):
bottles 单独换源可设为:https://mirrors.aliyun.com/homebrew-bottles(注意:阿里暂未提供 brew/core 的完整 Git 镜像,建议 brew 和 core 仍用清华或中科大)











