How to Compile Python3.12 from Source on macOS Sonoma


Pre-requisites

Compiling CPython

Install dependencies

brew install pkg-config openssl@3.0 xz gdbm tcl-tk

Specify gdbm library

GDBM_CFLAGS="-I$(brew --prefix gdbm)/include"
GDBM_LIBS="-L$(brew --prefix gdbm)/lib -lgdbm"

Get CPython source code

git clone git@github.com:python/cpython.git -b 3.12

cd cpython
sudo chown -R $(whoami) .
git clean -fdx

Configure a Makefile

cd cpython
./configure --with-openssl="$(brew --prefix openssl@3.0)" --enable-optimizations

Build CPython binary

make -j2

Test Python

make test

Install Python

# Install a standalone version (e.g. python3.12)
sudo make altinstall

# Create symbolic link for python3
sudo make install

Run Python

python3.12

References