Installing specific version of Python on macOS using home brew


Recently, I was working on some code that required Python 3.6. Although, it would probably run on newer versions of Python, I did not want to take any chances. I wanted to run it on Python 3.6. Normally, I would use a Docker container to ensure a reproducible environment. However, Docker imposes its own complexities when it comes to integrating with IDEs such as Pycharm. A common issue is debugging code when its runtime environment is inside a Docker container. IDEs usually don’t make it easy to create a breakpoint in the IDE and have the code run inside a Docker container. However, if the interpreter and the dependencies are on your host OS, it is easy to set breakpoints and debug code in Pycharm.

Getting back to my issue, I typically use Homebrew on macOS to install packages such as Python. It is a fantastic package manager and I have used it for a very long time now. However, it lacks the feature to install a specific version of a package. In this case, I wanted to install the latest Python 3.6 on my macOS. The only problem was that Homebrew had updated Python to 3.7 and so there wasn’t an easy way to have it install 3.6.

After a bit of digging around, I remembered that Homebrew uses a git repository to store all of its formulae. So, it was a matter of finding the correct Git SHA where the Homebrew maintainers had last updated Python 3.6 and using it to install Python.

Here are the steps I took to install Python 3.6 on macOS using Homebrew and a bit of Git.

First, unlink existing Python

brew unlink python

Next, install Python using the following command

brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb

Here, f2a764ef944b1080be64bd88dca9a1d80130c558 is the Git SHA where I saw the last Python 3.6 update take place in the commit history.

If you’re interested, check out /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula. This is where all the Homebrew formulae live.

Dinesh

Senior Software Engineer @ Apple | ex-Yahoo Finance Eng | Distributed Computing @ Georgia Tech | Python, Java | Scalability, ML/AI, C* | 🇮🇳 | Opinions mine only

Leave a Reply