Homebrew安装指南

说明

Brew全称叫Homebrew,是MacOS和Linux上的一个常用软件包管理工具。只需要一个命令, 安装和卸载它们非常方便。

安装

经典配置

由于网络环境的原因(直接连接 GitHub 通常不稳定或速度极慢),使用官方的安装命令往往会失败。最推荐的方案是使用国内开发者维护的自动化安装脚本,它会自动帮你把下载源替换为国内镜像(如中科大、清华大学等),速度快且成功率极高。

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

脚本运行后,会出现中文提示界面。请按照以下逻辑进行选择:

  1. 选择下载源: 脚本通常会列出多个镜像源(如 1.中科大 2.清华 3.腾讯 等),建议选择:1 (中科大) 或 2 (清华),这两个通常最稳定。输入数字并回车。

  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

常用命令

命令速查

命令

用途

brew install <name>

安装软件(如 brew install git

brew uninstall <name>

卸载软件

brew list

列出所有已安装的软件

brew search <text>

搜索软件(支持模糊匹配)

brew info <name>

查看软件详情(依赖、安装路径等)

brew update

更新 Homebrew 本身及仓库索引

brew upgrade

更新所有已安装的软件到最新版

brew cleanup

清理旧版本的安装包缓存,释放空间

示例:安装UV

(别问为啥装UV,问就是为了这盘醋包的饺子)

brew install uv


Homebrew安装指南
https://www.lsaika.com//archives/019bbf63-d1ad-73bc-9928-db0683fad41e
作者
saika
发布于
2026年01月15日
更新于
2026年01月15日
许可协议