Homebrew默认使用的是Github仓库,大陆访问Github不稳定,使用清华源替换。

源:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew/

替换已经安装的Homebrew仓库源

替换brew程序本身的源

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
brew update

替换Taps源

brew tap --custom-remote --force-auto-update homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
brew tap --custom-remote --force-auto-update homebrew/cask https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-fonts.git
brew tap --custom-remote --force-auto-update homebrew/cask-drivers https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-drivers.git
brew tap --custom-remote --force-auto-update homebrew/cask-versions https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask-versions.git
brew tap --custom-remote --force-auto-update homebrew/command-not-found https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-command-not-found.git
brew update

对于Brew程序本身和Core Tap,如果配置了环境变量,HOMEBREW_BREW_GIT_REMOTEHOMEBREW_CORE_GIT_REMOTE,则每次执行brew update时,Brew本身和Core Tap的远程地址会被自动配置。因此推荐将这个环境变量加入到shell的profile中。
例如ZSH:

echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"' >> ~/.zshrc
source ~/.zshrc

复原Homebrew仓库源

unset HOMEBREW_BREW_GIT_REMOTE
unset HOMEBREW_CORE_GIT_REMOTE

git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew
brew tap --custom-remote --force-auto-update homebrew/core https://https://github.com/Homebrew/homebrew-core
brew tap --custom-remote --force-auto-update homebrew/cask https://https://github.com/Homebrew/homebrew-cask
brew tap --custom-remote --force-auto-update homebrew/cask-fonts https://https://github.com/Homebrew/homebrew-cask-fonts
brew tap --custom-remote --force-auto-update homebrew/cask-drivers https://https://github.com/Homebrew/homebrew-cask-drivers
brew tap --custom-remote --force-auto-update homebrew/cask-versions https://https://github.com/Homebrew/homebrew-cask-versions
brew tap --custom-remote --force-auto-update homebrew/command-not-found https://https://github.com/Homebrew/homebrew-command-not-found
brew update
注:重置回默认远程后,用户应该删除 shell 的 profile 设置中的环境变量 HOMEBREW_BREW_GIT_REMOTEHOMEBREW_CORE_GIT_REMOTE 以免运行 brew update 时远程再次被更换。

Homebrew-bottles镜像配置

Homebrew-bottles是二进制预编译包。

临时替换

export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"

长期替换

添加到 shell 的 profile 中,例如ZSH。

echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zshrc
source ~/.zshrc

首次安装使用镜像

需要系统安装了bash、git和curl,Mac用户安装 Command Line Tools (CLT) for Xcode 即可。xcode-select --install

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
# 从本镜像下载安装脚本并安装 Homebrew / Linuxbrew
git clone --depth=1 https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install.git brew-install
/bin/bash brew-install/install.sh
rm -rf brew-install

# 也可从 GitHub 获取官方安装脚本安装 Homebrew / Linuxbrew
/bin/bash -c "$(curl -fsSL https://github.com/Homebrew/install/raw/master/install.sh)"
最后修改:2023 年 08 月 02 日
如果觉得我的文章对你有用,请随意赞赏