Apple's recent transition to the new architecture for its Mac computers has caused rather predictable problems for developers whose workflow depends on certain versions of pre-compiled libraries for x86 architecture. While the latest releases of Python come with a universal installer that allows to build universal binaries for M1 systems, those who prefer to manage Python environments with pyenv, may find it difficult to choose the correct version for installation.
This problem can be solved by installing both x86 and arm64 Python executables. To do that, we need to be able to run pyenv in x86 mode and make sure that all system dependencies are met for both architectures. In other words, we'll need both x86 and arm64 Homebrew packages that we'll keep separate using two installations of Homebrew.
First of all, to be able to run x86 executables, we'll need to install Rosetta:
$ softwareupdate â€”install-rosetta
Now we can install the x86 Homebrew:
$ arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
It will be installed in the /usr/local/bin/ directory. For convenience, you can create an alias by adding the following line in your shell configuration file:
alias brew86="arch -x86_64 /usr/local/bin/brew"
Now we can invoke the x86 Homebrew as brew86 and install packages required by pyenv:
$ brew install openssl readline sqlite3 xz zlib $ brew86 install openssl readline sqlite3 xz zlib
You can check whether the installation was successful and you have packages for both architectures using the file command, for example:
$ file /opt/homebrew/Cellaremail@example.com/1.1.1k/bin/openssl /opt/homebrew/Cellarfirstname.lastname@example.org/1.1.1k/bin/openssl: Mach-O 64-bit executable arm64 $ file /usr/local/Cellaremail@example.com/1.1.1k/bin/openssl /usr/local/Cellarfirstname.lastname@example.org/1.1.1k/bin/openssl: Mach-O 64-bit executable x86_64
To install x86 Python, you'll need to call pyenv with the arch -x86_64 prefix. For convenience, let's create an alias for this command by adding the following line in the shell config file:
alias pyenv86="arch -x86_64 pyenv"
Now you can install x86 Python binaries by calling:
$ pyenv86 install <PYTHON_VERSION>
By default, pyenv doesn't allow you to specify custom names for the installed Python versions, but you can use the pyenv-alias plugin to give your installations appropriate names:
$ VERSION_ALIAS="3.x.x_x86" pyenv86 install 3.x.x
Note that with aliases for your pyenv and Homebrew installations, youâ€™ll have to specify them in all commands and locations, for example:
$ CFLAGS="-I$(brew86 --prefix openssl)/include" \ LDFLAGS="-L$(brew86 --prefix openssl)/lib" \ VERSION_ALIAS="3.x.x_x86" \ pyenv86 install -v 3.x.x