laict.medium.com /install-python-on-macos-11-m1-apple-silicon-using-pyenv-12e0729427a9

Install Python on macOS 11 M1 (Apple Silicon) using pyenv

Tony Lai 6-8 minutes 2021/6/12

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:

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

$ brew install openssl readline sqlite3 xz zlib

To install Homebrew on a fresh macOS 11 Big Sur M1, check out this guide.

Install Python 3.9 on macOS 11 M1 (Apple Silicon)

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

Install Python 3.8 on macOS 11 M1 (Apple Silicon)

You need to use Homebrew’s patch to install Python 3.8 with pyenv. For the complete details, please read pyenv issue 1768.

If you want to jump right way to the answer, this comment is enough:

$ pyenv install --patch 3.8.6 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)

The patch for 3.8.3 applies to 3.8.6 without any issue:

$ pyenv install --patch 3.8.6 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.6.tar.xz...
-> https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tar.xz
Installing Python-3.8.6...
patching file Misc/NEWS.d/next/macOS/2020-05-18-02-43-11.bpo-34956.35IcGF.rst
patching file setup.py
patching file Mac/Tools/pythonw.c
patching file Misc/NEWS.d/next/macOS/2020-06-24-14-21-17.bpo-41101.z9hCsP.rst
patching file setup.py
patching file Doc/library/ctypes.rst
patching file Lib/test/test_bytes.py
patching file Lib/test/test_unicode.py
patching file Modules/_ctypes/_ctypes.c
patching file Modules/_ctypes/callproc.c
Hunk #4 succeeded at 1242 (offset 15 lines).
patching file Modules/_ctypes/ctypes.h
patching file Modules/_ctypes/callproc.c
patching file setup.py
patching file 'Misc/NEWS.d/next/Core and Builtins/2020-07-01-01-30-04.bpo-41100.hCrjJJ.rst'
patching file Doc/library/ctypes.rst
patching file configure
patching file configure.ac
patching file 'Misc/NEWS.d/next/Core and Builtins/2020-06-30-00-02-42.bpo-41164.zbzWsP.rst'
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.8.6 to /Users/squademy/.pyenv/versions/3.8.6

Install Python 3.7 on macOS 11 M1 (Apple Silicon)

Python 3.7.10 installed smoothly without any patch from Homebrew:

$ pyenv install 3.7.10
python-build: use openssl@1.1 from homebrew
python-build: use readline from homebrew
Downloading Python-3.7.10.tar.xz...
-> https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tar.xz
Installing Python-3.7.10...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.7.10 to /Users/squademy/.pyenv/versions/3.7.10

Install Python 3.6 or below on macOS 11 M1 (Apple Silicon) with pyenv

Other Homebrew patches are available here: 3.6.1, 3.7, 3.8.3, 3.8.7, arm64-3.9 A quick remind that 2.7 reached end of life (EOL) last year on 2020-01-01, 3.6 will reach EOL by the end of this year 2021. They shouldn't be used at all.

Post-install verification Python 3.7, 3.8 and 3.9

List all Python versions managed by pyenv:

$ pyenv versions
* system (set by /Users/squademy/.pyenv/version)
3.7.10
3.8.6
3.9.4

Set the default python to 3.8.6, cause that's what I want when typing python on the terminal

$ pyenv global 3.8.6 $ python --version 
Python 3.8.6

Verify pyenv shims is activated on the PATH:

$ type -a python
python is /Users/squademy/.pyenv/shims/python
python is /usr/bin/python

$ which python
/Users/squademy/.pyenv/shims/python

All python environments are natively installed with arch=arm64 on Apple Silicon (M1 Big Sur)

$ file /Users/squademy/.pyenv/versions/3.7.10/bin/python
/Users/squademy/.pyenv/versions/3.7.10/bin/python: Mach-O 64-bit executable arm64

$ file /Users/squademy/.pyenv/versions/3.8.6/bin/python
/Users/squademy/.pyenv/versions/3.8.6/bin/python: Mach-O 64-bit executable arm64

$ file /Users/squademy/.pyenv/versions/3.9.4/bin/python
/Users/squademy/.pyenv/versions/3.9.4/bin/python: Mach-O 64-bit executable arm64

Other guides

Install Homebrew on macOS 11 (Apple Silicon)

Install and configure Git on macOS 11 Big Sur (Apple Silicon)

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


订阅 substack 体验古早写作:


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::