58

When installing Python 3.6.13 on my MacBook Air, OS Big Sur, using the command pyenv install 3.6.13 I get this error:

Installing Python-3.6.13...
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

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

Inspect or clean up the working tree at /var/folders/7j/0qtpb8vs1_s34ynv0f6rrs840000gn/T/python-build.20210304114832.65954
Results logged to /var/folders/7j/0qtpb8vs1_s34ynv0f6rrs840000gn/T/python-build.20210304114832.65954.log

Last 10 log lines:
./Modules/posixmodule.c:8210:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        ret = sendfile(in, out, offset, &sbytes, &sf, flags);
              ^
./Modules/posixmodule.c:10432:5: warning: code will never be executed [-Wunreachable-code]
    Py_FatalError("abort() called from Python code didn't abort!");
    ^~~~~~~~~~~~~
1 warning and 1 error generated.
1 warning generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....

Any suggestions on how to fix this or another work around to install python 3.6.13 would be greatly appreciated. I currently have python 3.8, but need 3.6 in order to run:

conda install -c deezer-research spleeter 
Jacob Anderson
  • 1,747
  • 1
  • 12
  • 17
  • 3
    https://github.com/pyenv/pyenv/issues/1740 seems to be the same as your 3.6 issue – jordanm Mar 04 '21 at 20:14
  • Have you looked at `asdf`? https://asdf-vm.com/ It's personal preference, but I found it the easiest way to juggle the python versioning headaches. – Everett Mar 04 '21 at 20:14
  • 2
    @Everett `asdf` is awesome but it uses `pyenv` under the hood, so this thread is actual for both of the tools. – Andrey Semakin Sep 20 '21 at 06:18
  • Seems like this is an issue Apples new Clang release. Here is the fix. https://issueantenna.com/repo/pyenv/pyenv/issues/2284 – minhazur Aug 09 '22 at 13:41
  • 1
    For macOS 13.4 Ventura, I hit a similar issue. A complete uninstall and reinstall of Homebrew and its formulae worked for me: https://stackoverflow.com/a/76847742/2214933 – Steve Piercy Aug 06 '23 at 20:57

6 Answers6

95

Thanks to jordanm, The solution was to follow https://github.com/pyenv/pyenv/issues/1740#issuecomment-738749988

brew reinstall zlib bzip2

From people's comments (thanks to Lukasz Czerwinski and Alex Veksler) it seems many view this next step as optional (and even dangerous) and prefer to skip it; though, it's worth a try if nothing else is working:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

Whether you chose to skip the last step or not, continue by editing .zshrc or .bashrc using:

nano ~/.zshrc or nano ~/.bashrc

and add:

export PATH="$HOME/.pyenv/bin:$PATH"
export PATH="/usr/local/bin:$PATH"

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/bzip2/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/bzip2/include"

after editing and saving that file, run:

. ~/.zshrc or . ~/.bashrc

