Pyenv Quick Guide: Handling Dual Arch Python (ARM/x86) on Apple Silicon
Are you struggling to manage Python versions effectively on your Apple Silicon-based Mac (M1/M2/etc)? Transitioning between ARM and x86 architectures for Python environments can be a challenge. However, with the right tools and understanding, you can streamline this process.
I made a simple Pyenv plugin explicitly designed to simplify the management of both x86 and native ARM Python versions on Apple Silicon-based Mac computers. The primary motivation behind this initiative stems from the occasional incompatibility of certain Python packages on macOS with Apple Silicon chips, which typically arise when using older versions of packages in legacy projects.
The plugin’s functionality is straightforward: it identifies if Rosetta is active and appends the _x86
suffix to the installation path. This enables you to install a Python version built for arm64 alongside another compiled specifically for x86 architecture. Thus, allowing for the simultaneous installation of the same Python version tailored for different architectures.
Understanding Rosetta
Apple’s Rosetta is a crucial translation process that facilitates the execution of apps containing x86_64 instructions on Apple silicon. While Rosetta aids in transitioning to Apple silicon by emulating x86 architecture, it’s important to note that it’s not a substitute for creating native versions of applications.
For a more detailed understanding of Rosetta, refer to Apple’s official documentation.
Prerequisites
Here are the prerequisites before installing the Pyenv plugin:
- Ensure Homebrew (ARM) is installed: Run the command
which brew
in your terminal, expecting the output to be/opt/homebrew/bin/brew
. - Ensure Pyenv installed: If not already installed, you can install Pyenv using:
brew install pyenv
(Read more here) - Install Homebrew (x86) with Rosetta: Execute the following commands:
arch -x86_64 $SHELL # Run your shell using Rosetta.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Ensure it is installed under /usr/local/bin/brew
.
Install required packages:/usr/local/bin/brew install openssl readline
Those dependencies are required for building Python.
Using the Pyenv Plugin: Installation and Implementation
Plugin Installation: Clone the repository for the Pyenv plugin into your Pyenv plugins directory using this command:
git clone https://github.com/orlevii/pyenv-rosetta-suffix.git $(pyenv root)/plugins/pyenv-rosetta-suffix
Installing ARM64 Python Versions: To install a native ARM64 Python version, execute the standard command:
arch # Check output: arm64
pyenv install <version>
Installing x86 Python Versions: Installing x86 Python versions requires running your terminal in x86 mode:
arch -x86_64 $SHELL
arch # Check output: i386
# Set the x86 Homebrew in the path:
export PATH=/usr/local/bin:$PATH
pyenv install "<version>"
If you try to install a version of Python that already exists on your system, pyenv will display the following message:
pyenv: <python_path> already exists
Don’t worry, you can proceed with the installation. The installed version will have a _x86
suffix to distinguish it from the native arm64 version.
Usage Guidelines:
After successfully installing the Python version, you can create a virtual environment using the commands below:
# For arm64 Pythons:
~/.pyenv/versions/<python_version>/bin/python -m venv venv
# For x86 Pythons:
~/.pyenv/versions/<python_version>_x86/bin/python -m venv venv
These commands create separate virtual environments tailored to the architecture, allowing you to seamlessly operate between arm64 and x86 Python versions.
Summary
By leveraging this Pyenv plugin and understanding the nuances of managing Python versions between ARM and x86 architectures, you can navigate legacy projects on Apple Silicon-based MacBooks effortlessly.
Have more questions or looking for further guidance? Feel free to ask or refer to the plugin’s documentation for detailed instructions and troubleshooting.
References