Zlib error when installing Pillow on Mac

This is what worked for me on M1 mac for python 2.7.18 on Monterey 12.0.1:

LDFLAGS="-L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib" CFLAGS="-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include" pip install Pillow

Successfully installed Pillow-6.2.2

Running

$ xcode-select --install

seems to have solved the problem for me.


For Mojave (10.14), you may also need to run the following command:

sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /