Apple Silicon Mac Development Environment Setup

miAlism

miAlism / November 28, 2021

3 min read

The ARM architecture provides a poweful performance for our development but it's also a barrier to setup dev environment quickly and easily. In this article, I will give my solution to setup a Python dev env on Apple Silicon Mac.

i386-arm64 terminal switch

With Rosetta, Terminal can run in both i386(Intel) and arm64(ARM) mode. On macOS, enter command arch you wil get the architecture info. We can duplicate the terminal and rename the duplication app with name started with Rosetta- like Rosetta-terminal. Right click Get info and select Open using Rosetta, and this makes the app running in i386 mode. If you want to configure mode prompt in terminal and switch these two modes quickly with several simple commands, check this video and this Gist for more tips.

Homebrew installation

You can install Homebrew in two versions for their own architecture. Open i386 and amr64 terminal seperately, and enter the same command

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

Homebrew for i386 exists in path /usr/local/homebrew/bin/brew and the arm version exists in /opt/homebrew/bin/brew. These are separate versions of each other.

Python version management.

It seems that Python 3.9.1 is the first verison to support Apple Silicon Macs, so if you want to install an older version like 3.7.2, you should use i386 Homebrew.

At first I choose asdf as the version management tool, but unlike in this video, I always get an error python-build: use zlib from xcode sdk... BUILD FAILED (OS X 12.0.1 using python-build 2.2.2-1-gf2925393). Actually the same error occured if I install Python 3.7.2 (which is not supported in Apple Silicon) with arm64 Homebrew directly. I haven't found the solution so far.

So I highly recommend pyenv as the version management tool. Maybe you will still get the error BUILD FAILED (OS X ...)..., the solution is here

Solution for Xcode Build failed error

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.7.2 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Attention for the version tag 3.7.2 in this command, if you want to install a different version, change the tag.

Addition

pyenv usage

pyenv virtualenv `python version` `venv name`

References