26

I am trying to make a virtual environment for the development of my Django application. The commando's which I am using:

vagrant@vagrant:/var/www/djangogirls$ python3 -m venv myvenv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/var/www/djangogirls/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']


vagrant@vagrant:/var/www/djangogirls$ sudo apt-get install python3-venv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3-venv is already the newest version (3.5.1-3).
The following packages were automatically installed and are no longer required:
  javascript-common libjs-jquery libjs-sphinxdoc libjs-underscore python-pbr python-pkg-resources
  python-six python-stevedore python3-virtualenv virtualenv virtualenv-clone
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 108 not upgraded.

vagrant@vagrant:/var/www/djangogirls$ python3 -m ensurepip
ensurepip is disabled in Debian/Ubuntu for the system python.

Python modules for the system python are usually handled by dpkg and apt-get.

    apt-get install python-<module name>

Install the python-pip package to use pip itself.  Using pip together
with the system python might have unexpected results for any system installed
module, so use it on your own risk, or make sure to only use it in virtual
environments.


vagrant@vagrant:/var/www/djangogirls$ rm -r myvenv/ 

vagrant@vagrant:/var/www/djangogirls$ python3 -m venv myvenv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/var/www/djangogirls/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

As you can see, I am trying to make a myvenv, which cannot be created due to the python3-venv missing. I have already installed this, but the ensure pip is missing. After searching it seems that the system (Ubuntu 16.04) discourages the usage of the package. Could someone help me work around this problem?

3
  • 1
    The actual command that's failing is /var/www/djangogirls/myvenv/bin/python3 -Im ensurepip ..., which maybe failing for entirely different reasons.
    – muru
    Feb 3, 2017 at 14:37
  • Exactly @muru. The error-msg mislead the user about "missinG ensurepip" while it does not explain why the command failed - the user had to run it herself. Unfortunately OP didn't provide that, to diagnose the problem.
    – ankostis
    Jan 21, 2021 at 13:24
  • In case someone is on Ubuntu 22.04, gets errors building wheels in a virtualenv and looks for a better approach than ensurepip: sudo apt install virtualenv, then e.g. virtualenv --python=python3.9 venv, activate it, run pip install --upgrade pip wheel
    – LyteFM
    Jan 6 at 13:36

4 Answers 4

25

There is a related bug report here

ensurepip component is missing/disabled on Ubuntu

The way around it is to create a virtual environment without pip

python3 -m venv myvenv --without-pip

ensurepip component isn't called and a new environment is created in this case.

However missing pip in virtual environment can be a problem.

One solution is to install system pip3 package and use system pip module inside your virtual environment directly.

Virtual environment has to have access to system site packages to be able to use system pip module.

  1. install system python3 pip package

    sudo apt-get install python3-pip
    
  2. create virtual environment without pip and with access to system site packages

    python3 -m venv myvenv --without-pip --system-site-packages
    

You can use system pip module to install python packages into your virtual environment now.

Instead of pip install Django you have to use explicit

myvenv/bin/python3 -m pip install Django

or you may activate your virtual environment first

source myvenv/bin/activate
python3 -m pip install Django

python3 -m pip --version may comes handy to see which python environment is used.

Based on solution found here, but don't use proposed python get-pip.py in virtual environment, because it will steal system pip command

1
5

I had the same problem and installing python3-venv as instructed was not solving the ensurepip unavailability issue! However, since my python3 version is 3.7.5 installingpython3.7-venv` instead solved my issue.

3
  • 1
    You've seened it fixed 3y later probably because Debian fixed it (Python minor version were bumped +2 numbers from python3.5-->python3.7). But another case happens mostly around bump-times bc distutils are installed only for the latest python-release (see bugs.debian.org/cgi-bin/bugreport.cgi?bug=979819) and the command ['/var/www/djangogirls/myvenv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip'] fails with a ModuleNotFoundError: No module named 'distutils.util' error (which the user has to discover herself).
    – ankostis
    Jan 21, 2021 at 13:18
  • this did the trick for me with python3.8 after all other's failed - thanks @Sarah ESL! Feb 4, 2021 at 1:41
  • No joy here. Crikey. What a mess. Debian 11. Elpy failing to create a virtual env because ensurepip isnt available.
    – RichieHH
    Mar 16, 2021 at 1:24
0

Anaconda involucred

If you are using Anaconda or Conda this solution may help you:

Conda manages python itself as a package, so that conda update python is possible, in contrast to pip, which only manages Python packages. Conda is available in Anaconda and Miniconda (an easy-to-install download with just Python and conda).

very disturbing for me but well, hands to the keyboard in a terminal window:

conda update python

Look at this picture the result, maybe this help you, have a nice day!

0

I had a same problem and tried to run the command shown in the error

./venv/bin/python -Im ensurepip --upgrade --default-pip

You might see the output like this

Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/lib/python3.5/ensurepip/__main__.py", line 4, in <module>
    ensurepip._main()
  File "/usr/lib/python3.5/ensurepip/__init__.py", line 268, in _main
    default_pip=args.default_pip,
  File "/usr/lib/python3.5/ensurepip/__init__.py", line 174, in bootstrap
    _run_pip(args + _PROJECTS, additional_paths)
  File "/usr/lib/python3.5/ensurepip/__init__.py", line 67, in _run_pip
    pip.main(args)
  File "/tmp/tmp_yqoc6jf/pip-8.1.1-py2.py3-none-any.whl/pip/__init__.py", line 215, in main
  File "/usr/lib/python3.5/locale.py", line 594, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

Notice this line locale.Error: unsupported locale setting means that you forgot to set locate. So the solution is:

export LC_ALL=C
python3 -m venv venv

and you'll ready to go.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .