Homebrew install specific version of formula?

后端 未结 27 2129
渐次进展
渐次进展 2020-11-21 11:21

How do I install a specific version of a formula in homebrew? For example, postgresql-8.4.4 instead of the latest 9.0.

27条回答
  •  时光说笑
    2020-11-21 12:14

    Official method ( judging from the response to https://github.com/Homebrew/brew/issues/6028 )

    Unfortunately Homebrew still doesn’t have an obvious builtin way of installing an older version.

    Luckily, for most formulas there’s a much easier way than the convoluted mess that used to be necessary. Here are the full instructions using bash as an example:

    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.
    

    This creates the formula@version in your custom tap that you can install per the above example. An important note is that you probably need to brew unlink bash if you had previously installed the default/latest version of the formula and then brew link bash@4.4.23 in order to use your specific version of Bash (or any other formula where you have latest and an older version installed).

    A potential downside to this method is you can't easily switch back and forth between the versions because according to brew it is a "different formula".

    If you want to be able to use brew switch $FORMULA $VERSION you should use the next method.


    Scripted Method (Recommended)

    This example shows installing the older bash 4.4.23, a useful example since the bash formula currently installs bash 5.

    • First install the latest version of the formula with brew install bash
    • then brew unlink bash
    • then install the older version you want per the snippets below
    • finally use brew switch bash 4.4.23 to set up the symlinks to your version

    If you performed a brew upgrade after installing an older version without installing the latest first, then the latest would get installed clobbering your older version, unless you first executed brew pin bash.

    The steps here AVOID pinning because it is easy to forget about and you might pin to a version that becomes insecure in the future (see Shellshock/etc). With this setup a brew upgrade shouldn't affect your version of Bash and you can always run brew switch bash to get a list of the versions available to switch to.

    Copy and paste and edit the export lines from the code snippet below to update with your desired version and formula name, then copy and paste the rest as-is and it will use those variables to do the magic.

    # 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
    

    When you are certain the version exists in the formula, you can use the below:

    # 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}')
    

    Once you have exported the commit hash you want to use, you can use this to install that version of the package.

    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
    

    Follow the directions in the formula output to put it into your PATH or set it as your default shell.

提交回复
热议问题