pyenv zlib error on MacOS:

XcodeMacosZlibPyenv

Xcode Problem Overview


My goal

I am trying to install Python 2.7.5 and 3.6.5 side-by-side on my MBP with with pyenv.

pyenv Installation

Following https://stackoverflow.com/questions/18671253/how-can-i-use-homebrew-to-install-both-python-2-and-3-on-mac , I tried:

$ pyenv install 3.6.5

Which erred with:

python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.5.tar.xz...
-> https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
Installing Python-3.6.5...
python-build: use readline from homebrew

BUILD FAILED (OS X 10.13.4 using python-build 20160602)

Inspect or clean up the working tree at /var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709
Results logged to /var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709.log

Last 10 log lines:
  File "/private/var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709/Python-3.6.5/Lib/ensurepip/__main__.py", line 5, in <module>
    sys.exit(ensurepip._main())
  File "/private/var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709/Python-3.6.5/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/private/var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709/Python-3.6.5/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/private/var/folders/d0/t8d3jjp161g2dyrd4k67ypc80000gn/T/python-build.20180426091253.34709/Python-3.6.5/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip
zipimport.ZipImportError: can't decompress data; zlib not available
make: *** [install] Error 1

What have I tried

  • Installing zlib: brew install zlib

My question

How can I install multiple Python environment with pyenv on MBP?

Xcode Solutions


Solution 1 - Xcode

On Mojave, after installing Xcode command line tools, had to run the following:

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

Solution 2 - Xcode

brew install zlib
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
pyenv install 3.7.2

Samsin's answer didn't work for me. I didn't have a Packages dir

Solution 3 - Xcode

On MacOS 11.1 (Big Sur) I eventually needed:

brew install bzip2
export LDFLAGS="-L $(xcrun --show-sdk-path)/usr/lib -L brew --prefix bzip2/lib"
export CFLAGS="-L $(xcrun --show-sdk-path)/usr/include -L brew --prefix bzip2/include"
pyenv install 3.9.0

I added the export commands in ./bash_profile so next time I can just use pyenv if I need to install more versions.

Keep an eye on : https://github.com/pyenv/pyenv/issues/1643 which tracks this, hopefully this will be fixed in pyenv.

Solution 4 - Xcode

I tried all the solution here with no success.

This is what worked for me on MacOs 11.2.1 (Big Sur):

export MACOSX_DEPLOYMENT_TARGET=11.0
brew install pyenv bzip2 zlib xz [email protected]

## 3.6.8
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.8 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

## 3.6.12
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.12 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

## 3.7.9
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install 3.7.9

The solution is taken from this gist.

Solution 5 - Xcode

The pyenv wiki has a page specific for common build problems. There are some possible solutions mentioned for the missing zlib error.

I have the same ZipImportError but with the following command installation is successful:

CFLAGS="-I$(xcrun --show-sdk-path)/usr/include" pyenv install 3.6.5

Solution 6 - Xcode

In order to get Python 3.8.6 with tkinter, I built on Vincent Gerris's answer and this blog post, and found success with

brew install bzip2
export LDFLAGS="-L $(xcrun --show-sdk-path)/usr/lib -L brew --prefix bzip2/lib"
export CFLAGS="-L $(xcrun --show-sdk-path)/usr/include -L brew --prefix bzip2/include"
export PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6' --enable-framework"
pyenv install 3.8.6

Solution 7 - Xcode

The following works for me on macOS Big Sur (11.6.1):

brew update
brew install bzip2
echo 'export PATH="/usr/local/opt/bzip2/bin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/zlib/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include"
pyenv install 3.8.6



Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam MatanView Question on Stackoverflow
Solution 1 - XcodesamsinView Answer on Stackoverflow
Solution 2 - XcodeJames ZaghiniView Answer on Stackoverflow
Solution 3 - XcodeVincent GerrisView Answer on Stackoverflow
Solution 4 - XcodeMartinView Answer on Stackoverflow
Solution 5 - XcodejayeffView Answer on Stackoverflow
Solution 6 - XcodemefryarView Answer on Stackoverflow
Solution 7 - XcodemathsyouthView Answer on Stackoverflow