Mac M1: The quickest way of installing another Python version using pyenv on x86 when you already…
Here’s the step-by-step guide to installing another Python version using pyenv on an M1 Mac with Rosetta, for x86 architecture:
Prerequisite: Enable Rosetta for Terminal

1. Open the “Applications” folder.
2. Go to the “Utilities” folder.
3. Right-click on “Terminal” and select Get Info.
4. Check the option ”Open using Rosetta”.
5. Close any open Terminal instances, then reopen Terminal.

Now, you’re running Terminal under Rosetta, which allows x86 commands to work properly.

Step 1: Install a Separate Homebrew for x86

Since you already have Homebrew for arm64, you’ll need a separate instance for x86:

1. Install x86 Homebrew using Rosetta Terminal:

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

2. Set the path for x86 Homebrew binaries to ensure they are prioritized in your shell’s `$PATH`:

export PATH="/usr/local/bin:$PATH"

3. Verify the installation by checking the location of `brew`:

which brew

You should see `/usr/local/bin/brew` if the installation is correct.

Step 2: Install Dependencies for Python with Homebrew

In your Rosetta-enabled Terminal, install the necessary libraries for building Python:

1. Install OpenSSL, Readline, and Gettext:

brew install openssl@1.1 readline gettext

2. Set environment variables for the libraries:

export LDFLAGS="-L/usr/local/opt/readline/lib"
export CPPFLAGS="-I/usr/local/opt/readline/include"
export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"

3. Install additional dependencies (xz, zlib):

brew install xz zlib

Step 3: Set Environment Variables for Compiling Python

Set flags for the C and C++ compilers to find OpenSSL and system headers:

export CFLAGS="-Wno-implicit-function-declaration"
export CPPFLAGS="-I$(brew - prefix openssl@1.1)/include -I$(xcrun - show-sdk-path)/usr/include"
export LDFLAGS="-L$(brew - prefix openssl@1.1)/lib"

Step 4: Install the Required Python Version Using `pyenv`

Install Python 3.7.12 (or any other version) for x86 architecture:

arch -x86_64 pyenv install -v 3.7.12

The arch -x86_64 command ensures that pyenv runs in x86 mode under Rosetta.

Step 5: Verify Python Installation

pyenv versions

It should display the installed version, e.g., `Python 3.7.12`.

Following these steps, you’ll have successfully installed another Python version using `pyenv` under Rosetta for x86 on your M1 Mac. If you have any questions, feel free to ask :)