Homebrew 换成国内源

以下操作都在macOS中进行

首次安装

先尝试用官网的脚本安装:https://brew.sh

如果失败,继续往下看。

安装 Command Line Tools (CLT) for Xcode

xcode-select --install

从以下镜像中选择一个即可,但是请注意随着时间的推移或政策的影响,镜像可能会失效。

清华镜像

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

在终端输入以下几行命令设置环境变量:

export HOMEBREW_INSTALL_FROM_API=1
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"
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"

在终端运行以下命令以安装 Homebrew :

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

安装成功后需将 brew 程序的相关路径加入到环境变量中。先判断设备类型:

uname -a

基于 Intel CPU 设备上的 macOS 系统(输出中含有 x86_64)的用户可跳过本步。

基于 Apple Silicon CPU 设备上的 macOS 系统(输出中含有 arm64)上的 Homebrew:

先判断shell的类型:

echo $SHELL

可能是bash,也可能是zsh,根据不同情况,将brew路径写入配置。比如zsh

echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile

中科大镜像

https://mirrors.ustc.edu.cn/help/brew.git.html

首先在命令行运行如下几条命令设置环境变量:

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.ustc.edu.cn/homebrew-core.git"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"
export HOMEBREW_API_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api"

之后在命令行运行 Homebrew 安装脚本:

/bin/bash -c "$(curl -fsSL https://mirrors.ustc.edu.cn/misc/brew-install.sh)"

已经安装

从以下镜像中选择一个即可,但是请注意随着时间的推移或政策的影响,镜像可能会失效。

清华镜像

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

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"
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_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"

中科大镜像

https://mirrors.ustc.edu.cn/help/brew.git.html

export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.ustc.edu.cn/brew.git"
brew update

阿里巴巴镜像

https://developer.aliyun.com/mirror/homebrew/

# 临时替换

export HOMEBREW_INSTALL_FROM_API=1
export HOMEBREW_API_DOMAIN="https://mirrors.aliyun.com/homebrew-bottles/api"
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_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"
brew update

腾讯软件源

https://mirrors.cloud.tencent.com/help/homebrew.html

cd "$(brew --repo)"
git remote set-url origin http://mirrors.cloud.tencent.com/git/homebrew/brew.git

cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin http://mirrors.cloud.tencent.com/git/homebrew/homebrew-core.git

brew update

换回 Homebrew 官方源

rm -rf $(brew --repo)/.git