Homebrew下载慢或失败是因默认连接境外GitHub服务器,可通过一键脚本(推荐新手)、手动换源(适合已安装用户)或切换清华镜像源三种方式解决,并需验证仓库地址与环境变量是否生效。

如果您在Mac上运行brew install或brew update命令时,出现下载缓慢、超时甚至连接失败的情况,则很可能是Homebrew默认从GitHub境外服务器拉取数据所致。以下是针对该问题的多种配置方法:
一、使用一键安装脚本(推荐新手)
该方法将Homebrew核心程序安装与国内镜像源配置合并为单次操作,自动完成仓库地址替换和环境变量写入,避免手动出错。适用于尚未安装Homebrew或希望彻底重装的用户。
1、打开终端,粘贴并执行以下命令:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
2、等待脚本检测系统环境并显示镜像源菜单:
请选择下载源:1) 中科大 2) 清华大学 3) 腾讯云 4) 阿里云 5) 官方源
3、输入对应数字(例如输入1选择中科大源),按回车确认。
4、脚本将自动执行:Homebrew安装、brew.git与homebrew-core.git仓库换源、HOMEBREW_BOTTLE_DOMAIN环境变量写入~/.zshrc。
5、关闭并重新打开终端,运行brew -v验证版本,再执行brew update确认无报错即完成。
二、手动更换已有Homebrew的镜像源
该方法适用于已通过官方方式安装Homebrew的用户,全程可控、无需依赖第三方脚本,适合对系统环境有明确要求的开发者。
1、替换Homebrew主仓库地址:
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
2、替换核心软件包仓库(homebrew/core):
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
3、替换图形应用仓库(homebrew/cask):
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
4、配置预编译二进制包(bottles)镜像地址:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zshrc
5、使环境变量立即生效:
source ~/.zshrc
6、验证所有仓库源是否已更新:
cd "$(brew --repo)" && git remote -v
cd "$(brew --repo homebrew/core)" && git remote -v
cd "$(brew --repo homebrew/cask)" && git remote -v
三、切换为清华大学镜像源(备选方案)
当中科大源出现临时不可用或访问延迟较高时,可快速切换至清华大学TUNA镜像,其同步频率高、服务稳定,特别适合M1/M2芯片Mac用户。
1、替换主仓库为清华源:
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
2、替换核心公式仓库:
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-core.git
3、替换Cask仓库:
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/homebrew-cask.git
4、设置清华bottles地址:
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles' >> ~/.zshrc
5、重新加载配置并验证:
source ~/.zshrc && brew update
四、验证与故障排查关键步骤
完成任一配置后,必须执行以下检查以确认镜像生效且无残留问题,避免后续安装失败。
1、检查brew.git当前远程地址是否已变更:
cd "$(brew --repo)" && git remote get-url origin
2、检查homebrew-core.git是否指向国内源:
cd "$(brew --repo homebrew/core)" && git remote get-url origin
3、确认HOMEBREW_BOTTLE_DOMAIN环境变量已加载:
echo $HOMEBREW_BOTTLE_DOMAIN
4、强制刷新本地缓存并更新索引:
brew update --force
5、测试安装一个轻量工具验证全流程通畅:
brew install wget

















