方式一:官方多版本 formula

以 MySQL 为例

先搜索软件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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

发现可用的版本有 mysqlmysql@5.7mysql@5.6,那么我们可以安装的就是这几个大版本:

1
brew install mysql@5.7

缺点:多数软件是没有提供历史版本专属 formula,且每个都是大版本、无法具体版本号。

方式二:Formula Git 历史版本

hugo 为例。

查看软件的信息,获得其在 homebrew/core 仓库的文件路径:Formula/hugo.rb

1
➜ brew info htop
1
2
3
4
5
6
==> 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 的仓库路径:

1
➜ brew tap-info homebrew/core                                                    
1
2
3
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 的方式:

1
➜ git:(master) git log -p -- Formula/hugo.rb | grep -e ^commit -e 'url "http'
1
2
3
4
5
6
7
8
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 即可:

1
git checkout 366f6f

开始安装指定版本的软件:

HOMEBREW_NO_AUTO_UPDATE=1 环境变量是为了关闭 brew update 被执行

1
HOMEBREW_NO_AUTO_UPDATE=1 brew install hugo
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
==> 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 到最新:

1
➜ git:(366f6fc756c) git checkout master
1
2
Previous HEAD position was 366f6fc756c hugo: update 0.104.2 bottle.
Switched to branch 'master'

方式三:自定义本地 Formula

创建本地 Tap(如已创建过,可跳过):

1
➜ brew tap-new $USER/local-tap
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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 副本:

1
➜ brew extract --version=0.104.2 hugo $USER/local-tap
1
2
3
==> 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

1
➜ brew search hugo
1
2
==> Formulae
hugo                                               jioby/local-tap/hugo@0.104.2

安装 hugo@0.104.2

1
HOMEBREW_NO_AUTO_UPDATE=1 brew install jioby/local-tap/hugo@0.104.2

不想再要这个临时 Formula,可手动删除:

1
/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,修改其中的 urlsha256 等,然后再执行 brew install xxx 进行安装。