PyEnv BUILD FAILED installing Python on MacOS
Asked Answered
C

23

84

While trying to install Python 3.6.6 (for Airflow) using PyEnv on MacOS, I am encountering build failure with following stack-trace

File
"/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xmlrpc/client.py",
line 138, in <module>
    from xml.parsers import expat   File "/private/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347/Python-3.6.6/Lib/xml/parsers/expat.py",
line 4, in <module>
    from pyexpat import * ModuleNotFoundError: No module named 'pyexpat' make: *** [install] Error 1

BUILD FAILED (OS X 10.13.6 using python-build 20180424)

Inspect or clean up the working tree at
/var/folders/6y/kf699bqj2sgcgjshb20fr5zh0000gn/T/python-build.20180721180716.86347
Results logged to /var/folders/6y/kf699bqj2sgcgjsh

I've followed the steps mentioned in the docs for setting-up PyEnv on Mac via Homebrew.


Environment / Framework versions

  • MacOS High Sierra 10.13.6
  • Homebrew 1.7.1
  • PyEnv 1.2.6
  • Existing Python distributions
    • Python 2.7.15 [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
    • Python 3.7.0 [Clang 9.0.0 (clang-900.0.39.2)] on darwin
Cauline answered 27/7, 2018 at 5:55 Comment(1)
Same issue, just had to open a new terminal window. Had some bad environment variables from when I ran pyenv uninstall in the same session.Allheal
S
44

I had this problem with Mojave and Python 3.7.3.

This worked for me:

SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk MACOSX_DEPLOYMENT_TARGET=10.14 pyenv install 3.7.3
Scissile answered 23/6, 2019 at 1:15 Comment(11)
Works for Python 3.6.6 too. Thank you!Ber
Worked great on Mojave 10.14.5. The other answers did not work.Sherrellsherrer
Worked great on macOS Mojave 10.14.6Institutionalism
Unfortunately, not working for me on Mojave with Python 3.7.3 or 3.5.2Toehold
Worked for me on macOS Catalina (10.15.3) with Python 3.7.3Rifling
Anybody cares to explain what this does to newbies :)?Thiazole
Is there some way to setup the environment so we dont have to include SDKROOT and MACOSX_DEPLOYMENT_TARGET every time?Everick
I get this: By default, distutils will build C++ extension modules with "clang++". With the error checking for clang++... no. But clang/clang++ are 100% already installed globally. Running the code in this answer above prompts me to install clang, which seemed promising. Maybe my pyenv couldn't find clang++? But DOH! Still failed pyenv installImplicate
@MarcelWilson I don't know how to completely avoid the need for the variables, but you can add export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk MACOSX_DEPLOYMENT_TARGET=12.3 your .bashrc or .zshrc so you don't have explicitly prefix every invocation of pyenv. This is only a workaround, mind you. It might fail when you upgrade the Xcode command-line tools.Exterminatory
Incidentally, my version number is different, since I'm on macOS Monterey. I added the following to my .zshrc, which attempts to detect the latest version number. It might be more robust or less than just hard-coding it (no warranty express or implied): export SDKROOT=$(/bin/ls -d /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX*.*.sdk | /usr/bin/tail -1) and export MACOSX_DEPLOYMENT_TARGET=$(/bin/echo $SDKROOT | /usr/bin/sed -E 's/^.*[^[:digit:]]([[:digit:]]+\.[[:digit:]]+)\.sdk$/\1/')Exterminatory
I found I was able to get away with adding just this to my .bash_profile : export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdkEverick
H
36

I'm posting my solution for those who are trying to resolve this issue in 2021. I am using macOS Big Sur Version 11.5.1. For me, this worked:

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

    $ pyenv install 3.9.5

enter image description here

Hakon answered 9/8, 2021 at 7:14 Comment(6)
I can confirm this worked for me in MacOS Big Sur Ver 11.5.2. I uninstalled pyenv prior to performing the above steps to be safe then reinstalled after step 2 above with: curl https://pyenv.run | bashRoofing
This worked for me, thanks.Bloodstone
This works on Big Sur 11.6.4 as wellAbdicate
This worked for me, Monterey 12.3.1 with pyenv 3.9.6. I did not uninstall pyenv first.Coypu
In addition to these steps, I also had to run brew install opensslAntonyantonym
Worked for me on Monterey 12.5.1, for python 3.10.6Aspersion
W
32

