Homebrew
它是 macOS 上的包管理器,用命令行安装、更新、卸载开发工具、命令行软件和部分图形应用,类似于Linux中的apt-get或yum。
安装xcode-select
对于 macOS 用户,系统自带 bash、git 和 curl,安装 CLT for Xcode 即可。
xcode-select --install
出现这行说明已经安装:xcode-select: note: Command line tools are already installed.
环境变量
Homebrew git repository
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 bottles
该镜像是 Homebrew 二进制预编译包的镜像(即 brew update 时所更新内容),请参考 Homebrew 镜像使用帮助。
echo 'export HOMEBREW_PIP_INDEX_URL="https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"' >> ~/.zprofile
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"' >> ~/.zprofile
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"' >> ~/.zprofile
立即生效:
source ~/.zprofile
脚本安装
# 从镜像仓库安装
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
官方脚本:/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
科大脚本:/bin/bash -c "$(curl -fsSL https://mirrors.ustc.edu.cn/misc/brew-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对基于 Intel CPU 设备上的 macOS 系统(命令行运行
uname -m应输出x86_64)的用户可跳过本步。
安装校验
brew -v
输出 Homebrew 5.x.x说明安装成功!
其他
文件权限问题
sudo mkdir -p /opt/homebrew
sudo chown -R $(whoami) /opt/homebrew
bottles镜像源
阿里云:
export HOMEBREW_PIP_INDEX_URL="http://mirrors.aliyun.com/pypi/simple"
export HOMEBREW_API_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.aliyun.com/homebrew/homebrew-bottles"
科大源:
export HOMEBREW_PIP_INDEX_URL="http://mirrors.ustc.edu.cn/pypi/simple"
export HOMEBREW_API_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.ustc.edu.cn/homebrew-bottles"
仓库目录说明
| 仓库类型 | 说明 | 默认地址 |
|---|---|---|
| brew.git | Homebrew 源代码仓库 | https://github.com/Homebrew/brew |
| homebrew-core.git | 核心软件包仓库 | https://github.com/Homebrew/homebrew-core |
| homebrew-cask.git | macOS 应用仓库 | https://github.com/Homebrew/homebrew-cask |
| homebrew-bottles | 预编译二进制包 | https://ghcr.io/v2/homebrew |
替换清华仓库
#设置Homebrew Git仓库地址
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 开发命令地址
brew tap --custom-remote homebrew/core https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
brew tap --custom-remote homebrew/cask https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git
#更新Homebrew 配置
brew update
校验:
#查看tap源
brew tap
#查看git仓库
git -C "$(brew --repo)" remote -v
#查看包镜像
brew config | grep -E 'ORIGIN|HOMEBREW_API_DOMAIN|HOMEBREW_BOTTLE_DOMAIN|HOMEBREW_PIP_INDEX_URL|HOMEBREW_PREFIX'
复原官方仓库
(感谢 Snowonion Lee 提供说明)
-
以下针对 macOS 系统上的 Homebrew
# brew 程序本身,Homebrew / Linuxbrew 相同 unset HOMEBREW_API_DOMAIN unset HOMEBREW_BREW_GIT_REMOTE git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew # 以下针对 macOS 系统上的 Homebrew unset HOMEBREW_CORE_GIT_REMOTE BREW_TAPS="$(BREW_TAPS="$(brew tap 2>/dev/null)"; echo -n "${BREW_TAPS//$'\n'/:}")" for tap in core cask; do if [[ ":${BREW_TAPS}:" == *":homebrew/${tap}:"* ]]; then # 只复原已安装的 Tap brew tap --custom-remote "homebrew/${tap}" "https://github.com/Homebrew/homebrew-${tap}" fi done # 重新拉取远程 brew update
注:重置回默认远程后,用户应该删除 shell 的 profile 设置中的环境变量 HOMEBREW_BREW_GIT_REMOTE 和 HOMEBREW_CORE_GIT_REMOTE 以免运行 brew update 时远程再次被更换。
官网:https://brew.sh
参考:https://mirrors.ustc.edu.cn/help/brew.git.html
引用:https://mirrors.tuna.tsinghua.edu.cn/help/homebrew