如何在自制程序中安装特定版本的公式?例如,postgresql-8.4.4而不是最新的9.0。


当前回答

我已经尝试了这里的大多数解决方案,但它们已经过时了。我不得不把这里的一些想法与我自己的工作结合起来。因此,我创建了一个脚本来帮助我完成你可以在这里找到的繁重任务

用法:

brewv.sh formula_name desired_version

其他回答

以下是我如何降级KOPS(不支持版本控制)

# brew has a git repo on your localhost
cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core

git remote -v
origin  https://github.com/Homebrew/homebrew-core (fetch)
origin  https://github.com/Homebrew/homebrew-core (push)

# find the version of kops.rb you need
git log Formula/kops.rb

# checkout old commit
# kops: update 1.18.1 bottle.
git checkout 2f0ede7f27dfc074d5b5493894f3468f27cc73f0 -- Formula/kops.rb

brew unlink kops
brew install kops

# now we have old version installed
ls -1 /usr/local/Cellar/kops/
1.18.1
1.18.2

which kops
/usr/local/bin/kops
ls -l /usr/local/bin/kops
/usr/local/bin/kops -> ../Cellar/kops/1.18.1/bin/kops
kops version
Version 1.18.1

# revert to the newest version
brew uninstall kops
git checkout -f
brew link kops
kops version
Version 1.18.2

这里的其他答案很好,但如果您需要安装较旧版本的软件包并确保修改了软件包名称,则需要另一种方法。当使用脚本(在我的例子中是PHP构建脚本)时,这一点非常重要,这些脚本使用brew前缀package_name来确定要用于编译的目录。

如果您正在使用brew提取,则会在包名称的末尾添加一个版本,这将中断brew前缀查找。

以下是如何在保持原始软件包名称的同时安装较旧的软件包版本:

# uninstall the newer version of the package that you accidentally installed
brew uninstall --ignore-dependencies icu4c

# `extract` the version you'd like to install into a custom tap
brew tap-new $USER/local-tap
brew extract --version=68.2 icu4c $USER/local-tap

# jump into the new tap you created
cd $(brew --repository $USER/local-tap)/Formula

# rename the formula
mv icu4c@68.2.rb icu4c.rb

# change the name of the formula by removing "AT682" from the `class` definition
# the exact text you'll need to remove will be different
# depending on the version you extracted
nano icu4c.rb

# then, install this specific formula directly
brew install $(brew --repository $USER/local-tap)/Formula/icu4c.rb

我在这里写了更多。

这些都不适用于我的案例(Python),所以我将添加我的2美分:

cd `brew --prefix`
git log Library/Formula/python.rb

输出如下所示:

提交文件作者:Dominik Tiller<dominyktiller@gmail.com>日期:2016年6月30日星期四17:42:18+0100python:澄清pour-bottle原因提交cb3b29b824a264895434214e191d0d7ef4d51c85作者:BrewTestBot<brew-test-bot@googlegroups.com>日期:2016年6月29日星期三14:18:40+0100python:更新2.7.12瓶。提交45bb1e20234184bbb7d6fd3f6df20987dc14f0作者:Rakesh<rakkesh@users.noreply.github.com>日期:2016年6月29日星期三10:02:26+0530python 2.7.12关闭#2452。签字人:Tim D.Smith<git@tim-smith.us>提交cf5da0547cd261f79d69e7ff62fdfbd2c2d646e9作者:BrewTestBot<brew-test-bot@googlegroups.com>日期:2016年6月17日星期五20:14:36+0100python:更新2.7.11瓶子。...

我想要2.7.11版本,所以我的哈希是cf5da0547cd261f79d69e7ff62fdfbd2c2d646e9(简称cf5da05)。接下来,我检查该版本并安装公式python:

git checkout cf5da05
brew install python

最后,清理:

git checkout master

