Skip to content

Homebrew 使用指南

Mac 上最流行的包管理器,让你用命令行轻松安装、更新、卸载软件。


安装 Homebrew

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装完成后验证:

bash
brew --version

Formula vs Cask

类型说明示例
Formula命令行工具git, node, ffmpeg, wget
CaskGUI 图形应用Ghostty, Chrome, VS Code
  • 安装命令行工具:brew install <name>
  • 安装 GUI 应用:brew install --cask <name>

常用命令

安装与卸载

bash
# 安装命令行工具
brew install git

# 安装 GUI 应用
brew install --cask ghostty

# 卸载
brew uninstall git
brew uninstall --cask ghostty

搜索与查询

bash
# 搜索软件
brew search node

# 查看软件详情(版本、依赖、官网等)
brew info git

# 查看已安装的所有软件
brew list

# 查看已安装的 GUI 应用
brew list --cask

更新与升级

bash
# 更新 Homebrew 本身及软件源
brew update

# 升级所有已安装的软件
brew upgrade

# 仅升级某个软件
brew upgrade git

# 查看哪些软件有可用更新
brew outdated

维护与清理

bash
# 检查环境是否正常
brew doctor

# 清理旧版本和缓存
brew cleanup

# 清理指定软件的旧版本
brew cleanup git

锁定版本

bash
# 锁定某个软件,不随 upgrade 更新
brew pin git

# 解除锁定
brew unpin git

查看当前源

bash
# 查看 Homebrew 主源
git -C "$(brew --repo)" remote -v

# 查看 core 源
git -C "$(brew --repo homebrew/core)" remote -v

# 查看所有环境变量中的镜像配置
env | grep HOMEBREW

配置国内镜像(提升下载速度)

将以下内容添加到 ~/.zshrc

bash
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"

保存后执行:

bash
source ~/.zshrc

常见问题

权限报错

bash
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib

软件安装后找不到命令

bash
# 查看 Homebrew 安装路径
brew --prefix

# 确认 PATH 包含 Homebrew 路径(Apple Silicon Mac)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc
source ~/.zshrc

彻底卸载 Homebrew

bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

实用技巧

bash
# 查看某命令属于哪个包
brew which python3

# 查看包的依赖关系
brew deps git

# 查看哪些包依赖某个包
brew uses --installed git

# 只下载不安装(缓存到本地)
brew fetch git

快速参考

操作命令
安装brew install <name>
安装 GUI 应用brew install --cask <name>
卸载brew uninstall <name>
搜索brew search <name>
更新源brew update
升级全部brew upgrade
清理缓存brew cleanup
检查环境brew doctor

官方网站:https://brew.sh 软件包搜索:https://formulae.brew.sh

最近更新