5
5

More than 3 years have passed since last update.

[Ubuntu]pyenvでPythonをインストールしようとするとzlibなどがエラーを起こすのを解決した

Posted at

pyenvをど使って新し目のPython(ex. 3.9.6)をインストールするとビルドに失敗することがあります。

  • zlib関連のエラーが表示される例
  • "Successfully installed pip-21.1.3 setuptools-56.0.0" と一見成功したように見えて BUILD FAILEDと表示される例
    • /tmp/python-build.*.log を確認するとエラーの痕跡があります
    • curses.h, ffi.h が見つからずにコンパイルが中断した形跡があります
    • 次のモジュールのビルドが失敗しています
      • _ctypes
      • _curses
/tmp/python-build.*.log
building '_curses' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -I/home/linuxbrew/.linuxbrew/opt/readline/include -I/home/fx9/.anyenv/envs/pyenv/versions/3.9.6/include -I/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/include -I/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/include -fPIC -DHAVE_NCURSESW=1 -I/usr/include/ncursesw -I./Include -I. -I/home/linuxbrew/.linuxbrew/opt/readline/include -I/home/fx9/.anyenv/envs/pyenv/versions/3.9.6/include -I/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/include -I/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/include -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/tmp/python-build.20210804002303.2178/Python-3.9.6/Include -I/tmp/python-build.20210804002303.2178/Python-3.9.6 -c /tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_cursesmodule.c -o build/temp.linux-x86_64-3.9/tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_cursesmodule.o
In file included from /tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_cursesmodule.c:113:
./Include/py_curses.h:36:10: fatal error: curses.h: No such file or directory
   36 | #include <curses.h>
      |          ^~~~~~~~~~
compilation terminated.
building '_ctypes' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -I/home/linuxbrew/.linuxbrew/opt/readline/include -I/home/fx9/.anyenv/envs/pyenv/versions/3.9.6/include -I/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/include -I/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/include -fPIC -I./Include -I. -I/home/linuxbrew/.linuxbrew/opt/readline/include -I/home/fx9/.anyenv/envs/pyenv/versions/3.9.6/include -I/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/include -I/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/include -I/usr/include/x86_64-linux-gnu -I/usr/local/include -I/tmp/python-build.20210804002303.2178/Python-3.9.6/Include -I/tmp/python-build.20210804002303.2178/Python-3.9.6 -c /tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_ctypes/_ctypes.c -o build/temp.linux-x86_64-3.9/tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_ctypes/_ctypes.o -DPy_BUILD_CORE_MODULE
/tmp/python-build.20210804002303.2178/Python-3.9.6/Modules/_ctypes/_ctypes.c:107:10: fatal error: ffi.h: No such file or directory
  107 | #include <ffi.h>
      |          ^~~~~~~
compilation terminated.

Python build finished successfully!
The necessary bits to build these optional modules were not found:
_bz2                  _curses_panel         _dbm               
_gdbm                 _hashlib              _lzma              
_ssl                  _tkinter              _uuid              
To find the necessary bits, look in setup.py in detect_modules() for the module's name.


The following modules found by detect_modules() in setup.py, have been
built by the Makefile instead, as configured by the Setup files:
_abc                  atexit                pwd                
time                                                           


Failed to build these modules:
_ctypes               _curses 

しばらくこのバージョンを避けてPythonをインストールしたのですが、apt, Linuxbrew(Homebrew on Linux)の2つを用いてパッケージをインストールしてこの問題を解決できたので残します。

環境

  • Windows 10 Pro 21H1
  • WSL2(Ubuntu 20.04 LTS)
  • anyenvを用いてpyenvをインストール

aptによるパッケージのインストール

libffiなどは apt コマンドを用いてインストールします。

sudo apt install libffi-dev libncurses5-dev zlib1g zlib1g-dev bzip2
sudo apt install checkinstall libreadline-gplv2-dev libncursesw5-dev libsqlite3-dev tk-dev libgdbm-dev libbz2-dev libssl-dev libsqlite3-dev

Linuxbrewによるパッケージのインストール

zlib, sqliteのインストールにはLinuxbrewが必要です。
また、パッケージインストール時にファイルの配置場所が表示されるのでこれを控えて次のように環境変数を設定する必要があります。
パッケージのバージョンによってパスが変わります。

brew install zlib
brew install sqlite
export LDFLAGS="${LDFLAGS} -L/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/lib"
export CPPFLAGS="${CPPFLAGS} -I/home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/include"
export LDFLAGS="${LDFLAGS} -L/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/lib"
export CPPFLAGS="${CPPFLAGS} -I/home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/include"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /home/linuxbrew/.linuxbrew/Cellar/zlib/1.2.11/lib/pkgconfig"
export PKG_CONFIG_PATH="${PKG_CONFIG_PATH} /home/linuxbrew/.linuxbrew/Cellar/sqlite/3.36.0/lib/pkgconfig"

Pythonのインストール

これでzlib関連のエラーが出るPythonがインストールできるようになります。

$ pyenv install 3.9.6
Downloading Python-3.9.6.tar.xz...
-> https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tar.xz
Installing Python-3.9.6...
python-build: use readline from homebrew
Installed Python-3.9.6 to /home/yoshida.m/.anyenv/envs/pyenv/versions/3.9.6

$ pyenv versions
* system (set by /home/yoshida.m/.anyenv/envs/pyenv/version)
3.9.6

$ pyenv global 3.9.6

$ python --version
Python 3.9.6

参考

pyenv installでコケる(zlib) - Qiita
https://qiita.com/k3ntar0/items/36820743948f3d0c2536

pyenv install 3.x BUILD FAILED (Ubuntu 20.04 using python-build 20180424) using 'python-build: use readline from homebrew' - Stack Overflow
https://stackoverflow.com/questions/67807596/pyenv-install-3-x-build-failed-ubuntu-20-04-using-python-build-20180424-using

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5