Then run the command below to install (changing 3.6.0 for the desired 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.6.0 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
kellyfj
  • 6,586
  • 12
  • 45
  • 66
Jacob Anderson
  • 1,747
  • 1
  • 12
  • 17
  • 10
    I confirm it worked for Python 3.6.13 for pyenv 1.2.26 on MacOS Big Sur (11.2.3). (Actually, I skipped `sudo rm -rf /Library/Developer/CommandLineTools` and `xcode-select --install` but followed all the rest) – Lukasz Czerwinski Apr 10 '21 at 16:30
  • 3
    This is not working for Mac M1 with Big Sur 11.2.2. – m-ketan May 05 '21 at 11:56
  • Worked for python 3.5.10 MacOs 11.2 – leonsas Jun 09 '21 at 01:51
  • It worked for me, python 3.8.0 - MacOs 11.2.1 – Rostan Jun 20 '21 at 19:27
  • This worked for me, 2019 Macbook Pro Intel MacOS 11.4, Python 3.6.9. – Jeff Wright Jun 28 '21 at 13:55
  • @m-ketan Python 3.9 is the first version of Python to have full support for Mac M1. – BoarGules Jun 28 '21 at 14:17
  • @BoarGules Yes i figured that and ended up using 3.9.5. – m-ketan Jun 28 '21 at 14:19
  • Worked for Python 3.6.8 on macOS 11.4. Don't forget to run the last command from OP. – northtree Sep 23 '21 at 06:53
  • It worked for me! Thanks! Big Sur 11.5.2, Python 3.7.2 – Thiago Martins Sep 28 '21 at 13:37
  • 2
    I have been on Intel Macs for about four years ago, and I generally get an error like this after an OS update. Almost every time: `sudo rm -rf /Library/Developer/CommandLineTools && xcode-select --install` fixes the issue. It is as the CommandLineTools don't get updated correctly. – Doug Nov 05 '21 at 15:26
  • I was able to install Python 3.6 on Big Sur 11.6.1. However when I try to run python using the command : /Users/myuserid/.pyenv/versions/3.6.0/bin/python3.6, I get a message - "xcode-select: Failed to locate 'python3.6', requesting installation of command line developer tools". I have installed xcode command line tool multiple times, but get same error. How can this be fixed? – steve Dec 10 '21 at 13:36
  • 2
    Running the command that starts with CFLAGS as shown, I got a `ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?` error. I believe this is because the `brew --prefix openssl` command was outputting OpenSSL@3 in 2022, and by changing the command to include `brew --prefix openssl@1.1` instead in the two places it occurs, I was able to get 3.6.9 to install. – RyanV May 24 '22 at 16:58
  • After I added `export PATH="/usr/local/bin:$PATH"`to mi `~/.zshrc` my brew started to point `/usr/local/` instead of `/opt/homebrew/` on `which brew`. Be aware of that. – jplattus Aug 24 '23 at 19:07
24

According to this: https://github.com/pyenv/pyenv/issues/1737#issuecomment-731672292 this is related to a commit issue and the following fixes it. It worked for me.

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.13 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

maede rayati
  • 756
  • 5
  • 10
8

The following steps finally worked for my MacBook Air M1 with Big Sur 11.6.

‼️ Please note that this instruction is for Rosetta 2 emulated x86 terminal.

  1. Install brew:

    arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  2. Install the recommended dependencies:

    arch -x86_64 brew install openssl readline sqlite3 xz zlib
    
  3. Install Pyenv with brew:

    arch -x86_64 brew install pyenv
    
  4. Configure your shell's environment (in my case zsh):

    echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zshrc
    
  5. Restart the terminal.

  6. Add environment variables (in my case the problem was with zlib and openssl):

    export LDFLAGS="-L/usr/local/opt/zlib/lib"
    export CPPFLAGS="-I/usr/local/opt/zlib/include"
    export LDFLAGS="-L/usr/local/opt/openssl@3/lib"
    export CPPFLAGS="-I/usr/local/opt/openssl@3/include"
    
  7. Install the required Python version with patch:

    arch -x86_64 pyenv install --patch 3.6.15 <<(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
    
  8. Switch shell to new Python version:

    pyenv shell 3.6.15
    
  9. Check the currently used Python version:

    pyenv which python3
    
spyker77
  • 143
  • 1
  • 8
3

The answer by Jacob Anderson was right, but it’s worth noting that there's no need to perform the dangerous step:

sudo rm -rf /Library/Developer/CommandLineTools 

Things worked for me (installed Python 3.6.0 on MacOS 11.4) without doing it. Thanks to Jacob!

Brandon H. Gomes
  • 808
  • 2
  • 9
  • 24
1

I was facing this on mu ubuntu machine, I uninstalled Brew and reinstalled pyenv from source. It worked for me.

Manoj More
  • 51
  • 5
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.13 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

it Worked on MacOs 11.2.2