4

Homebrew 安装指定版本的三种方式

 1 year ago
source link: https://shockerli.net/post/homebrew-install-formula-specific-version/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

方式一:官方多版本 formula

以 MySQL 为例

先搜索软件

brew search mysql
```shell

```shell
==> Formulae
automysqlbackup          mysql-client             mysql-sandbox            [email protected] ✔
mysql                    [email protected]         mysql-search-replace     mysqltuner
mysql++                  mysql-connector-c++      [email protected]                qt-mysql

==> Casks
mysql-connector-python            mysqlworkbench                    sqlpro-for-mysql
mysql-shell                       navicat-for-mysql

发现可用的版本有 mysql[email protected][email protected],那么我们可以安装的就是这几个大版本:

brew install [email protected]

缺点:多数软件是没有提供历史版本专属 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 update 被执行

HOMEBREW_NO_AUTO_UPDATE=1 brew install hugo
==> 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/[email protected]

搜索 hugo

➜ brew search hugo
==> Formulae
hugo                                               jioby/local-tap/[email protected]

安装 [email protected]

HOMEBREW_NO_AUTO_UPDATE=1 brew install jioby/local-tap/[email protected]

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

/opt/homebrew/Library/Taps/jioby/homebrew-local-tap/Formula/[email protected]

如果 Homebrew 中没有想要的具体版本,可以创建 Formula 副本后,自行用文本编辑器打开 .rb 文件(或使用 brew edit jioby/local-tap/[email protected],修改其中的 urlsha256 等,然后再执行 brew install xxx 进行安装。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK