Search code examples
python-3.xlinuxamazon-web-servicesamazon-ec2pyenv

Unable to install Python with pyenv on fresh AWS EC2 instance running Amazon Linux 2023: no module named: _bz2, _curses, _ctypes, readline, _ssl


I'm trying to to install Python with pyenv on a fresh AWS EC2 instance running Amazon Linux 2023. I've done these steps:

  1. sudo yum groupinstall "Development Tools"
No match for group package "system-rpm-config"
No match for group package "rcs"
No match for group package "pkgconfig"
  1. sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel (from pyenv docs)
Error: Unable to find a match: openssl11-devel
  1. sudo yum install build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev (from stackoverflow)
Error: Unable to find a match: build-essential zlib1g-dev libffi-dev libssl-dev libbz2-dev libreadline-dev libsqlite3-dev liblzma-dev
  1. sudo yum install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git (also from stackoverflow)
Error: Unable to find a match: build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
  1. following this guide:
    1. git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    2. echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
    3. echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
    4. echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
  2. bash --login (to start a new shell with the env variables)
  3. pyenv install 3.11.2

I got these errors (tldr, no module named: _bz2, _curses, _ctypes, readline, _ssl):

$ pyenv install 3.11.2
Downloading Python-3.11.2.tar.xz...
-> https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tar.xz
Installing Python-3.11.2...
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/bz2.py", line 17, in <module>
    from _bz2 import BZ2Compressor, BZ2Decompressor
ModuleNotFoundError: No module named '_bz2'
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/curses/__init__.py", line 13, in <module>
    from _curses import *
ModuleNotFoundError: No module named '_curses'
WARNING: The Python curses extension was not compiled. Missing the ncurses lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/ctypes/__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
WARNING: The Python ctypes extension was not compiled. Missing the libffi lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'readline'
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/ec2-user/.pyenv/versions/3.11.2/lib/python3.11/ssl.py", line 100, in <module>
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ModuleNotFoundError: No module named '_ssl'
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (Amazon Linux 2023 using python-build 2.3.24)

Inspect or clean up the working tree at /tmp/python-build.20230824022449.134442
Results logged to /tmp/python-build.20230824022449.134442.log

Last 10 log lines:
    LD_LIBRARY_PATH=/tmp/python-build.20230824022449.134442/Python-3.11.2 ./python -E -m ensurepip \
        $ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpeswj7w0u
Processing /tmp/tmpeswj7w0u/setuptools-65.5.0-py3-none-any.whl
Processing /tmp/tmpeswj7w0u/pip-22.3.1-py3-none-any.whl
Installing collected packages: setuptools, pip
  WARNING: The scripts pip3 and pip3.11 are installed in '/home/ec2-user/.pyenv/versions/3.11.2/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.3.1 setuptools-65.5.0

When this completes, ~/.pyenv/versions/ is empty. I'm not sure if that is related to the modules being not found. However, there seems to be a (perhaps incomplete) installation of python 3.11.2 in /tmp/python-build.20230824022449.134442 (or whatever the date and time is).

Looking inside, I do see the supposed missing modules in /tmp/python-build.20230824022449.134442/Python-3.11.2/Lib:

bz2.py
curses
ctypes
ssl.py

Only readline seems missing.

Furthermore, checking for libraries (as per the wiki):

$ openssl version
OpenSSL 3.0.8 7 Feb 2023 (Library: OpenSSL 3.0.8 7 Feb 2023)
$ which openssl
/usr/bin/openssl

I also tried passing the location of OpenSSL headers and libraries explicitly: CONFIGURE_OPTS="-with-openssl=/etc/pki/tls" pyenv install 3.11.2 (as per the wiki)


Solution

  • ChatGPT solved this one. What I did was uninstall everything that I previously installed with yum, then install again. I have no fking clue why this works, because the packages all seem to be the same:

    sudo yum remove gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl11-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel
    sudo yum clean all
    sudo yum install gcc make patch zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel tk-devel libffi-devel xz-devel bzip2-devel ncurses-devel libffi-devel readline-devel openssl-devel zlib-devel xz-devel