大概率是源的问题,要换成国内的源,但因为Homebrew 早就切换到 API 优先模式,默认不再克隆完整的 homebrew-core git 仓库,而是通过 API 拉取软件包元数据,所以本地可能没有传统的 homebrew-core 目录。所以不能按照老办法更新源。(老办法划了删除线,如果是之前的版本也是可以用的)
核心逻辑
API 模式下,切换镜像源的核心是让 Homebrew 的 API 请求指向国内镜像(而非 GitHub)
1、配置 brew 核心仓库镜像(基础前提)
bash
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
2、配置 API 镜像源(关键步骤,替代传统 git 仓库配置)
bash
# 对于 zsh 用户(默认)
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zshrc
# 对于 bash 用户
# echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.bash_profile
# 生效配置
source ~/.zshrc # 或 source ~/.bash_profile
确认自己是zsh还是bash用户的命令:echo $SHELL
3、测试更新
brew update --verbose
4、执行更新:brew update
以前更新源的方法
目前,清华大学的镜像源是 Homebrew 国内用户中最稳定和推荐的选择
步骤1:请按照以下命令,在终端中执行,以更换为清华大学镜像源:
bash
# 替换 brew.git
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 替换 homebrew-core.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 替换 homebrew-cask.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
# 替换 homebrew-cask-versions.git
git -C "$(brew --repo homebrew/cask-versions)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
# 替换 homebrew-services.git
git -C "$(brew --repo homebrew/services)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-services.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' >> ~/.bashrc
# source ~/.bashrc

步骤 2:清理并手动更新
更换完镜像源后,执行清理命令,然后手动触发一次更新,这将帮助 Homebrew 重新建立缓存。
bash
# 清理旧的缓存和下载
brew cleanup
# 手动更新 Homebrew
brew update