In this tutorial, we will learn how to make Python 3.9 the default Python version on your Mac. Most Mac systems come pre-installed with Python 2.7, which is an older version and no longer supported.
Python 3.9 is the latest version and comes with many new features and performance improvements. We will use brew and pyenv to install and manage different Python versions.
Step 1: Install Homebrew
First, we need to install Homebrew, which is a popular package manager for macOS. It allows us to install and manage different software packages on our system. If you don’t have it installed, open the Terminal application and run the following command:
1 |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
This command will download and install Homebrew on your system. Once it’s done, verify the installation by running:
1 |
brew --version |
Step 2: Install pyenv
Now that we have Homebrew installed, we can use it to install pyenv. pyenv is a Python version manager that allows us to have multiple Python versions installed on our system easily. Run the following command to install pyenv using Homebrew:
1 |
brew install pyenv |
After the installation is complete, we need to configure our shell to use pyenv. Add the following lines to your shell configuration file (.bashrc, .bash_profile, or .zshrc):
1 2 3 |
export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" |
Now, restart your terminal to apply the changes, or run the following command:
1 |
source ~/.bashrc |
or
1 |
source ~/.zshrc |
Depending on which shell configuration file you edited.
Step 3: Install Python 3.9
With pyenv installed, we can now install Python 3.9. Run the following command to download and compile Python 3.9.7:
1 |
pyenv install 3.9.7 |
Once the installation is complete, we can verify it by running:
1 |
pyenv versions |
This command will list all the Python versions installed on your system. You should see Python 3.9.7 in the list.
Step 4: Set Python 3.9 as the default Python version
Now that Python 3.9 is installed, we can set it as the default Python version for our system. Run the following command to set Python 3.9.7 as the default Python version:
1 |
pyenv global 3.9.7 |
After running this command, run the following command to verify that Python 3.9.7 is set as the default version:
1 |
python --version |
If everything went well, you should see “Python 3.9.7” as the output.
Full Code
The following code snippets were used in this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" brew --version brew install pyenv export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" source ~/.bashrc source ~/.zshrc pyenv install 3.9.7 pyenv versions pyenv global 3.9.7 python --version |
Conclusion
In this tutorial, we learned how to make Python 3.9 the default Python version on a Mac using Homebrew and pyenv. This allows us to use the latest version of Python and take advantage of its new features and performance improvements. Remember that you can use the same process to install and manage other Python versions using pyenv.