I’m unable to create a Python virtual environment via venv
in which I can install dependencies via pip
. These issues occur on a clean install of Ubuntu 24.04 LTS.
I specifically want to use Python’s built-in venv (not any other variants), so I run the command: python3.12 -m venv myvenv
. This partially creates the venv directory but fails midway (doesn’t create bin/activate) with the message:
Error: Command '['/sample/path/myvenv/bin/python3.12', '-m', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
As per this answer, I’ve tried installing python3.12-venv via sudo apt install python3.12-venv
, which successfully installs but doesn’t change the error.
Venv creation does succeed if I run it with the –without-pip flag, and I can install pip independently via sudo apt install python3-pip
, but actually using pip in the newly-created and activated venv gives me an “externally-managed-environment” error and instructs me to use the pip in myenv/bin/pip
, which doesn’t exist because I specified the –without-pip flag.
Running the failing command directly results in a stack trace that complains “No module named ‘zlib'” and “can’t decompress data; zlib not available.” I followed the directions in several other answers and ensured that packages were installed successfully, via sudo apt-get install zlib1g-dev
(as well as build-essential and the non-dev version of zlib), but the command still fails with the same “zlib not available” error. My zlib is version 1:1.3.dfsg-3.1ubuntu2.
For what it’s worth, I did also try building Python from source, and that gave me the same error. I followed the official developer’s guide for installing dependencies, and this also didn’t resolve the zlib error.
Will fixing the zlib issue allow me to use venv, and if so, what am I missing? I’ve been stuck on this for hours — any help is appreciated!
Just10 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.