MacOS and pyenv peculiarities


I’ve been using pyenv for ages to manage my python environments. I’ll leave singing the praises of this for others who do it well. Bascially I have a custom python environment for pretty much every project. This avoids dependency conflicts and ‘rot’ across projects.

It is an essential tool and I cannot imagine Python without it.

However, it is a bit difficult to remember the incantations needed to get it to build successfully on MacOS 10.15 and beyond. So I’m going to record this here and save myself the constant Google searching everytime I need to build a new environment or run into trouble. For bonus points I suppose I should add the error messages too so I can find this (and potentially others) while looking for how to solve the problem.

Building Python with pyenv on MacOS Catalina (and later)

LDFLAGS="-L $(xcrun --show-sdk-path)/usr/lib -L $(brew --prefix)/lib" \
CFLAGS="-I $(xcrun --show-sdk-path)/usr/include -I $(brew --prefix)/include" \
PYTHON_CONFIGURE_OPTS="--enable-framework" \
SDKROOT=$(xcrun --show-sdk-path) \
pyenv install -v 3.8.2

Change 3.8.6 to whatever mainline release you’d like to install.

2021-01-11: Updated for Big Sur