Installing Python 2 on macOS Using Pyenv

Python 2 may be considered outdated, but there are still scenarios where you need it. If you’re a macOS user, here’s a straightforward guide to help you install Python 2 using Pyenv, a powerful Python version management tool. Before we begin, ensure you have Homebrew (brew) installed, as it simplifies the process.

Prerequisite:

  • Homebrew (brew) should be installed.

Follow these steps:

Step 1:

Install Pyenv Open your terminal and run the following command to install Pyenv using Homebrew:

brew install pyenv

Step 2:

Verify Pyenv Installation Ensure that Pyenv is successfully installed by checking its version:

pyenv --version

You should see something like:

pyenv 2.3.27

Step 3:

Install Python 2 Now that Pyenv is in place, you can easily install Python 2.7.18 with the following command:

pyenv install 2.7.18

This step might take a few minutes, depending on your internet speed.

Step 4:

Verify Python 2 Installation To confirm that Python 2.7.18 is installed correctly, run the following:

python --version

You should see the Python 2 version displayed.

Python Version Output
Python Version Output

Step 5:

Configure Your Shell Profile To make your Python 2 installation accessible from anywhere in your terminal, you need to add its path to your shell profile. Open your shell profile configuration file (usually ~/.bashrc for Bash or ~/.zshrc for Zsh) in your preferred text editor. For example:

vi ~/.zshrc

Step 6:

Add Python 2 Path Within your shell profile configuration, add the following line at the end, replacing /Users/your_username/ with your actual username:

export PATH="/Users/your_username/.pyenv/versions/2.7.18/bin:$PATH"

Save the file and exit the text editor.

Step 7:

Source Your Shell Profile To apply the changes immediately, source your shell profile:

source ~/.zshrc

Now, Python 2 is installed on your macOS using Pyenv, and you can access it from anywhere in your terminal. This guide ensures you can continue to work with Python 2 when needed while maintaining a clean and organized development environment.

Related Post