[TOC] #### 前言 --- 当我们使用默认官方源时,经常会遇到以下问题  查看镜像配置 ```bash brew config ```  #### 镜像配置项 --- Homebrew 镜像配置分三层: | 层级 | 变量 | 作用 | 是否必须 | | ------------ | ------------ | ------------ | ------------ | | API(配方索引) | HOMEBREW_API_DOMAIN | 找软件(brew search/install) | 推荐 | | Bottle(二进制包) | HOMEBREW_BOTTLE_DOMAIN | 下软件(安装包从这里获取的) | 推荐 | | Git 仓库(brew) | HOMEBREW_BREW_GIT_REMOTE | 更新程序(控制 brew update 从哪更新) | 可选 | | Git 仓库(core) | HOMEBREW_CORE_GIT_REMOTE | 存放着所有软件的安装脚本(Formula) | 可选 | | Git 仓库(cask) | HOMEBREW_CASK_GIT_REMOTE | macOS 专用,安装图形界面软件 | 可选 | ```bash # API 源镜像:控制 formula / cask 元数据来源,解决 brew install 慢、GitHub API 慢的问题 export HOMEBREW_API_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api" # Bottle 二进制包镜像:控制下载软件包(.bottle.tar.gz),解决下载失败、安装慢的问题 export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles" ``` ```bash export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git" ``` 什么情况下需要配置这些 ? + HOMEBREW_API_DOMAIN、HOMEBREW_BOTTLE_DOMAIN 安装软件需要,直接配置上 + brew tap/update 慢或失败,考虑配置 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE #### 4.x 以上镜像配置 ---- 温馨提示:不要使用阿里云的 Homebrew 源,好久没更新了,而且还不支持 Homebrew 4.0 的镜像源 ##### [清华大学镜像站](https://mirrors.tuna.tsinghua.edu.cn/help/homebrew) ```bash # homebrew 4.x 清华大学镜像 最新的 JSON API 安装方式 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" ``` ##### [中国科学技术大学镜像源](https://mirrors.ustc.edu.cn/help/brew.git.html) ```bash # homebrew 4.x 中科大镜像 最新的 JSON API 安装方式 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" ``` 如果 shell 为 zsh,也就是添加 `~/.zshrc` 文件中,然后执行以下命令或重新打开窗口 ```bash source ~/.zshrc ``` #### 4.x 以前镜像配置 ---- Zsh 终端配置[(阿里云镜像源)](https://developer.aliyun.com/mirror/homebrew) ```bash # 替换brew.git: cd "$(brew --repo)" git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git # 替换homebrew-core.git: cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git # 应用生效 brew update # 替换homebrew-bottles: echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc source ~/.zshrc ``` 恢复 Homebrew 默认配置 ```bash # 替换 brew.git cd "$(brew --repo)" git remote set-url origin https://github.com/Homebrew/brew.git # 替换 homebrew-core.git cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core" git remote set-url origin https://github.com/Homebrew/homebrew-core.git # 应用生效 brew update # 删除 homebrew-bottles: # 注释掉 .zshrc 中的 HOMEBREW_BOTTLE_DOMAIN source ~/.zshrc ``` #### 可能会遇到的问题 ---- 运行 `brew config` 命令,看到 `ORIGIN` 的配置是中科大的镜像,但是当前并没有在 `~/.zshrc` 看到这个配置 ```plaintext $ brew config HOMEBREW_VERSION: 5.1.5 ORIGIN: https://mirrors.ustc.edu.cn/brew.git .... ``` 这是一个非常典型的 “环境变量未显式设置,但配置已生效” 的情况 Homebrew 本质上是一个 Git 仓库,当使用国内镜像(如中科大 USTC)安装 Homebrew 时,安装脚本通常会直接将仓库克隆为镜像地址,或者你后来运行了 `git remote set-url origin ...` 命令 这些信息存储在 Homebrew 核心目录下的 `.git/config` 文件中,与你的 Shell 配置文件(.zshrc)无关 通过以下命令查看当前生效的源: ```bash # macOS ARM cat /opt/homebrew/.git/config # Linux(Ubuntu) cat /home/linuxbrew/.linuxbrew/Homebrew/.git/config # 还可以使用 git 命令查看 Homebrew 核心仓库的 Git 配置(macOS ARM) git -C /opt/homebrew config --get remote.origin.url ``` 恢复 Homebrew 核心仓库源(重置为官方源) ```bash # macOS ARM git -C /opt/homebrew remote set-url origin https://github.com/Homebrew/brew # Linux(Ubuntu) git -C /home/linuxbrew/.linuxbrew/Homebrew remote set-url origin https://github.com/Homebrew/brew ```