Homebrew卡在克隆或curl失败是因GitHub海外链路被限速,换清华或阿里镜像源可将速度从50KB/s提升至8MB/s以上;需先确认已安装并正常,再按步骤切换主仓库、core、cask及bottles源。

Mac上Homebrew安装或更新卡在Cloning into '/opt/homebrew/Library/Taps/homebrew/homebrew-core'、curl连接raw.githubusercontent.com失败、brew update长时间无响应,本质是官方源走海外GitHub链路被限速或中断。直接换清华或阿里镜像源,能让下载速度从50KB/s跃升至8MB/s以上,首次update也能顺利跑完。
确认当前Homebrew状态
打开终端,先检查是否已安装Homebrew并定位其架构路径:
执行 which brew,若返回 /opt/homebrew/bin/brew(Apple Silicon)或 /usr/local/bin/brew(Intel),说明已安装;若报command not found,需先安装再换源。
执行 brew --version 确认基础功能正常,避免在损坏状态下强行换源导致路径错乱。
一键切换为清华大学镜像源
清华TUNA镜像同步及时、北方用户延迟最低,适合绝大多数开发者。以下命令需逐行执行,不可合并粘贴:
第一步:切换brew主仓库源cd "$(brew --repo)" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
第二步:切换homebrew-core核心公式源cd "$(brew --repo homebrew-core)" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
第三步:切换homebrew-cask图形应用源(如VS Code、Google Chrome等)cd "$(brew --repo homebrew-cask)" && git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
第四步:设置预编译二进制包(bottles)下载地址——这一步最影响安装速度,【必须执行】:echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc && source ~/.zshrc
最后执行 brew update,首次运行会拉取新索引,耗时1–3分钟,无报错即生效。
快速切换为阿里云镜像源(推荐新手)
阿里云镜像采用商业级CDN,全国覆盖广、兼容性最强,首次换源出错率最低。只需一条命令即可完成全部替换:
方法一:使用社区维护的快捷脚本(推荐)
在终端中运行:curl -fsSL https://gitee.com/zhaiduting/zdt/raw/master/switch_brew_mirror | zsh,然后输入数字 1(阿里云)并回车。
方法二:手动设置环境变量(适合排查问题)
执行:export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/brew.git"
执行:export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-core.git"
执行:export HOMEBREW_CASK_GIT_REMOTE="https://mirrors.aliyun.com/homebrew/homebrew-cask.git"
执行:export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew-bottles"
立即生效:source ~/.zshrc
注意:手动设置的变量仅当前终端有效,如需永久生效,把四行 export 命令追加到 ~/.zshrc 文件末尾。
验证镜像源是否生效
执行 brew tap,观察输出中各tap的remote URL是否含 mirrors.tuna.tsinghua.edu.cn 或 mirrors.aliyun.com;
执行 brew config | grep "HOMEBREW_",确认所有关键变量已指向镜像地址;
运行 brew install wget,观察下载速度是否达数MB/s,而非持续卡在几十KB/s。

















