DEV Community

loading...

Install and manage multiple Python versions on macOS 11 (Apple Silicon) with pyenv

Squademy
Build the people and the software.
・2 min read

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
Enter fullscreen mode Exit fullscreen mode

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: 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. 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
Enter fullscreen mode Exit fullscreen mode

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 -)"
Enter fullscreen mode Exit fullscreen mode

3.Quit your terminal and reopen it. Now pyenv should be activated 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
Enter fullscreen mode Exit fullscreen mode

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. Make sure you have Xcode Command Line Tools (xcode-select --install) and Homebrew on your system. Then:

$ brew install openssl readline sqlite3 xz zlib
Enter fullscreen mode Exit fullscreen mode

For a complete guide on how to install Homebrew on a fresh macOS 11 Big Sur (Apple Silicon), check out this guide.

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
Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)