Homebrew安装指南
说明
Brew全称叫Homebrew,是MacOS和Linux上的一个常用软件包管理工具。只需要一个命令, 安装和卸载它们非常方便。
安装
经典配置
由于网络环境的原因(直接连接 GitHub 通常不稳定或速度极慢),使用官方的安装命令往往会失败。最推荐的方案是使用国内开发者维护的自动化安装脚本,它会自动帮你把下载源替换为国内镜像(如中科大、清华大学等),速度快且成功率极高。
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"脚本运行后,会出现中文提示界面。请按照以下逻辑进行选择:
选择下载源: 脚本通常会列出多个镜像源(如 1.中科大 2.清华 3.腾讯 等),建议选择:
1(中科大) 或2(清华),这两个通常最稳定。输入数字并回车。
输入密码: 脚本运行过程中可能会提示输入电脑的开机密码(输入时屏幕不会显示字符),输完按回车即可。
确认安装: 遇到提示是否继续安装时,按
Enter键或输入Y确认。
特殊配置
如果你之前在github下载的,现在想切到国内镜像源,请参考下面的操作
# 修改 brew 本身的代码源
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
# 修改核心库源 (针对还在使用 Git 模式的情况)
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
# 修改 Cask 库源 (针对还在使用 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
配置环境变量 (脚本通常会自动处理)
现在的自动化脚本通常会自动帮你配置好环境变量(.zshrc 或 .bash_profile)。
脚本运行结束后,它会提示你运行一条命令来使配置生效(通常是
source /Users/你的用户名/.zshrc)。或者,你可以直接重启终端(关闭窗口再重新打开)。
在新的终端窗口中,输入以下命令来检查是否安装成功:
brew -v更换镜像源
备注:可以不看我的镜像源配置,但是不要去看阿里源的官方配置,升级到brew4.0之后阿里源的brew镜像教程没更新
临时替换
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"永久替换
# 写入 .bash_profile
cat << 'EOF' >> ~/.bash_profile
# 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"
EOF
# 同时也建议写一份到 .bashrc (为了兼容性)
cp ~/.bash_profile ~/.bashrc
source ~/.bash_profile# 1. 写入清华源配置(使用 > 会覆盖之前可能重复的配置,确保干净)
cat << 'EOF' >> ~/.zshrc
# 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_CASK_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git"
EOF
# 2. 使配置立即生效
source ~/.zshrc恢复默认配置
unset HOMEBREW_BREW_GIT_REMOTE
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew
unset HOMEBREW_API_DOMAIN
unset HOMEBREW_CORE_GIT_REMOTE
BREW_TAPS="$(BREW_TAPS="$(brew tap 2>/dev/null)"; echo -n "${BREW_TAPS//$'\n'/:}")"
for tap in core cask{,-fonts,-versions} command-not-found services; do
if [[ ":${BREW_TAPS}:" == *":homebrew/${tap}:"* ]]; then
brew tap --custom-remote "homebrew/${tap}" "https://github.com/Homebrew/homebrew-${tap}"
fi
done
brew update
# 如果您之前永久配置了 HOMEBREW 环境变量,还需要在对应的 ~/.bash_profile 或者 ~/.zshrc 配置文件中,将对应的 HOMEBREW 环境变量配置行删除
brew update-reset brew doctor常用命令
命令速查
示例:安装UV
(别问为啥装UV,问就是为了这盘醋包的饺子)
brew install uvHomebrew安装指南
https://www.lsaika.com//archives/019bbf63-d1ad-73bc-9928-db0683fad41e