How to Link and Set Homebrew Python 3.12 as the Default Python Version on macOS

Link Home Brew Python Version To Be Default

On macOS, Homebrew is a popular package manager that makes installing various Unix tools, languages, and software easy. Apple ships macOS with a system Python version installed by default (currently Python 3.10 or 3.11 depending on your OS version). However, you may need newer Python versions with the latest features and security updates.

While you can compile and install newer Python versions directly, using a package manager like Homebrew to install and manage multiple Python versions is highly recommended.

Homebrew formulas are self-contained bundles with everything you need to install a Unix tool or language. This means you get a consistent installation without needing to compile dependencies manually.

Use Homebrew to easily install Python 3.12 on macOS. Set it as the default Python version by unlinking the current default and forcing a link to [email protected]. Manage multiple versions with pyenv and create virtual environments for project-specific dependencies.

This guide will use the [email protected] Homebrew formula to install Python 3.12 on macOS. We will also link it to /usr/local/bin/python so that running Python launches Python 3.12 by default.

Also read: How to Uninstall Python 3.7 from Ubuntu

Step 1 – Install Homebrew (If not already installed)

If you don’t already have Homebrew installed, open the Terminal app and run:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This will install the latest Homebrew version along with dependencies like the Xcode command line tools.

Step 2 – Installing Python 3.12 with Homebrew

With Homebrew installed, run the following to install Python 3.12:

brew install [email protected]

This will download and install Python 3.12 without overwriting the system Python. It may take a few minutes to complete, depending on your internet speed.

Once installed, you can verify the new Python 3.12 version:

python3.12 --version
# Python 3.12.0

However, running Python still launches the system Python 3.11.6:

python --version  
# Python 3.11.6

We need to explicitly link Python 3.12 as the default python executable.

Step 3 – Setting Python 3.12 as the Default

Homebrew maintains symlinks in /usr/local/bin to manage version defaults for various tools and languages.

To set Python 3.12 as the new default executable for the python command, run:

brew unlink [email protected] && brew link --force --overwrite [email protected]

This first unlinks the previously default python version (3.11), then relinks [email protected], overwriting any conflicting symlinks.

Check the new default Python version:

python --version
# Python 3.12.0

Success! python now launches Python 3.12.

You can also update package managers like pip:

pip3 install --upgrade pip

To manage other Python versions installed with Homebrew, you simply need to unlink the current default before relinking a different version.

Step 4 – Managing Multiple Versions with pyenv

While Homebrew links provide an easy way to switch Python defaults, developers often need finer control for switching between versions for different projects.

This is where pyenv comes in.

pyenv is a popular Python version manager for macOS and Linux. It lets you:

  • Easily install multiple Python versions in user space without overrides
  • Switch between versions with simple commands/APIs
  • Set Python versions on a per-project basis

To install pyenv using Homebrew:

brew update
brew install pyenv

Next, initialize pyenv to enable shims and autocomplete:

pyenv init

Then reload your shell so pyenv changes take effect:

exec "$SHELL"

With pyenv ready, you can list available Python versions that can be installed:

pyenv install --list

Let’s install some Python versions:

pyenv install 3.11.6
pyenv install 3.12

To see installed Python versions managed by pyenv:

pyenv versions

  system
  3.7.0
* 3.11.6 (set by /Users/user/.pyenv/version)
  3.12.0

The asterisk shows the active global Python version.

To change the global version, use:

pyenv global 3.12

You can also set Python versions on a per-project basis.

For example, within a project folder containing a .python-version file with 3.10.9 value, pyenv will automatically switch to Python 3.10.9 when you cd into that folder.

These are just some basic examples of how powerful pyenv is for Python version management compared to Homebrew links.

Also read: How to Uninstall Python3 on MacOS?

Step 5 – Creating Isolated Environments

Whether you directly use Homebrew Python versions or manage them with pyenv, another best practice is to create self-contained virtual environments for your Python projects.

Virtual environments provide isolated spaces for Python projects, allowing you to:

  • Install project-specific packages without conflicts
  • Lock down specific package versions
  • Set custom Python versions per project

For example, after installing some Pythons with pyenv:

cd my_project
pyenv local 3.10.9
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

This switches the project folder to use Python 3.10.9, creates a .venv virtual environment, activates it, and installs packages just for this project.

The virtual environment remains isolated from global packages and other projects.

Modern Python projects should always use virtual environments to avoid version conflicts.

Summary

We walked through a complete guide to:

  • Install Python 3.12 using the Homebrew package manager
  • Link Python 3.12 as the new Python default executable
  • Use pyenv to install and switch between multiple Python versions easily
  • Create isolated virtual environments for Python projects

Homebrew combined with tools like pyenv provides flexible Python version management with minimal headaches.

Follow these best practices, and you’ll be able to smoothly upgrade and manage multiple Python versions for all your development projects on macOS!