The only way I could get it working is through the below prefixes before installing

CFLAGS="-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include 
-I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib" \
PYTHON_CONFIGURE_OPTS=--enable-unicode=ucs2 \
pyenv install -v 3.7.1

Here's a breakdown of what each part of the command does:

  1. CFLAGS: This is an environment variable that specifies additional flags to be used by the C compiler when compiling Python.

-I$(brew --prefix readline)/include -I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include: These flags tell the compiler where to find the header files for readline, openssl, and the SDK (Software Development Kit) provided by Xcode.

The -I option adds directories to the list of directories to be searched for header files.

  1. LDFLAGS: This is another environment variable that specifies additional flags for the linker.

  2. -L$(brew --prefix readline)/lib -L$(brew --prefix openssl)/lib: These flags tell the linker where to find the libraries for readline and openssl. The -L option adds directories to the list of directories to be searched for libraries.

  3. PYTHON_CONFIGURE_OPTS: This environment variable is used to pass additional options to the configure script when building Python.

--enable-unicode=ucs2: This option configures Python to use UCS-2 (2-byte Unicode) as the internal representation for Unicode characters. This can be important for compatibility with certain Python packages or systems.

pyenv install -v 3.7.1: This is the actual command to install Python 3.7.1 using pyenv.

pyenv is a tool that allows you to easily install and manage multiple versions of Python.

-v is an option for verbose logging, which provides more detailed output during the installation process.

Washbowl answered 11/1, 2019 at 7:59 Comment(3)
If you're copy-pasting the snippet above, make sure to remove the new line at the end of the first line which can break the compilationIsm
what does this actually do?Glasper
@Glasper Updated the answer with breakdown of each commandWashbowl
C
14

I was able to get away with above error by referring to Common build problems

  • brew install readline xz
  • xcode-select --install
  • brew install openssl

After this, I installed and initialized pyenv

  • brew install pyenv
  • pyenv init (inside your project directory)

Finally install and activate required python version

  • pyenv install 3.6.6
  • pyenv local 3.6.6 (inside your project directory)

Here's the link to original thread #1188 on GitHub by @Harry Moreno

Cauline answered 27/7, 2018 at 5:59 Comment(5)
in my case it doesn't work, the error remains the sameCesya
This works if you have the LATEST version of XCode AND if you have "agreed" to the new license!Julienne
Also in my case did not work, but Srinivas Gowda solution did. Anyway, the link to the thread is useful.Collagen
This worked to help me get the AWS ebs cli installed if you get this on Mac OS catalinaMannerism
What am I even doing anymore.. I need to install readline so that I can install pyenv so that i can install a python version so I can install a node version so that i can run a test.Konstantine
L
12

The accepted answer didn't work for me (Mojave) but this did:

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

Credit to http://www.blog.howechen.com/macos-mojave-pyenv-install-multi-version-build-failed-solution/

Lavender answered 24/1, 2019 at 10:26 Comment(3)
I suppose that might work, but is that a good idea? Doesn't this prevent you from using different SDK versions at different times? Presumably Apple changed this for a reason...Coble
Works. Hopefully it doesn't break other things.Oilcloth
This is a known requirement for Mojave as described here github.com/pyenv/pyenv/wiki/Common-build-problems which I routinely completed during initial installation but still got the same errorFinish
M
9

install Xcode:

xcode-select --install

install Brew:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

OR

update brew:

brew upgrade

install packages:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force
brew install openssl

Install Pyenv

curl https://pyenv.run | bash

Insert the following lines to .bashrc/.zshrc config file:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

restart the terminal and execute:

pyenv install 3.6.5

If still doesn't work: (just change the python version to the desired one - in my case i've installed python 3.6.5 to pyenv)

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.5 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
Misapply answered 11/4, 2021 at 12:58 Comment(2)
The --patch is the only thing that worked for me. Thanks.Flinty
Thanks, this part did the trick for me: brew install libxml2 brew install libxslt brew link libxml2 --force brew link libxslt --forceEvans
R
7

UPDATE FOR 2023

Python 3.6.X don't seem to support Apple Silicon. According to release note, python support Apple Silicon from 3.9.1.

SOLVED with the help of arch command:

arch -x86_64 pyenv install 3.8.12

