Can't install Poetry in Ubuntu 20.04 Docker image due to setuptools error

Question:

I’m trying to build the following Dockerfile:

FROM ubuntu:20.04

RUN apt-get update && 
    apt-get install -y curl software-properties-common && 
    add-apt-repository ppa:deadsnakes/ppa && 
    apt install -y python3.11 python3.11-distutils && 
    curl -sS https://bootstrap.pypa.io/get-pip.py | python3.11 && 
    python3.11 -m pip install poetry==1.2.2

However, it fails to install poetry with the following error message:

Collecting msgpack>=0.5.2
  Downloading msgpack-1.0.4.tar.gz (128 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 128.1/128.1 kB 15.2 MB/s eta 0:00:00
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
ERROR: Exception:
Traceback (most recent call last):
...
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1142, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'setuptools'

If I replace the install command for Poetry at the end and replace it with python3.11 -m pip list then the output clearly shows setuptools being installed:

Package             Version
------------------- -----------
...
setuptools          67.2.0
...

I can also import setuptools without problems when I open a Python interpreter. What is going on?

Note that I’m unable to use a newer Ubuntu version or pythonX.Y image due to other requirements.

Answers:

Don’t install poetry into the global Python environment (nor into your project venv), or it’ll influence package compatibility within your project/system dependencies.

See poetry docs on how to give poetry its own venv when doing a "manual install". Otherwise, use the official poetry install script which does that for you.

Answered By: Kache