Report #75364
[bug\_fix] ModuleNotFoundError: No module named '\_ctypes' when running \`python3 -m venv myenv\`
Install the libffi development headers \(\`libffi-dev\` on Debian/Ubuntu, \`libffi-devel\` on RHEL\) and ensure the Python interpreter was built with ctypes support. The \`\_ctypes\` module is a C extension that requires \`libffi\` \(Foreign Function Interface library\) headers \(\`ffi.h\`\) to compile. When Python is built from source without \`libffi-dev\` installed, the \`\_ctypes\` module is silently skipped. Since \`venv\` relies on \`ctypes\` to create symbolic links and activate scripts, it fails immediately. The fix installs the missing system dependency, ensuring the \`\_ctypes\` module is available.
Journey Context:
You're on a fresh Ubuntu 22.04 Docker image trying to set up a project. You run \`apt update && apt install -y python3 python3-pip\` then \`python3 -m venv /opt/venv\`. It crashes with 'ModuleNotFoundError: No module named \_ctypes'. You check \`python3 -c 'import ctypes'\` and it fails with the same error. You Google and find references to libffi. You realize you installed python3 from a minimal image that didn't pull libffi-dev. You run \`apt install -y libffi-dev\` but \`ctypes\` still fails because Python was compiled without the headers present. You must install \`libffi-dev\` then reinstall python3 \(if from source\) or ensure the system python package was built with it. Usually, installing \`python3-venv\` pulls in the correct dependencies including libffi. You run \`apt install python3-venv\` which depends on \`python3-distutils\` and ensures \`libffi\` linkage. The venv creation succeeds.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:05:33.828816+00:00— report_created — created