Homebrew install specific version of formula?

后端 未结 27 2170
渐次进展
渐次进展 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条回答
  •  旧时难觅i
    2020-11-21 12:11

    Most of the other answers are obsolete by now. Unfortunately Homebrew still doesn’t have a builtin way of installing an outdated version, unless that version exists as a separate formula (e.g. python@2, postgresql@9.4 …).

    Luckily, for other formulas there’s a much easier way than the convoluted mess that used to be necessary. Here are the full instructions:

    1. Search for the correct version in the logs:

      brew log formula
      # Scroll down/up with j/k or the arrow keys
      # or use eg. /4\.4\.23 to search a specific version
      
      # This syntax only works on pre-2.0 Homebrew versions
      brew log --format=format:%H\ %s -F --grep=‹version› ‹formula›
      

      This will show a list of commit hashes. Take one that is appropriate (mostly it should be pretty obvious, and usually is the most recent (i.e. top) one.

    2. Find the URL at which the formula resides in the upstream repository:

      brew info ‹formula› | grep ^From:
      
    3. Fix the URL:

      1. Replace github.com with raw.githubusercontent.com
      2. Replace blob/master with the commit hash we found in the first step.
    4. Install the desired version by replacing master in the previously found URL by the commit hash, e.g.:

      brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/‹hash›/Formula/‹formula›.rb
      

    (The last step may necessitate running brew unlink ‹formula› before.)


    If you have copied a commit hash you want to use, you can use something like this example to install that version, replacing the value and bash with your commit hash and your desired formula.

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

    This example is installing bash 4.4.23 instead of bash 5, though if you performed a brew upgrade afterward then bash 5 would get installed over top, unless you first executed brew pin bash. Instead to make this smoother WITHOUT pinning, you should first install the latest with brew install bash, then brew unlink bash, then install the older version you want per the script above, and then use brew switch bash 4.4.23 to set up the symlinks to the older version. Now a brew upgrade shouldn't affect your version of Bash. You can brew switch bash to get a list of the versions available to switch to.


    Alternative using a custom local-only tap

    Another way of achieving the same goal appears to be:

    brew tap-new username/repo-name
    # extract with a version seems to run a grep under the hood
    brew extract --version='4.4.23' bash username/repo-name
    brew install bash@4.4.23
    # Note this "fails" when 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 a formula@version in your custom tap that you can install per the above example. The downside is that you probably still need to brew unlink bash and then brew link bash@4.4.23 in order to use your specific version of Bash or any other formula.

提交回复
热议问题