Homebrew(macOS/Linux 包管理)
Homebrew 在 macOS/Linux 下提供统一的软件管理方式。本文更新至 2025 年常用命令、镜像、常见故障排查以及与 AI/IDE 集成的建议。
简介
Homebrew 由以下部分构成:
名称 | 说明 |
---|---|
brew | Homebrew 核心命令与脚本 |
homebrew-core | 核心配方仓库(Formulae) |
homebrew-cask | 提供 macOS 应用与大型二进制安装(Casks) |
homebrew-bottles | 预编译二进制包(Bottles) |
安装与卸载
官方脚本
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
sh
macOS 需预装 Command Line Tools (
xcode-select --install
);Linux 需安装基本依赖(build-essential、curl、git)。
国内镜像脚本
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
sh
- 卸载:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"
sh
安装完成后的环境变量
安装脚本会提示将 Homebrew 路径加入 shell 配置,如:
# macOS Intel
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/usr/local/bin/brew shellenv)"
# macOS Apple Silicon
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
# Linux
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
sh
常用命令
brew update # 更新 brew 与仓库
dew upgrade # 升级已安装软件(注意可能需要维护)
brew install <pkg> # 安装 formula
brew install --cask <pkg> # 安装 cask 应用
dew list # 列出已安装软件
dew info <pkg> # 查看配置信息
dew services start <svc> # 管理背景服务(redis/mysql 等)
dew doctor # 自检环境
dew cleanup # 清理旧版本
sh
建议安装
brew bundle
,通过Brewfile
管理团队软件依赖。
镜像配置
中科大、清华源
# 中科大
git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
git -C "$(brew --repo homebrew/core)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-core.git
git -C "$(brew --repo homebrew/cask)" remote set-url origin https://mirrors.ustc.edu.cn/homebrew-cask.git
brew update
# 清华大学
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git
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
sh
Bottles 镜像
根据 Shell 类型写入环境变量(下例以 Zsh 为主):
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.ustc.edu.cn/homebrew-bottles' >> ~/.zprofile
source ~/.zprofile
sh
恢复默认:删除上述环境变量并还原仓库地址。
常见问题
问题 | 说明 | 解决方案 |
---|---|---|
brew doctor 提示权限问题 | 目录归属不正确 | 调整 /usr/local 或 /opt/homebrew 权限:sudo chown -R $(whoami) /usr/local/* |
安装慢/超时 | 网络受限 | 配置镜像、使用代理、断点续传 |
Apple Silicon 安装 x86 包 | 使用 Rosetta 或 arch -x86_64 brew install <pkg> | |
brew services 无法启动服务 | LaunchAgents 配置异常 | 使用 brew services cleanup ,检查日志 /usr/local/var/log |
冲突/重复安装 | Formula 与系统包重叠 | 使用 brew unlink 、brew link --overwrite 控制链接 |
高效使用技巧
brew search
查找软件;brew tap
添加第三方源。brew upgrade --greedy
升级 cask 应用;配合mas
管理 App Store 应用。- 在 CI/CD 中使用
brew bundle --no-lock
提升可重复性。 - 与 AI 助手结合,快速查询命令解释,但需人工确认执行的风险。
推荐 Brewfile 模板
# Brewfile
brew "git"
brew "node@20"
brew "pnpm"
brew "redis"
cask "visual-studio-code"
cask "docker"
ruby
执行 brew bundle install
自动安装清单。
建议团队维护公共 Brewfile,保证开发环境一致性。定期运行
brew update && brew upgrade
,同时关注升级带来的兼容性问题。
↑