Mac使用清华源安装Homebrew
首次安装 Homebrew / Linuxbrew
首先,需要确保系统中安装了 bash、git 和 curl,对于 macOS 用户需额外要求安装 Command Line Tools (CLT) for Xcode。
- 对于 macOS 用户,系统自带 bash、git 和 curl,在命令行输入
xcode-select --install
安装 CLT for Xcode 即可。 - 对于 Linux 用户,系统自带 bash,仅需额外安装 git 和 curl。
接着,在终端输入以下几行命令设置环境变量:
if [[ "$(uname -s)" == "Linux" ]]; then BREW_TYPE="linuxbrew"; else BREW_TYPE="homebrew"; fi 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/${BREW_TYPE}-core.git" export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/${BREW_TYPE}-bottles"
最后,在终端运行以下命令以安装 Homebrew / Linuxbrew:(注意:在上面设置环境变量的终端执行哦)
# 从本镜像下载安装脚本并安装 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)"
安装成功后需将 brew 程序的相关路径加入到环境变量中:
-
以下针对基于 Apple Silicon CPU 设备上的 macOS 系统(命令行运行
uname -m
应输出arm64
)上的 Homebrew:test -r ~/.bash_profile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.bash_profile test -r ~/.zprofile && echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
source ~/.bash_profile
source ~/.zprofile -
对基于 Intel CPU 设备上的 macOS 系统(命令行运行
uname -m
应输出x86_64
)的用户可跳过本步。
替换现有仓库上游
替换 brew 程序本身的源,Homebrew / Linuxbrew 相同:
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
以下针对 macOS 系统上的 Homebrew:
# 手动设置 git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
执行:
brew update-reset