Install and manage multiple Python versions on macOS 11 (Apple Silicon) with pyenv
Tags: applesilion, pyenv, python, m1
Published: 2021-06-13 06:36:34

## A fresh Mac and its default Python
**macOS 11** comes with a default version of python2, which is now obsolete in favor of python3. Python2 reached end of life in January 2020.
```
$ type -a python
python is /usr/bin/python

$ which python
/usr/bin/python

$ python --version
Python 2.7.16
```
We are certainly do not want to touch macOS's default python2 and will leave it as it is. Let's install `pyenv` to manage multiple versions of Python.

## Install `pyenv` on macOS 11 (Apple Silicon)
There are two ways to install `pyenv` on macOS:
- Via [Homebrew](https://brew.sh/): this is the quickest way. Unfortunately I could not make `pyenv` works as expected on a fresh macOS 11 (Apple Silicon).
- Git checkout: `pyenv` team has documented the install process in details [here](https://github.com/pyenv/pyenv#basic-github-checkout). However, when I followed that step by step and applied on my new macOS 11 (M1), it did not work. After serveral hours tweaking, here's how I managed to get the `pyenv` to work with Git check out installation.

1.Clone the `pyenv` repo into your home folder:
```
$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src
```
2.Edit your `.zshrc` and add the following lines at the bottom of the file (macOS 11 comes with zsh as the default shell)

```
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
```
3.Quit your terminal and reopen it. Now `pyenv` should be [activated](https://github.com/pyenv/pyenv#how-it-works) and you can start to install some Python.

## My macOS 11 (Big Sur, Apple Silicon) system info before install Python

```
$ arch
arm64

$ which brew
/opt/homebrew/bin/brew

$ brew --version
Homebrew 3.1.11
Homebrew/homebrew-core (git revision 7c34424687; last commit 2021-06-10)
Homebrew/homebrew-cask (git revision ab9a64f927; last commit 2021-06-10)

$ pyenv --version
pyenv 2.0.1-3-g1706436f
```


## Install the Python build dependencies for macOS 11
`pyenv` suggests that before install any version of Python, we need a [Python build environment for Mac](https://github.com/pyenv/pyenv/wiki#suggested-build-environment). Make sure you have Xcode Command Line Tools (`xcode-select --install`) and Homebrew on your system. Then:
```
$ brew install openssl readline sqlite3 xz zlib
```
**For a complete guide on how to install Homebrew on a fresh macOS 11 Big Sur (Apple Silicon), check out [this guide](https://squademy.medium.com/install-homebrew-on-macos-11-apple-silicon-630f37a74490).**

## Install Python 3.9 on macOS 11 M1 (Apple Silicon) with `pyenv` 
```
$ pyenv install 3.9.4
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.9.4.tar.xz...
-> https://www.python.org/ftp/python/3.9.4/Python-3.9.4.tar.xz
Installing Python-3.9.4...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.9.4 to /Users/squademy/.pyenv/versions/3.9.4
```
`pyenv` install Python 3.9.4 seamlessly on Mac M1 and everything works out of the box.

## Final Note
This guide originally published and frequently updated [here](https://squademy.medium.com/install-python-on-macos-11-m1-apple-silicon-using-pyenv-12e0729427a9)