Related to this issue: https://github.com/pyenv/pyenv/issues/1876

Reconstruction answered 24/6, 2023 at 19:18 Comment(1)
You ROCK Carlos, thank you!! Confirm works for installing 3.10.11 too.Barty
M
3

short answer:

$ brew upgrade

now you can try to install python through pyenv

$ pyenv install 3.7.4

then, you have to set the python path

pyenv global 3.7.4

now, close and open a new terminal and write the command

echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

That's all. For me it was fine.

Melicent answered 24/9, 2019 at 11:56 Comment(1)
I tried this and it didn't work. The brew upgrade part worked fine (took a while) but then pyenv install 3.7.4 gave BUILD FAILED (OS X 11.1 using python-build 20180424) Inspect or clean up the working tree at /var/folders/m8...Cuneate
L
2

In my case this was caused by migration of data from old to new Mac. Both Xcode Command Line Tools and Homebrew migrated, but something broke. Reinstalling them from scratch worked for me.

Reinstall CLI tools:

$ sudo rm -rf /Library/Developer/CommandLineTools

$ xcode-select --install

Reinstall and update Homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

brew update

Istall pyenv + packages and python 3:

brew install pyenv
brew install openssl readline sqlite3 xz zlib
pyenv install 3.7.5
Leandraleandre answered 5/11, 2019 at 13:39 Comment(1)
I was in the same situation of migrating and this was the only working solution.Slather
C
2

The cause is a change in Apple Clang 13.0.0+ that made it incompatible with existing Python versions. Upgrade to the aforementioned newest Python versions to fix the issue.

Please try the following suggestion https://github.com/pyenv/pyenv/issues/2112#issuecomment-1096478446

Caravaggio answered 26/10, 2022 at 3:59 Comment(0)
A
1

same issue solved following a comment on pyenv issue tracking system suggesting to apply this patch: https://github.com/python/cpython/commit/8ea6353.patch

# original comment
pyenv install --patch 3.8.3 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

it did work for 3.6.9 and 3.7.4 versions as well e.g.

pyenv install --patch 3.6.9 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch)

source: https://github.com/pyenv/pyenv/issues/1738#issuecomment-880517959

Ameeameer answered 3/11, 2021 at 15:52 Comment(0)
S
1

I tried xcode install, brew upgrade etc, nothing worked. In the end , this patch worked (for MacOS Monterey)

