DEV Community

Fahmi Nurfikri
Fahmi Nurfikri

Posted on • Originally published at Medium

How to Install Python Pyenv on macOS

One of the hardest processes to learning Python for beginners is installing Python. Sometimes it was a version problem, the machine doesn’t recognize commands, etc.

I was included with people who deal with those problems. My solution at that time was just to install the Anaconda package. But since the Anaconda package size is too big for what I need, I feel that method is too overkill for me.

After searching for a while, I got the solution to this problem, and the solution is Pyenv.

What is Pyenv?

Basically, Pyenv is a tool to simplify installation and version changing in Python. It helps developers quickly install or change the Python version without needing to change the whole system.

In this post, I will show you how to install Pyenv and manage the Python version.

Installing Homebrew

If Homebrew hasn’t been installed on your computer, run this command in the terminal.

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

It will take a while. After the installation process is done, it will show you a message like this.

Homebrew installation

For the next step, we will need to copy the text highlighted above to the terminal.

echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /Users/fahminurfikri/.zprofile
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/fahminurfikri/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
Enter fullscreen mode Exit fullscreen mode

When the process is finished, restart the terminal, and Homebrew is already installed on your computer.

Installing PyEnv using Homebrew

Once Homebrew is installed, we can use it to install Pyenv. This can be done by running the command below:

brew install pyenv
Enter fullscreen mode Exit fullscreen mode

Once the installation progress is finished, you can check it by using the command pyenv versions on your terminal. The output would be like this.

pyenv installation

If the output doesn’t show an error, then we can continue to the Python installation. To see the available Python version, you can use this command.

pyenv install --list
Enter fullscreen mode Exit fullscreen mode

For example, I will install Python 3.9.15. So the command will be like this.

pyenv install 3.9.15
Enter fullscreen mode Exit fullscreen mode

Don’t worry if your terminal doesn’t return anything, the process takes a while. Maybe you can leave it for a while to make coffee or something.

If the installation is already done, you can verify if the Python version is already installed, you can use the:

pyenv versions
Enter fullscreen mode Exit fullscreen mode

And the result will look like this.

* system (set by /home/user/.pyenv/version)
  3.9.15
Enter fullscreen mode Exit fullscreen mode

The * means that the system is the default Python version. To set Python 3.9.15 as the default Python, use the command:

pyenv global 3.9.15
Enter fullscreen mode Exit fullscreen mode

After that, check the Python version.

python --version  # or python -V
Enter fullscreen mode Exit fullscreen mode

If the Python version is 3.9.15 or whatever Python version you install, then the Python installation is a success. If you want to add another Python version, you can just use pyenv install <version> and if you want to change the Python version, you can just use pyenv global <version>.

Top comments (0)