主题模式
Homebrew 镜像:分层配置与恢复默认
Homebrew 的程序仓库、软件定义 API 和二进制 bottles 是不同下载层。现代默认安装通常无需先克隆 core/cask。以下以 Bash/zsh 的当前终端临时设置为例,两个镜像方案择一。
先记录现状:
bash
brew config
git -C "$(brew --repo)" remote -v
env | sort | grep "^HOMEBREW_"brew config 用于查看当前值,remote -v 看程序仓库来源,HOMEBREW_ 列表看当前终端覆盖项。把需要恢复的值保存到自己的笔记;输出可能包含内部地址,分享前移除。
使用中科大的镜像
bash
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git"
export HOMEBREW_API_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"
brew update
brew config三个变量分别控制 brew 程序仓库、软件定义 API 和 bottles。执行 update 后,在 config 中找对应 HOMEBREW_ 字段,再用 remote -v 看 origin;只改 bottles 不会让程序仓库的更新也走镜像。地址来自下方 USTC 使用说明。
使用清华大学的镜像
bash
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
brew update
brew config清华方案同样设置三项,不能把中科大与清华地址交叉粘贴。brew 4.0 起默认通过 API 安装,大部分用户无需克隆 core/cask,见TUNA 说明。当前终端可用后,再把同一组 export 写入所用 shell 的配置文件;不要创建旧 core 目录来凑路径。
恢复官方默认
bash
unset HOMEBREW_BREW_GIT_REMOTE HOMEBREW_API_DOMAIN HOMEBREW_BOTTLE_DOMAIN
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew
brew update
brew configunset 只清除当前终端的变量。若 ~/.zprofile 或 ~/.bash_profile 中还留有那三行 export,新开终端又会恢复镜像;删除对应行后再打开新终端查看 config。程序仓库 origin 已被 update 改写时,还需要上面的 remote set-url。
如曾设置 HOMEBREW_ARTIFACT_DOMAIN、core remote 或在 shell 配置文件里保存镜像,也要按之前记录逐项恢复。这里只恢复本例修改的配置。TLS 错误先查时间、代理与证书,不要关闭证书验证。