Homebrew install specific version of formula?

后端 未结 27 2133
渐次进展
渐次进展 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:11

    I've tried most of the solutions here and they are outdated. I had to combine some ideas from here with my own work. As a result I've created a script to help me do the heavy lifting which you can find here

    Usage:

    brewv.sh formula_name desired_version
    
    0 讨论(0)
  • brew versions and brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/<COMMIT-HASH>/Formula/<Formula>.rb not supported now.

    You can try like this:

    $ brew extract --version 5.6.2 gradle vitahlin/core
    $ brew install gradle@5.6.2
    
    0 讨论(0)
  • 2020-11-21 12:12

    An updated answer since that adds to what @lance-pollard already posted as working answer.

    How to Install specific version of a Formula (formula used in this example is terraform):

    1. Find your formula file, e.g: https://github.com/Homebrew/homebrew-core/blob/master/Formula/terraform.rb
    2. Get the commit version from github’s history with https://github.com/Homebrew/homebrew-core/commits/master/Formula/terraform.rb or git log master -- Formula/terraform.rb if you have cloned the repo locally.
    3. Get the raw git URL with the commit version of your formula: If the formula link in github.com is https://github.com/Homebrew/homebrew-core/blob/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb, your raw URL will be: https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb
    4. Install it with: brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/e4ca4d2c41d4c1412994f9f1cb14993be5b2c59a/Formula/terraform.rb
    0 讨论(0)
  • 2020-11-21 12:12

    it could be done very easy for last version of brew.

    brew tap homebrew/versions
    brew install subversion17 # for svn 1.7 branch instead of last available
    brew install postgresql8  # for postgresql 8 (which you ask)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 12:15

    There's now a much easier way to install an older version of a formula that you'd previously installed. Simply use

    brew switch [formula] [version]
    

    For instance, I alternate regularly between Node.js 0.4.12 and 0.6.5:

    brew switch node 0.4.12
    brew switch node 0.6.5
    

    Since brew switch just changes the symlinks, it's very fast. See further documentation on the Homebrew Wiki under External Commands.

    0 讨论(0)
提交回复
热议问题