Do you need to run brew unlink / brew link before / after brew switch?

Viewed 2999

Do you need to run brew unlink / brew link before / after brew switch?

According to this link https://docs.brew.sh/Tips-N'-Tricks#activate-a-previously-installed-version-of-a-formula brew switch is activate a previously installed version of a formula.

Looks like I have some 'system' python installed(it's installed under /usr/bin/python3), that was not installed with brew, I can see it if I do brew unlink python@3.9 and brew unlink python@3.7:

python -V
Python 3.7.3

/usr/bin/python3 -V
Python 3.7.3

which python3
/usr/bin/python3

Then I switch to python@3.9:

brew switch python@3.9 3.9.0_1
python -V
Python 3.9.0

Then I switch to python@3.7:

brew switch python@3.7 3.7.9
python -V
Python 3.9.0

but it still show me Python 3.9.0, ls -l /usr/local/bin | grep python3 shows me python3.9 too.

I also tried to unlink all brew python packages before each brew switch test:

brew unlink python@3.7 && brew unlink python@3.9
brew switch python@3.7 3.7.9
python -V
Python 3.7.3


brew unlink python@3.7 && brew unlink python@3.9
brew switch python@3.9 3.9.0_1
python -V
Python 3.9.0

So seems for some reason it automatically links python@3.9 on brew switch and don't do it for python@3.7, why? Is brew switch have brew unlink / brew link inside it or should it be done manually?

Can I just do brew unlink / brew link to switch python version?

Update:

Seems newer versions of brew don't have switch:

Error: Calling `brew switch` is disabled! Use `brew link` @-versioned formulae instead.

brew --version
Homebrew 2.7.7
Homebrew/homebrew-core (git revision 918f0; last commit 2021-02-04)
Homebrew/homebrew-cask (git revision 2b83c; last commit 2021-02-04)

So now it should be something like this:

brew unlink python@3.7 && brew unlink python@3.9
brew link python@3.7
python -V
Python 3.7.9
1 Answers

brew switch is no more a brew command.

$ brew --version                                                                    
Homebrew 3.0.0
Homebrew/homebrew-core (git revision 8d644; last commit 2021-02-10)

$ brew switch mariadb@10.3                                                  
Error: Unknown command: switch

The Tips and Tricks page you were referring to in your question also remove any mention of brew switch.

Installing previous versions of formulae

The supported method of installing specific versions of some formulae is to see if there is a versioned formula (e.g. gcc@7) available. If the version you’re looking for isn’t available, consider using brew extract.

Either, no mention of brew switch in man brew.

So it seems that your question has been solved upstream by Homebrew maintainers.

Note that brew switch was deprecated since Homebrew 2.6.0 .

== Update ==

If we take your example with python@3.7 and python@3.9. We should first inspect the output of brew info python@3.7 and brew info python@3.9. We see that python@3.7 is keg-only:

python@3.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

which is not the case of python@3.9 because it is for now an alias of the python formula. This means that installing python@3.9 automatically run brew link python@3.9 and installing python@3.7 does not run brew link python@3.7.

Then to list which files will be linked with python@3.9, you can run brew link --dry-run python@3.9

Would link:
/usr/local/bin/2to3
/usr/local/bin/2to3-3.9
/usr/local/bin/easy_install-3.9
/usr/local/bin/idle3
/usr/local/bin/idle3.9
/usr/local/bin/pip3
/usr/local/bin/pip3.9
/usr/local/bin/pydoc3
/usr/local/bin/pydoc3.9
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.9
/usr/local/bin/python3.9-config
/usr/local/bin/wheel3
/usr/local/share/man/man1/python3.1
/usr/local/share/man/man1/python3.9.1
/usr/local/lib/pkgconfig/python-3.9-embed.pc
/usr/local/lib/pkgconfig/python-3.9.pc
/usr/local/lib/pkgconfig/python3-embed.pc
/usr/local/lib/pkgconfig/python3.pc
/usr/local/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/3.9
/usr/local/Frameworks/Python.framework/Versions/Current

To list which files will be linked with python@3.7, run brew link --dry-run python@3.7:

Would link:
/usr/local/bin/2to3
/usr/local/bin/2to3-3.7
/usr/local/bin/easy_install-3.7
/usr/local/bin/idle3
/usr/local/bin/idle3.7
/usr/local/bin/pip3
/usr/local/bin/pip3.7
/usr/local/bin/pydoc3
/usr/local/bin/pydoc3.7
/usr/local/bin/python3
/usr/local/bin/python3-config
/usr/local/bin/python3.7
/usr/local/bin/python3.7-config
/usr/local/bin/python3.7m
/usr/local/bin/python3.7m-config
/usr/local/bin/pyvenv
/usr/local/bin/pyvenv-3.7
/usr/local/bin/wheel3
/usr/local/share/man/man1/python3.1
/usr/local/share/man/man1/python3.7.1
/usr/local/lib/pkgconfig/python-3.7.pc
/usr/local/lib/pkgconfig/python-3.7m.pc
/usr/local/lib/pkgconfig/python3.pc
/usr/local/Frameworks/Python.framework/Headers
/usr/local/Frameworks/Python.framework/Python
/usr/local/Frameworks/Python.framework/Resources
/usr/local/Frameworks/Python.framework/Versions/3.7
/usr/local/Frameworks/Python.framework/Versions/Current

So to answer your question, one way to switch between python@3.7 and python@3.9 is by using brew link and brew unlink and yes this can break things if you have scripts which are compatible with python@3.7 and not with python@3.9 or vice versa.

There is another way to use different version of python in the same time. This is explained at the end of the output of brew link --dry-run python@3.7:

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/python@3.7/bin:$PATH"' >> ~/.zshrc

For scripts that neeeds python@3.7, you can set:

 export PATH="/usr/local/opt/python@3.7/bin

and for scripts that neeeds python@3.9, you can set:

 export PATH="/usr/local/opt/python@3.9/bin

Hope this helps.

Related