How can I install a previous version of Python 3 in macOS using brew?
With the command brew install python
I got the latest version of Python 3 (currently v3.7.0), but I want the last version of Python 3.6 (currently 3.6.5).
I have read about another package pyenv
that can assist in handle different python installation, but this solution is not suitable for me.
Short Answer
To make a clean install of Python 3.6.5 use:
If you prefer to recover a previously installed version, then:
Long Answer
There are two formulas for installing Python with Homebrew:
python@2
andpython
.The first is for Python 2 and the second for Python 3.
Note: You can find outdated answers on the web where it is mentioned
python3
as the formula name for installing Python version 3. Now it's justpython
!By default, with these formulas you can install the latest version of the corresponding major version of Python. So, you cannot directly install a minor version like 3.6.
Solution
With
brew
, you can install a package using the address of the formula, for example in a git repository.Or specifically for Python 3
The address you must specify is the address to the last commit of the formula (python.rb) for the desired version. You can find the commint identifier by looking at the history for homebrew-core/Formula/python.rb
https://github.com/Homebrew/homebrew-core/commits/master/Formula/python.rb
Python > 3.6.5
In the link above you will not find a formula for a version of Python above 3.6.5. After the maintainers of that (official) repository released Python 3.7, they only submit updates to the recipe of Python 3.7.
As explained above, with homebrew you have only Python 2 (python@2) and Python 3 (python), there is no explicit formula for Python 3.6.
Although those minor updates are mostly irrelevant in most cases and for most users, I will search if someone has done an explicit formula for 3.6.
As an update, when doing
You may encounter
To bypass it, add the
--ignore-dependencies
argument to brew install.What I did was first I installed python 3.7
brew install python3
thenbrew unlink python
then I installed python 3.6.5 using above linkbrew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb --ignore-dependencies
After that I ran
brew link --overwrite python
. Now I have all pythons in the system to create the virtual environments.To create Python 3.7 virtual environment.
To create Python 3.6 virtual environment
To solve this with
homebrew
, you can temporarily backdatehomebrew-core
and set theHOMEBREW_NO_AUTO_UPDATE
variable to hold it in place:I don't recommend permanently backdating homebrew-core, as you will miss out on security patches, but it is useful for testing purposes.
You can also extract old versions of homebrew formulae into your own tap (tap_owner/tap_name) using the
brew extract
command: