如何使用 Brew 安装旧公式?

How to install older formula using Brew?

使用安装 Python 2.7.9 而不是最新的 2.7.10 的情况,以前我可以简单地使用 brew versions python 并通过以下方式查看 Python 公式的所有版本他们在 brew 中提交 SHA,然后检查它们以安装特定版本。这是 brew versions python 输出的示例:

$ brew versions python
Warning: brew-versions is unsupported and will be removed soon.
You should use the homebrew-versions tap instead:
  https://github.com/Homebrew/homebrew-versions
2.7.9    git checkout 667284f /usr/local/Library/Formula/python.rb
2.7.8    git checkout f26ca5c /usr/local/Library/Formula/python.rb
2.7.7    git checkout d48206e /usr/local/Library/Formula/python.rb
2.7.6    git checkout 3c64184 /usr/local/Library/Formula/python.rb
2.7.5    git checkout a04b443 /usr/local/Library/Formula/python.rb
2.7.3    git checkout 865f763 /usr/local/Library/Formula/python.rb
2.7.4    git checkout 280581d /usr/local/Library/Formula/python.rb
2.7.2    git checkout 97c6869 /usr/local/Library/Formula/python.rb
2.7.1    git checkout 83ed494 /usr/local/Library/Formula/python.rb
2.7      git checkout 1bf3552 /usr/local/Library/Formula/python.rb
2.6.5    git checkout acd49f7 /usr/local/Library/Formula/python.rb
2.6.4    git checkout 843bff9 /usr/local/Library/Formula/python.rb
2.6.3    git checkout 5c6cc64 /usr/local/Library/Formula/python.rb

但是最新版本的brew已经移除了versions支持,我不明白如何使用https://github.com/Homebrew/homebrew-versions安装一个以前的Python。您实际上如何安装 Python 2.7.9 而不是较新的 2.7.10?

我想知道如何使用 homebrew-versions 而不是更琐碎的方法 brew versions

使用 brew versions 我可以很容易地看到该特定公式的所有版本(看看 brew versions python 吐出的上述 Python 版本)。从 doc 开始,没有明确的方法可以实现与 brew versions python 相同的结果。

homebrew-versions 曾经是最简单的方法,但 homebrew-versions 已被弃用,在当前版本的 homebrew 中不再可用。

要查找可用的版本,请使用以下命令:

brew search python

列出所有可用的 python 软件包,这些软件包将显示像 python@2 这样的旧版本,然后您可以使用以下命令安装它们:

brew install python@2

一些替代方法

切换到以前的版本

如果您已经安装了旧版本的公式并且没有删除它,您可以简单地切换符号链接以使用 brew 命令引用它。

brew switch python 2.7.9

此命令会将您切换到版本 2.7.9

brew switch python 2.7.10

这会将您切换回版本 2.7.10

公式GitHub历史

如果您的系统上仍然没有可用的旧版本,您可以尝试另一种方法,但它更难并且几乎肯定不受 Homebrew 支持,因此如果您最终遇到问题,您可能无法依赖在他们的帮助下。

https://github.com/Homebrew/homebrew-core/commits/master/Formul/<formula>.rb 应该会将您带到该公式的提交历史记录。对于安装 python 2.7.9 的示例,您将执行以下操作:

  1. 转到https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb
  2. 查找提交摘要为 "python 2.7.10"
  3. 的条目
  4. 查找并复制其下方条目的提交哈希(本例中为1681e19
  5. 在终端输入git checkout 1681e19 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/python.rb

从这一点开始,您可以像往常一样使用旧版本方法安装旧版本 python。这似乎是旧方法所做的全部。

Homebrew 不支持从 git 历史安装公式,尽管这是可能的。 homebrew-versions tap 中没有 python* 公式,因为我认为维护它们需要大量工作,而且无法提供最佳的用户体验。 pyenv is a great tool that solves a lot of the problems associated with keeping multiple pythons around. There's also a user-maintained tap 旧 Python 版本;你可以在那里贡献一个 2.7.9 公式。

如果您想使用 brew(自制软件)安装特定的 python 版本。

示例:我正在尝试安装 python 3.7,此时当前 python 是 python 3.8

brew install python@3.7 -- 这会让我安装 python 3.7

brew install python -- 这将使我在撰写此评论时安装 python 3.8,这是 python 的当前版本。