pyenv install --patch 3.8.5 <<(curl -sSL https://raw.githubusercontent.com/Homebrew/formula-patches/113aa84/python/3.8.3.patch\?full_index\=1)
Sleepless answered 13/5, 2022 at 18:32 Comment(1)
Welcome to stackoverflow! In oder to make your answer useful for others please format code as code with backtick-markups. Btw the link in your script seems to be broken.Pogey
T
1

I just had outdated prerequisites on MacOS 12.6

brew install openssl readline sqlite3 xz zlib tcl-tk

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

Taunyataupe answered 26/9, 2022 at 12:19 Comment(0)
A
1

Following these 3 steps also solved my problem of installing pyenv 3.10.6 for macOS Monterey 12.6.5

  1. Uninstall the old tools

    $ sudo rm -rf /Library/Developer/CommandLineTools

  2. Reinstall Xcode Command line tools

    $ xcode-select --install

  3. Install python using pyenv

$ pyenv install 3.10.6
Archon answered 8/6, 2023 at 3:19 Comment(0)
G
1

I had to uninstall and reinstall Home Brew before it returned to work. It concerned the change from Mac Intel to Mac M1 (Silicon). See the article below from Josh Alletto to find out why.

HomeBrew on Mac M1/M2

Geanticlinal answered 10/7, 2023 at 2:50 Comment(0)
C
0

After upgrading to Big Sur you need to reinstall X Code tools. Even if you had done it previously

xcode-select --install

This worked for me

If that doesn't work, force it to reset

sudo xcode-select --reset

More details can be found here: https://dev.to/o9uzdev/macos-xcrun-error-invalid-active-developer-path-missing-xcrun-411a

Coronet answered 29/3, 2021 at 13:17 Comment(0)
C
0

Try any of these:

❯ xcode-select --install
❯ xcode-select --reset
❯ export SDKROOT="macosx"

Maybe you don't have command line tools installed.
Or maybe you messed up the $DEVELOPER_DIR environment
variable path.
Or maybe your compiling with command line tools meant
for iPhone development or WatchOS !?!
❯ brew update && brew upgrade pyenv

Maybe your command versions aren't up to date ?
Double check you've properly followed the installation
steps for your version, OS and shell.
❯ brew install make cmake;
❯ export PATH="/usr/local/opt/make/libexec/gnubin:$PATH"

Maybe you don't have `make` and `cmake` build/compilation commands.
Or maybe they can't be found.
❯ brew install openssl readline tcl-tk sqlite3 xz zlib bzip2;
❯ export PATH="/usr/local/opt/bzip2/bin:$PATH";
export CPPFLAGS="\
    -I$(xcrun --show-sdk-path)/usr/include \
    -I$(brew --prefix openssl)/include \
    -I$(brew --prefix readline)/include \
    -I$(brew --prefix zlib)/include \
    -I$(brew --prefix bzip2)/include";
export LDFLAGS="\
    -L$(xcrun --show-sdk-path)/usr/lib \
    -L$(brew --prefix openssl)/lib \
    -L$(brew --prefix readline)/lib \
    -L$(brew --prefix zlib)/lib \
    -L$(brew --prefix bzip2)/lib";

Maybe you're missing some suggested `pyenv` build,
tools or what not. Or maybe they can't be found during
compilation.
https://github.com/pyenv/pyenv/wiki#suggested-build-environment
❯ unset LIBRARY

Maybe you've set/defined a LIBRARY environment variable
(either in your shell initialization/startup files,
e.g. .bashrc, .zshrc, etc; or shell session).
This can be bad because it could be used in the build/compilation
process and may crash it.
❯ pyenv install --verbose --patch 3.8.11 \
    < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Maybe you haven't heard of the magical patch yet ?
which fixes issue with build process not taking into
account certain MacOS versions.
Also using the `--verbose` option, might give you
better hints as to what's going wrong.

❯ pyenv global 3.8.11 && eval "$(pyenv init -)"
OR  
❯ pyenv global 3.8.11 && exec $SHELL --login

Also, don't forget to set your preferred global
python interpreter version and reinitialize `pyenv`
or restart your [login] shell altogether.

It may still not work, in that case just keep looking and post your answer when you've found it. Best Wishes.

Here are some of the resources I've consulted:

Cloistral answered 6/7, 2021 at 6:16 Comment(0)
K
0
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)

change the version you want

Kapellmeister answered 19/7, 2021 at 14:28 Comment(0)
C
0

I had the same error and this command worked for me, note that you need to change python version to your desired one to install, after --patch

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.8.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
Cabriole answered 19/11, 2021 at 11:12 Comment(0)
P
0

On Ventura 13.3.1 with XCode 14.1 I was able to get it resolved by:

  1. Open XCode -> Settings -> Locations
  2. Select the latest Command Line Tools
  3. Open new terminal and pyenv install 3.10.6
Pissarro answered 1/6, 2023 at 17:1 Comment(0)
C
-1

for the recent googler, if you are using Big Sur, you should run the command

xcode-select --install
Cabriole answered 19/11, 2021 at 10:40 Comment(1)
This has already been mentioned in many other answers. When answering older questions that already have answers, please make sure you provide either a novel solution or a significantly better explanation than existing answers.Precursor
S
-1

This steps helped me solving BUILD FAILED (OS X 13.0.1 using python-build 20180424)

  • First step run xcode-select --install
  • Second step brew install openssl@3
  • Finally brew install llvm and follow instructions for setting appropriate environment variables (in the output of the brew install command).
Strang answered 26/11, 2022 at 19:4 Comment(0)
G
-3

This worked for me:

  1. remove CommandLineTools folder under /Library/Developer
  2. run in terminal: xcode-select --install
  3. sudo mv /usr/local/include /usr/local/include_old
Guck answered 7/11, 2018 at 11:52 Comment(3)
Might work, but that's super destructive, and probably cause you even more problems in the end...Broody
This is the only option that worked for me, but be cautious - after you perform this step, you need to recreate include dir and then reinstall everything from start with brew. Thanks @Tomer, this saved me from complete OS reinstall!Electronegative
further, I don't have a Packages folder under CommandLineToolsEmarie

© 2022 - 2024 — McMap. All rights reserved.