官方方法(从对https://github.com/Homebrew/brew/issues/6028 )

不幸的是,Homebrew仍然没有明显的内置方式来安装旧版本。

幸运的是,对于大多数公式来说,有一种比过去必要的复杂混乱更容易的方法。以下是使用bash作为示例的完整说明:

brew tap-new $USER/local-tap
# extract with a version seems to run a `git log --grep` under the hood
brew extract --version=4.4.23 bash $USER/local-tap
# Install your new version from the tap
brew install bash@4.4.23
# Note this "fails" trying to grab a bottle for the package and seems to have
# some odd doubling of the version in that output, but this isn't fatal.

这将创建formula@version在您可以按照上面的示例安装的自定义抽头中。一个重要的注意事项是,如果您之前安装了默认/最新版本的公式,那么可能需要brew unlink bash,然后再brew linkbash@4.4.23以便使用您的特定版本的Bash(或安装了最新版本和较旧版本的任何其他公式)。

这种方法的一个潜在缺点是,你不能轻易地在不同版本之间来回切换,因为根据brew的说法,这是一种“不同的配方”。

如果你想使用酿造开关$FORMULA$VERSION,你应该使用下一个方法。


脚本化方法(推荐)

这个示例显示了安装较旧的bash 4.4.23,这是一个有用的示例,因为bash公式当前安装bash 5。

首先使用brew install bash安装最新版本的公式然后酿造unlink bash然后按照下面的代码段安装所需的旧版本最后使用brew开关bash 4.4.23设置到您版本的符号链接

如果您在安装旧版本后执行了brew升级,而没有先安装最新版本,那么最新版本的安装将与旧版本相冲突,除非您首先执行brew pin bash。

这里的步骤避免了钉扎,因为这很容易忘记,并且您可能会钉到将来变得不安全的版本(请参见Shellshock等)。通过这种设置,brew升级不会影响Bash的版本,您可以始终运行brew switch Bash来获取可切换到的版本列表。

复制、粘贴和编辑下面代码段中的导出行,以更新所需的版本和公式名称,然后按原样复制和粘贴其余内容,它将使用这些变量来实现神奇的效果。

# This search syntax works with newer Homebrew
export BREW_FORMULA_SEARCH_VERSION=4.4.23 BREW_FORMULA_NAME=bash
# This will print any/all commits that match the version and formula name
git -C $(brew --repo homebrew/core) log \
--format=format:%H\ %s -F --all-match \
--grep=$BREW_FORMULA_SEARCH_VERSION --grep=$BREW_FORMULA_NAME

当您确定公式中存在版本时,可以使用以下方法:

# Gets only the latest Git commit SHA for the script further down
export BREW_FORMULA_VERSION_SHA=$(git -C $(brew --repo homebrew/core) log \
 --format=format:%H\ %s -F --all-match \
--grep=$BREW_FORMULA_SEARCH_VERSION --grep=$BREW_FORMULA_NAME | \
head -1 | awk '{print $1}')

一旦导出了要使用的提交哈希,就可以使用它来安装该版本的包。

brew info $BREW_FORMULA_NAME \
| sed -n \
    -e '/^From: /s///' \
    -e 's/github.com/raw.githubusercontent.com/' \
    -e 's%blob/%%' \
    -e "s/master/$BREW_FORMULA_VERSION_SHA/p" \
| xargs brew install

按照公式输出中的指示将其放入PATH或设置为默认shell。

自那以后,一个更新的答案补充了@lance pollard已经发布的有效答案。

如何安装特定版本的公式(本示例中使用的公式为地形):

查找公式文件,例如:https://github.com/Homebrew/homebrew-core/blob/master/Formula/terraform.rb使用获取github历史记录中的提交版本https://github.com/Homebrew/homebrew-core/commits/master/Formula/terraform.rb或git-log-master--Formula/traform.rb(如果您在本地克隆了repo)。获取带有公式提交版本的原始gitURL:如果github.com中的公式链接是https://github.com/Homebrew/homebrew-core/blob/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb,原始URL将为:https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb使用:brew Install安装https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb