方式一:官方多版本 formula
以 MySQL 为例
先搜索软件:
brew search mysql
```shell
```shell
==> Formulae
automysqlbackup mysql-client mysql-sandbox mysql@5.7 ✔
mysql mysql-client@5.7 mysql-search-replace mysqltuner
mysql++ mysql-connector-c++ mysql@5.6 qt-mysql
==> Casks
mysql-connector-python mysqlworkbench sqlpro-for-mysql
mysql-shell navicat-for-mysql
发现可用的版本有 mysql
、mysql@5.7
、mysql@5.6
,那么我们可以安装的就是这几个大版本:
brew install mysql@5.7
缺点:多数软件是没有提供历史版本专属 formula,且每个都是大版本、无法具体版本号。
方式二:Formula Git 历史版本
以 hugo
为例。
查看软件的信息,获得其在 homebrew/core
仓库的文件路径:Formula/hugo.rb
➜ brew info htop
==> htop: stable 3.2.1 (bottled), HEAD
Improved top (interactive process viewer)
https://htop.dev/
/opt/homebrew/Cellar/htop/3.2.1 (12 files, 347KB) *
Poured from bottle on 2022-07-30 at 10:33:22
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git/Formula/htop.rb
查看 homebrew/core
的仓库路径:
➜ brew tap-info homebrew/core
homebrew/core: 3 commands, 6331 formulae
/opt/homebrew/Library/Taps/homebrew/homebrew-core (7,031 files, 514.4MB)
From: https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git
切换到 homebrew/core
的仓库路径下。
找到你需要 Checkout 至的 Commit。怎么找 Commit 呢?这介绍一种通过搜索 Git log 的方式:
➜ git:(master) git log -p -- Formula/hugo.rb | grep -e ^commit -e 'url "http'
commit 9e99a2778112fca08dad62b916cdbf3b2117135e
commit e2ba0778070f8c5fc5dc8f81c20923e7c45fc634
- url "https://github.com/gohugoio/hugo/archive/v0.104.2.tar.gz"
+ url "https://github.com/gohugoio/hugo/archive/v0.104.3.tar.gz"
commit 366f6fc756ce9ee434fb2af5fdc4c33697f8b24b
commit 0f7d523aff3152de4d64a17010bc17d10f9935cc
- url "https://github.com/gohugoio/hugo/archive/v0.104.1.tar.gz"
+ url "https://github.com/gohugoio/hugo/archive/v0.104.2.tar.gz"
比如想安装 hugo v0.104.2
,那么切换到指定 Commit 即可:
git checkout 366f6f
开始安装指定版本的软件:
HOMEBREW_NO_AUTO_UPDATE=1 brew install hugo
HOMEBREW_NO_AUTO_UPDATE=1
是禁止brew update
被自动执行
==> Downloading https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/hugo-0.104.2.arm64_monterey.bo
curl: (22) The requested URL returned error: 404
Warning: Bottle missing, falling back to the default domain...
==> Downloading https://ghcr.io/v2/homebrew/core/hugo/manifests/0.104.2
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/hugo/blobs/sha256:fb7f6d0b49f7c7248c820665cfed5b0b9
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:fb7f6d0b49f7c72
######################################################################## 100.0%
==> Pouring hugo--0.104.2.arm64_monterey.bottle.tar.gz
==> Caveats
zsh completions have been installed to:
/opt/homebrew/share/zsh/site-functions
==> Summary
🍺 /opt/homebrew/Cellar/hugo/0.104.2: 48 files, 55.8MB
==> Running `brew cleanup hugo`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
然后,别忘了切换回到最新 Git:
➜ git:(366f6fc756c) git checkout master
Previous HEAD position was 366f6fc756c hugo: update 0.104.2 bottle.
Switched to branch 'master'
方式三:自定义本地 Formula
创建本地 Tap(如已创建过,可跳过):
➜ brew tap-new $USER/local-tap
Initialized empty Git repository in /opt/homebrew/Library/Taps/jioby/homebrew-local-tap/.git/
[main (root-commit) d4dac7f] Create jioby/local-tap tap
3 files changed, 90 insertions(+)
create mode 100644 .github/workflows/publish.yml
create mode 100644 .github/workflows/tests.yml
create mode 100644 README.md
==> Created jioby/local-tap
/opt/homebrew/Library/Taps/jioby/homebrew-local-tap
When a pull request making changes to a formula (or formulae) becomes green
(all checks passed), then you can publish the built bottles.
To do so, label your PR as `pr-pull` and the workflow will be triggered.
在 /usr/local/Homebrew/Library/Taps/
下生成一个 Tap 模板 jioby/homebrew-local-tap
。
基于软件的特定版本创建 Formula 副本:
➜ brew extract --version=0.104.2 hugo $USER/local-tap
==> Searching repository history
==> Writing formula for hugo from revision 366f6fc to:
/opt/homebrew/Library/Taps/jioby/homebrew-local-tap/Formula/hugo@0.104.2.rb
搜索 hugo
➜ brew search hugo
==> Formulae
hugo jioby/local-tap/hugo@0.104.2
安装 hugo@0.104.2
HOMEBREW_NO_AUTO_UPDATE=1 brew install jioby/local-tap/hugo@0.104.2
不想再要这个临时 Formula,可手动删除:
/opt/homebrew/Library/Taps/jioby/homebrew-local-tap/Formula/hugo@0.104.2.rb
如果 Homebrew 中没有想要的具体版本,可以创建 Formula 副本后,自行用文本编辑器打开 .rb
文件(或使用 brew edit jioby/local-tap/hugo@0.104.2
,修改其中的 url
、sha256
等,然后再执行 brew install xxx
进行安装。