6.1 brew bundle 入门
原文链接: https://docs.brew.sh/Brew-Bundle-and-Brewfile
声明式安装
一个最简单的 Brewfile 可以只写一个 formula:
执行:
(简写就是 brew bundle。)bundle 会读取 Brewfile,相当于执行 brew install ruby:
1
2
3
| $ brew bundle
Installing ruby
`brew bundle` complete! 1 Brewfile dependency now installed.
|
如果 Ruby 已经过时,它会执行 brew upgrade ruby;如果已经安装且是最新,则什么都不做:
1
2
3
| $ brew bundle install
Using ruby
`brew bundle` complete! 1 Brewfile dependency now installed.
|
安装前检查:brew bundle check
想知道执行安装会不会有实际动作:
若输出 “The Brewfile’s dependencies are satisfied.” 说明依赖已全部满足。可以把检查写进脚本:
1
| brew bundle check || brew bundle install
|
检查失败且没有 --verbose 时,可用 brew bundle check --verbose 列出未满足的依赖。
Brewfile 支持哪些类型
除了 formula,Brewfile 还支持 tap、cask、Mac App Store 应用、WSL 上的 WinGet 包、VSCode 扩展、Go 包、Cargo 包、npm 包、uv 工具、Flatpak 包和 krew kubectl 插件,以及通过 brew services 启动后台服务:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| tap "apple/apple"
brew "apple/apple/game-porting-toolkit"
brew "postgresql@16", restart_service: true
cask "firefox"
mas "Refined GitHub", id: 1519867270
winget "Steam", id: "Valve.Steam"
winget "PowerToys", id: "XP89DCGQ3K6VLD", source: "msstore"
vscode "editorconfig.editorconfig"
go "github.com/charmbracelet/crush"
cargo "ripgrep"
cargo "bat", source: "https://github.com/sharkdp/bat"
uv "mkdocs"
uv "ruff", source: "git+https://github.com/astral-sh/ruff.git"
krew "ctx"
flatpak "com.visualstudio.code"
|
安装结果会逐条显示 “Using …”:
1
2
3
4
5
6
7
8
9
10
11
| $ brew bundle
Using apple/apple
Using apple/apple/game-porting-toolkit
Using postgresql@16
Using firefox
Using Refined GitHub
Using editorconfig.editorconfig
Using github.com/charmbracelet/crush
Using ripgrep
Using mkdocs
`brew bundle` complete! 9 Brewfile dependencies now installed.
|
mas(Mac App Store)需要额外的 mas 工具;WinGet 仅用于 WSL;Flatpak 仅用于 Linux。
把 Brewfile 放进项目
把 Brewfile 提交到项目仓库,就像提交 package.json、Gemfile 或 requirements.txt 一样,可以让开发者用一条命令装齐项目依赖并启动服务:
Homebrew 同时支持 macOS、Linux 与 WSL,因此同一份 Brewfile 可用于本地不同平台,也可以用于 GitHub Actions 这类 CI 环境。GitHub 托管的 macOS runner 默认装有 Homebrew;Linux runner 可通过 Homebrew/actions/setup-homebrew 安装。
更新与升级行为
默认情况下 brew bundle 会尝试升级所有已过期的软件。若想让它只安装缺失项、不自动升级,可传 --no-upgrade 或设置:
1
| export HOMEBREW_BUNDLE_NO_UPGRADE=1
|
注意这只会跳过 brew upgrade,并不会固定版本,也不提供锁文件。关于版本策略的讨论见 6.2 用 brew bundle 管理机器状态。