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


当前回答

官方方法(从对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。

其他回答

官方方法(从对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。

基于@tschundee和@Debilski的更新1所描述的工作流,我自动化了该过程,并在此脚本中添加了清理。

下载它,将其放到您的路径中,然后brewv<formula_name><wanted_version>。对于特定OP,它将是:

cd path/to/downloaded/script/
./brewv postgresql 8.4.4

:)

更新Library/Formal/postgresql.rb第8行至

http://ftp2.uk.postgresql.org/sites/ftp.postgresql.org/source/v8.4.6/postgresql-8.4.6.tar.bz2

第9行的MD5

fcc3daaf2292fa6bf1185ec45e512db6

保存并退出。

brew install postgres
initdb /usr/local/var/postgres

现在在这个阶段,您可能会遇到postgresql无法创建共享内存段错误,要解决这个问题,请像这样更新/etc/sysctl.conf:

kern.sysv.shmall=65536
kern.sysv.shmmax=16777216

再次尝试initdb/usr/local/var/postgres,它应该运行平稳。

在启动时运行postgresql

launchctl load -w /usr/local/Cellar/postgresql/8.4.6/org.postgresql.postgres.plist

希望有助于:)

更新日期:2015年1月15日

查找所需软件和版本的提交历史记录。例如,我需要从docker版本1.4.1切换到1.3.3:https://github.com/Homebrew/homebrew-core/commits/master/Formula/docker.rb使用此按钮查看文件:单击“原始”按钮:从地址栏复制URL(本例中为docker.rb URL)brew install<url>(可能必须首先brew unlink,例如brew unlinkdocker)酿造开关装卸工1.3.3切换回码头1.4.1酿造切换码头1.4.1

根据这一要点

brew update
brew versions FORMULA
cd `brew --prefix`
git checkout HASH Library/Formula/FORMULA.rb  # use output of "brew versions"
brew install FORMULA
brew switch FORMULA VERSION
git checkout -- Library/Formula/FORMULA.rb    # reset formula

## Example: Using Subversion 1.6.17
#
# $ brew versions subversion
# 1.7.3    git checkout f8bf2f3 /usr/local/Library/Formula/subversion.rb
# 1.7.2    git checkout d89bf83 /usr/local/Library/Formula/subversion.rb
# 1.6.17   git checkout 6e2d550 /usr/local/Library/Formula/subversion.rb
# 1.6.16   git checkout 83ed494 /usr/local/Library/Formula/subversion.rb
# 1.6.15   git checkout 809a18a /usr/local/Library/Formula/subversion.rb
# 1.6.13   git checkout 7871a99 /usr/local/Library/Formula/subversion.rb
# 1.6.12   git checkout c99b3ac /usr/local/Library/Formula/subversion.rb
# 1.6.6    git checkout 8774131 /usr/local/Library/Formula/subversion.rb
# 1.6.5    git checkout a82e823 /usr/local/Library/Formula/subversion.rb
# 1.6.3    git checkout 6b6d369 /usr/local/Library/Formula/subversion.rb
# $ cd `brew --prefix`
# $ git checkout 6e2d550 /usr/local/Library/Formula/subversion.rb
# $ brew install subversion
# $ brew switch subversion 1.6.17
# $ git checkout -- Library/Formula/subversion.rb

以下是我如何降级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