pyenv zlib error on MacOS:
Asked Answered
H

11

60

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?

Hayott answered 26/4, 2018 at 6:22 Comment(0)
W
106

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 /
Winder answered 2/10, 2018 at 0:36 Comment(5)
Thank you @samsin. This fixed the issue for me, after spending hours on it. OP should have marked it as the correct answer.Clone
Thanks, that is really useful answer, most of other comments reflect problem for Mac OS before MojaveDonothingism
Can I ask how on earth you figured out to do that @samsin?Elizebethelizondo
For anyone else who has Mojave and may still be having issues, see this github issue - github.com/pyenv/pyenv/issues/1219Steeple
2020 El Capitan this answer no longer applies, https://mcmap.net/q/146339/-pyenv-zlib-error-on-macos is the solution to use!Mantle
R
47
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

Radiant answered 2/3, 2019 at 4:37 Comment(1)
This is what I just had to do to get "pyenv install" working on Big SurPaleoasiatic
G
18

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.

Gynaecocracy answered 4/1, 2021 at 2:1 Comment(1)
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"Maxima
I
8

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.

Indicate answered 25/2, 2021 at 7:42 Comment(0)
N
3

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
Neptunium answered 4/6, 2019 at 9:51 Comment(1)
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 .Gynaecocracy
A
0

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
Alum answered 6/4, 2021 at 0:27 Comment(0)
N
0

The solution by @mathsyouth worked for me but I had to add arch -x86_64 before the commands.

Nason answered 7/8, 2022 at 10:57 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Tupungato
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewMiguel
R
0

Make sure you've opened XCode recently and granted permissions during that first-open process. This solved it for me. Got the idea from:

https://github.com/pyenv/pyenv/issues/2112#issuecomment-1265874025

Richmound answered 23/3, 2023 at 17:34 Comment(0)
F
0

I had no luck with all of these attempts. But found a workaround solution.

I still see

pyenv install 3.8.8

python-build: use [email protected] from homebrew
python-build: use readline from homebrew
Downloading Python-3.8.8.tar.xz...
-> https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tar.xz
Installing Python-3.8.8...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.7.1 using python-build 1.2.23-19-g9ee109b6)

Inspect or clean up the working tree at /var/folders/mx/kwgrm0vx6vn1r5ptjpr75tjw0000gn/T/python-build.20231201205607.30499
Results logged to /var/folders/mx/kwgrm0vx6vn1r5ptjpr75tjw0000gn/T/python-build.20231201205607.30499.log

Last 10 log lines:
checking whether we are using the GNU C compiler... yes
checking whether clang accepts -g... yes
checking for clang option to accept ISO C89... none needed
checking how to run the C preprocessor... clang -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=<compiler>... no
checking for the platform triplet based on compiler characteristics... darwin
configure: error: internal configure error for the platform triplet, please file a bug report
make: *** No targets specified and no makefile found.  Stop.

Workaround - Manually download pkg file from source

Download manually a release from https://www.python.org/downloads/macos/ I downloaded and ran python-3.8.9-macosx10.9.pkg

~/.zshrc

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

# setting up for pipenv 3.8
export PATH="$(pyenv root)/shims:$PATH"
export CXX=clang++
export LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib -L$(brew --prefix sqlite3)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix readline)/include -I$(brew --prefix zlib)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix sqlite3)/include"
export PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig:$(brew --prefix readline)/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig:$(brew --prefix bzip2)/lib/pkgconfig:$(brew --prefix sqlite3)/lib/pkgconfig"

# Setting PATH for Python 3.8
export PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:$PATH"
alias python="python3"
export PATH="/Users/yourname/Library/Python/3.8/bin:$PATH"

➜  which python
python: aliased to python3

➜  which python3
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3

➜  pipenv shell --python 3.8
zsh: command not found: pipenv
➜  python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"

OpenSSL 1.1.1k  25 Mar 2021
➜  python3 -m pip install --user pipenv

➜  /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 -m site --user-base

/Users/yourname/Library/Python/3.8

➜  pipenv shell --python 3.8
Creating a virtualenv for this project...

(ai_sandbox) ➜  ai_sandbox jupyter-lab

at this point we are in business, I can proceed with python running allowing me to do what I need to do

Fulfillment answered 2/12, 2023 at 2:16 Comment(0)
A
0

This is for MacOS Sonoma 14.2.1 for those who have been trying to install python on M3 macbook and having issues OR got Homebrew accidently installed in /usr/local or any other path than the default '/opt/homebrew'

Properly uninstalling Homebrew

go to -> https://github.com/Homebrew/install

This page shows two terminal commands to uninstall homebrew One interactive another non interactive.

If your Homebrew is installed at base prefix location /opt/homebrew first command should do it for you.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Otherwise run below command

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

Due to some reason I had to execute both 1 & 2 uninstall commands to properly uninstall it as mine got installed in /usr/local

Next: Download Package Installer for macOS

go to -> https://github.com/Homebrew/brew/releases/tag/4.2.4 Download the pkg file

Run it, if it warns you with a popup that Command line tools are missing. Run: xcode-select —install

Open a terminal and run

xcode-select --install

xcode-select: note: install requested for command line developer tools

If you see the message above look for a popup try moving or minimizing your large windows, it could be overlooked - Will request your permission to install xcode-tools, grant the same.

It will download lot of files in the background to install the same for 5-7 minutes on a fast internet connection, otherwise wait longer.

Once it is installed the popup will say

**‘The software is installed’**

Now re-run the Homebrew pkg file

It will show you below message

This package will install to:
    ⁃   /opt/homebrew on Apple Silicon
    ⁃   /usr/local/bin/brew and /usr/local/Homebrew on Intel

So depending on your processor, homebrew shall be installed soon.

Once the installer finishes go back to terminal and run below two commands -

brew help

Homebrew should be setup properly at this point.

Try installing a latest version of python to ensure all is well! Below few versions installed for me. More about this on a related pages here -> https://github.com/pyenv/pyenv/issues

nr@MacBook-Pro ~ % pyenv install 3.11.0
nr@MacBook-Pro ~ % pyenv install 3.10.13

...

Installed Python-3.11.0 to /Users/nr/.pyenv/versions/3.11.0

Quoting fernando for his answer / link for homebrew formula patch on python 3.8.6 - https://mcmap.net/q/146492/-unexpected-output-of-39-arch-39-on-osx-using-mac-m1-installing-elastic-beans

nr@MacBook-Pro ~ % pyenv install --patch 3.8.6 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)

...

Installed Python-3.8.6 to /Users/nr/.pyenv/versions/3.8.6

nr@MacBook-Pro ~ % pyenv versions
* system (set by /Users/nr/.pyenv/version)
  3.8.6
  3.11.0
  3.10.13

Note: the pyenv could still fail to install some versions. I listed the ones which succeeded.

Good Luck!

Antispasmodic answered 18/1, 2024 at 1:1 Comment(0)
G
-2

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
Giorgio answered 10/11, 2021 at 4:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.