pyenv zlib error on MacOS:

25,632

Solution 1

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

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

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

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

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
Share:
25,632
Adam Matan
Author by

Adam Matan

Team leader, developer, and public speaker. I build end-to-end apps using modern cloud infrastructure, especially serverless tools. My current position is R&amp;D Manager at Corvid by Wix.com, a serverless platform for rapid web app generation. My CV and contact details are available on my Github README.

Updated on July 16, 2022

Comments

  • Adam Matan
    Adam Matan almost 2 years

    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 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?

  • Andrew-Dufresne
    Andrew-Dufresne over 5 years
    Thank you @samsin. This fixed the issue for me, after spending hours on it. OP should have marked it as the correct answer.
  • s_mart
    s_mart over 5 years
    Thanks, that is really useful answer, most of other comments reflect problem for Mac OS before Mojave
  • Neil
    Neil about 5 years
    Can I ask how on earth you figured out to do that @samsin?
  • dcordz
    dcordz about 5 years
    For anyone else who has Mojave and may still be having issues, see this github issue - github.com/pyenv/pyenv/issues/1219
  • Lewis Smith
    Lewis Smith almost 4 years
    2020 El Capitan this answer no longer applies, stackoverflow.com/a/54955286/5847511 is the solution to use!
  • iszzypop
    iszzypop over 3 years
    This is what I just had to do to get "pyenv install" working on Big Sur
  • Vincent Gerris
    Vincent Gerris over 3 years
    This does not work on MacOS 11.1 (Big Sur) for me with Xcode 12.3. Some workarounds for that can be found here : github.com/pyenv/pyenv/issues/1643 .
  • hoda azadi
    hoda azadi over 2 years
    I think you need to use the output of the brew --prefix bzip2 command: 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"