How to Install and Set the Latest Python Version as Default on macOS Sequoia and Sonoma

Tony KipkemboiTony Kipkemboi
4 min read

If you're a Mac user struggling to install the latest Python version and make it your system's default, here is an easy to follow solution for you.

Managing multiple Python versions on macOS can be a bit tricky, especially when you need the latest features or compatibility with new libraries. This tutorial will guide you through the process step-by-step, ensuring you have the latest Python version up and running as your default interpreter.

Why Update Python on Your Mac?

macOS comes with a pre-installed version of Python, but it's often outdated and not suitable for development purposes. Installing the latest Python version allows you to:

  • Access new language features.
  • Ensure compatibility with the latest libraries and frameworks.
  • Improve performance and security.

Prerequisites

  • macOS Sonoma or later: While these steps should work on earlier versions, they are tailored for macOS Sonoma.
  • Homebrew Installed: Homebrew is a package manager for macOS that simplifies the installation of software.

Step-by-Step Guide

1. Check Your Current Python Version

Before making any changes, it's good to know which Python version you're currently using.

python3 --version

Example Output:

Python 3.9.6

2. Install Homebrew (If not already installed)

If you don't have Homebrew installed, you can install it using the following command:

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

For more detailed instructions, visit the Homebrew website.

3. Update Homebrew

Before installing new packages, ensure Homebrew is up to date:

brew update

4. Install the Latest Python Version

As of writing this tutorial, the latest stable Python version is Python 3.12. Install it using Homebrew:

brew install python@3.12

This command downloads and installs Python 3.12 and its associated tools.

5. Adjust Your PATH Environment Variable

To make sure your system uses the newly installed Python version by default, you need to adjust your PATH environment variable.

Edit Your Shell Configuration File Depending on the shell you're using (likely zsh or bash), you'll need to edit either ~/.zshrc or ~/.bash_profile.

For Zsh (default on macOS Catalina and later):

nano ~/.zshrc

For Bash:

nano ~/.bash_profile

Add Homebrew's Python to Your PATH Add the following line at the top of the file:

export PATH="/opt/homebrew/bin:$PATH"

Note: On Intel-based Macs, use /usr/local/bin instead of /opt/homebrew/bin.

Save and Exit

  • In Nano editor, press CTRL + X, then Y, and hit Enter to save the changes.

Reload Your Shell Configuration Apply the changes immediately by running:

source ~/.zshrc

Or for Bash:

source ~/.bash_profile

6. Verify the Installation

Check that python3 now points to the latest version:

python3 --version

Expected Output:

Python 3.12.0

7. Check the Python Executable Path

Ensure that the python3 command is pointing to the correct executable:

which python3

Expected Output:

/opt/homebrew/bin/python3

8. Handling Multiple Python Versions

If you have multiple Python versions installed, you might want to manage them without uninstalling older versions.

Use Python Version Managers Consider using a Python version manager like pyenv to switch between different Python versions easily.

Install pyenv via Homebrew:

brew install pyenv

Install Python 3.12 Using pyenv:

pyenv install 3.12.0

Set Global Python Version:

pyenv global 3.12.0

9. Optional: Uninstall Older Python Versions

If you're certain you no longer need the older Python version, you can uninstall it to free up space.

Uninstall via Homebrew:

brew uninstall python@3.9

Note: If the older Python version wasn't installed via Homebrew, you'll need to remove it manually, which requires caution to avoid deleting system-critical files.

10. Troubleshooting Tips

  • Command Not Found Errors: If you encounter errors like command not found, ensure that you've correctly updated your PATH and reloaded your shell configuration.
  • Permission Issues: Avoid using sudo with brew commands, as Homebrew manages permissions internally.
  • Conflicting Python Versions: Use which -a python3 to list all python3 executables in your PATH and identify any conflicts.

Conclusion

By following these steps, you should have the latest Python version installed on your Mac and set as the default interpreter. This setup will allow you to take advantage of the newest Python features and ensure compatibility with up-to-date libraries.

Additional Resources

  • Homebrew Documentation: https://docs.brew.sh/
  • Python Downloads: https://www.python.org/downloads/
  • pyenv GitHub Repository: https://github.com/pyenv/pyenv

Feel free to share your thoughts or ask questions in the comments below!

Happy coding!

0
Subscribe to my newsletter

Read articles from Tony Kipkemboi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tony Kipkemboi
Tony Kipkemboi

Army veteran turned programmer and now a Developer Advocate building AI Agents. I love writing what I find inspiring and also practical.