When you create a virtual environment, you might encounter the following error:
Error: Command '['/home/kc/Desktop/projects/pretty/venv/bin/python3.11', '-m', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.
This error indicates that the ensurepip module failed to set up pip in your virtual environment. This can happen if the Python installation is incomplete or corrupted, or if the virtual environment was not created properly.
Many Linux distributions disable the bundled ensurepip module to force use of system packaging. The steps below resolve this for Python 3.11 on Linux — adjust the version as needed.
Resolution
Ensure Python is properly installed
python3.11 --version
python3.11 -m ensurepip --version
Reinstall Python with venv and dev packages
If the commands above fail, reinstall Python along with the venv and dev packages:
sudo apt update
sudo apt install --reinstall python3.11 python3.11-venv python3.11-dev
If your system package name differs (e.g., python3.11-venv), adjust the package name accordingly.
Recreate the virtual environment
Delete the existing virtual environment and create a new one: