55

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Adam Matan
  • 128,757
  • 147
  • 397
  • 562

9 Answers9

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 /
philipp
  • 4,133
  • 1
  • 36
  • 35
samsin
  • 1,194
  • 1
  • 9
  • 3
  • 2
    Thank you @samsin. This fixed the issue for me, after spending hours on it. OP should have marked it as the correct answer. – Andrew-Dufresne Oct 07 '18 at 13:55
  • 1
    Thanks, that is really useful answer, most of other comments reflect problem for Mac OS before Mojave – s_mart Oct 23 '18 at 20:59
  • 3
    Can I ask how on earth you figured out to do that @samsin? – Neil Feb 25 '19 at 17:34
  • 1
    For anyone else who has Mojave and may still be having issues, see this github issue - https://github.com/pyenv/pyenv/issues/1219 – dcordz Mar 13 '19 at 17:37
  • 3
    2020 El Capitan this answer no longer applies, https://stackoverflow.com/a/54955286/5847511 is the solution to use! – Lewis Smith Jul 07 '20 at 14:03
44
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

James Zaghini
  • 3,895
  • 4
  • 45
  • 61
17

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.

Vincent Gerris
  • 7,228
  • 1
  • 24
  • 22
  • 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"` – postrational Dec 02 '21 at 21:49
7

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 openssl@1.1

## 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.

Martin
  • 11,216
  • 23
  • 83
  • 140
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
jayeff
  • 1,689
  • 14
  • 14
  • This does not work on MacOS 11.1 (Big Sur) for me with Xcode 12.3. Some workarounds for that can be found here : https://github.com/pyenv/pyenv/issues/1643 . – Vincent Gerris Jan 03 '21 at 15:16
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
mefryar
  • 199
  • 4
  • 7
0

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

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 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](/help/how-to-answer). – Community Aug 10 '22 at 16:30
  • 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 Review](/review/late-answers/32428982) – Jan Aug 13 '22 at 23:00
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

-1

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
mathsyouth
  • 3,770
  • 1
  • 20
  • 18