How to Install Python Pyenv on macOS
Tags: python, apple, pyenv
Published: 2023-10-02 13:00:00

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.

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

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

![Homebrew installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/er8qhw3e034jrz8ukqpq.png)

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

``` bash
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)"
```

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:

``` bash
brew install pyenv
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cu1fpwaoyhc9lt4owmfl.png)

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.

``` bash
pyenv install --list
```

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

``` bash
pyenv install 3.9.15
```

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:

``` bash
pyenv versions
```

And the result will look like this.

``` bash
* system (set by /home/user/.pyenv/version)
  3.9.15
```

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

``` bash
pyenv global 3.9.15
```

After that, check the Python version.

``` bash
python --version  # or python -V
```

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>`.