178

I upgraded from ubuntu 14.04 to ubuntu 16.04 a few days ago. When I try to create a virtual env by using

pyvenv .venv

or

python3 -m venv .venv

There is an error:

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: ['/home/user/.venv/bin/python3.5', '-Im', 'ensurepip', '--upgrade', '--default-pip']

I tried running both

sudo apt-get install python3-venv

and

sudo apt-get install python3.5-venv

but it did not solve my problem.

6
  • My problem is solved on OS with release info: Distributor ID: LinuxMint Description: Linux Mint 18.3 Sylvia Release: 18.3
    – nix
    Jan 18, 2018 at 6:38
  • 2
    none of the answers solved it for me. still stuck on same error May 18, 2018 at 11:42
  • Possible duplicate of Python locale error: unsupported locale setting
    – S.S. Anne
    Sep 16, 2019 at 11:51
  • Advice for similar Qs: problems related to python3-venv need the exception from the Failing command: ['/home/user/... and are highly sensitive to: a) to the specific Debian/Ubuntu release we're talking about, b) whether the asked venv to install is the default for that distribution-release or an older/later one, and c) any customized apt-repos in /etc/apt/sources.list.
    – ankostis
    Jan 22, 2021 at 16:00
  • use virtualenv instead; python3 -m pip install virtualenv
    – Elijah
    May 30, 2022 at 4:22

32 Answers 32

228

try installing python3.6-venv:

sudo apt-get install python3.6-venv
13
  • 3
    Thanks for the solution. It worked on 16.04 (given that python3.6 was installed already of course). What I do NOT understand is that: 1. venv should precisely make using apt-get unecessary. It's part of the standard library. 2. why is the default version of pip using venv not the latest version? Jun 4, 2018 at 16:03
  • 62
    ...or sudo apt-get install python3.7-venv if you use python 3.7
    – Alleo
    Sep 27, 2018 at 2:03
  • 49
    ...or sudo apt-get install python3.8-venv if you use python 3.8
    – cowlinator
    Jan 22, 2020 at 0:58
  • 26
    ...or sudo apt-get install python3.9-venv if you use python 3.9 (for future) May 24, 2020 at 14:41
  • 63
    ...or sudo apt-get install python3.10-venv if you use python 3.10 (in case we survive 2020) Aug 28, 2020 at 19:44
143

It seems that it was a locale problem. Solved by executing:

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

found on this thread Python locale error: unsupported locale setting

15
  • 45
    It's crazy but it's true Sep 21, 2016 at 12:42
  • 10
    Really curious, but someone knows what is the reason? May 24, 2017 at 21:52
  • 2
    Thanks, running a minimal docker setup here. I'd never have guessed. Feb 22, 2018 at 12:14
  • 5
    I always run into problems like this when using Python and Pip. Either something isn't available / installed, a virtualenv isn't working, etc. Then you have to spend half an hour debugging stuff in a language where you may not be an expert. On top of that you get ambiguous suggestions like "You may need to use sudo with that command." which opens up a whole new rats nest.
    – Snowcrash
    Jan 14, 2021 at 9:04
  • 4
    It's crazy but this doesn't always solve the problem
    – JDiMatteo
    Feb 18, 2021 at 0:40
85

Under Windows Linux Subsystem and Ubuntu 18.04, this was caused by my not having upgraded recently.

I ran:

sudo apt update
sudo apt upgrade

Then sudo apt install python3-venv worked.

Note that I had also tried the UTF-8 solution beforehand (I made it part of my .bashrc), so that could have been a contributing factor.

5
  • 7
    I don't think the UTF-8 solution matters. I'm also on WSL Ubuntu 18.04 and the steps sudo apt update and sudo apt install python3-venv were sufficient.
    – Theja
    Jun 6, 2019 at 13:07
  • 1
    This is the only solution that worked for me! I tried changing locale and installing newer version of Python to no avail Apr 12, 2020 at 11:17
  • 2
    this worked for me too, I tried several other options .. it did not work ,
    – Learner
    Apr 20, 2020 at 8:59
  • 2
    Same here. I tried sudo apt install python3-venv several times without success. But everything worked after the update/upgrade.
    – joctee
    Jun 1, 2021 at 10:21
  • 1
    This worked on my WSL installation which I just installed today 5.15.79.1-microsoft-standard-WSL2. I did not need any of the other solutions.
    – Ð..
    Jan 10, 2023 at 2:58
62

One of the other answers fixed it for me last time, but with Python 3.7 I had to do:

apt install python3-pip python3-setuptools python3.7-venv

Followed by

python3.7 -m venv /path/to/venv
0
13

Resolved similar problems on Ubuntu18 when came upon this answer. It is similar to the one that worked for @Niko Rikken, except it doesn't really need any new PPA's and "python3.8-distutils" package. I was installing new python3.8 environment with venv and I already had "python3-venv" installed and up to date, so my solution was to install only "python3.8-venv":

% sudo apt-get install python3.8-venv

And that got this lines working:

% python3.8 -m venv ~/envs/new_env
% source ~/envs/new_env/bin/activate
11

In my case the next steps worked:

  • Ubuntu 18.04.4 LTS
 $ sudo apt-get install python3-venv python3.7-venv
 $ python3.7 -m venv [your_path_to_virtual_env_here]
9

In case this helps anyone down the line, I was getting the same error on Ubuntu 18.04. Setting the locales didn't work and trying to install python3-venv gave the error:

$ sudo apt-get install python3-venv
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 python3-venv : Depends: python3.6-venv (>= 3.6.5-2~) but it is not going to be installed
                Depends: python3 (= 3.6.5-3) but 3.6.7-1~18.04 is to be installed
E: Unable to correct problems, you have held broken packages.

And it looks like the apt repository had two versions of python:

$ apt list python3 -a
python3/bionic-updates,now 3.6.7-1~18.04 amd64 [installed]
python3/bionic 3.6.5-3 amd64

I tried to install Python3.6.5-3 but apt wanted to uninstall every dependency. I was able to solve the problem by installing Python3.7 and creating the venv with that:

$ sudo apt-get install python3.7 python3.7-venv
$ python3.7 -m venv my_venv
1
  • 1
    This worked for me. Did sudo apt install python3.8 python3.8-venv and created the virtual environment using python3.8 -m venv venv_name. Thanks! Dec 6, 2019 at 18:14
9

I encountered this problem on Ubuntu 18.04 for the recent release of Python-3.8. My solution was to add the Deadsnakes PPA which supplies the required python3.8-distutils package. The python3.8-venv package is already in the repository. Thanks to this blogpost:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.8
sudo apt install python3.8-distutils
sudo apt install python3.8-venv

Note: This is only a temporary solution. In the near future the required python3.8-distutils package will probably be available in the default Ubuntu repository.

Edit:

For Ubuntu 20.04 LTS the python3-distutils package is based on Python 3.8. As of yet there is no Python 3.8 package distutils package available for Ubuntu 18.04 LTS.

As mentioned in other comments on this thread, distutils might not be required. It was for my use-case, but please consider that solution before adding additional PPA's.

1
  • 1
    sudo apt install python3.8-venv was the winner for me. All the other help I could find had me trying to install python3-venv instead. Sep 19, 2020 at 0:38
8

I had to mention the specific version of python and replace python3.10 with you version

 $ sudo apt-get update -y && sudo apt-get upgrade -y
 $ sudo apt-get install python3.10-venv

Creating a virtual environment

$ python3.10 -m venv --system-site-packages Project_Name
3
  • I using Ubuntu22.04, when running "sudo apt-get install python3.10-venv", I got the following errors: " ....... The following packages have unmet dependencies: python3.10-venv : Depends: python3.10 (= 3.10.6-1~22.04) but 3.10.6-1~22.04.1 is to be installed E: Unable to correct problems, you have held broken packages. " Too many bugs in the python&ubuntu pakcage version management. Dec 29, 2022 at 15:46
  • python3.10 works with this solution. Feb 2, 2023 at 9:46
  • installing venv with specific version apt-get install python3.7-venv and then creating venv python3 -m venv .venv worked. Thanks Jun 12, 2023 at 10:31
7

Python updated or Default python changed and venv already installed

the venv installed on your system is installed by your previous python version(let say python3.6). that's why venv is not working with current python version(lets say 3.8).

so first check your default python or python3 version,( suppose ur current version is python3.8).

reinstall virtual environment by mentioning current python version(3.8) as follow

sudo apt-get install python3.xx-venv        >> replace xx with your current/default python version

if this helps you plz upvote, I'm new on this platform.

3
  • But the last command you suggested is that it doesn't work. You seem to solve another problem, that of an existing venv being broken when underlying python-interpreter gets updates, while the OP asks for help how to create a new venv.
    – ankostis
    Jan 20, 2021 at 20:55
  • Question was asked in 2016 and his problem has been solved also in 2016. So its obvious i'm not answering for him. I got same problem due to mention reason, and i came to here, but its take 2days to realise and solve problem. So i have mentioned problem before answering. Thanks 😊 for comments. Jan 22, 2021 at 2:46
  • This site does is not a forum, that is why it discourages irrelevant talk and not to-the-point solutions.
    – ankostis
    Jan 22, 2021 at 15:53
6
  1. First, make a directory :
mkdir testing
  1. Then, moved to this directory named testing :
cd testing
  1. When you type following command in this directory:
python3 -m venv env

You got error like :

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 install python3.8-venv
  1. Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
  1. Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari   

Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)

  1. Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
  1. If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.

After this, we can install anything that will be isolated from the rest of the system....

5

I was faced with the same problem and I am searching for a solution. It is about the problem:

ensurepip is disabled in Debian/Ubuntu for the system python.

And this my solution:

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

Try the following commands:

sudo apt install python-virtualenv
virtualenv --python=python3.6 myenv

These commands might work for you.

If you get any error like E: Unable to locate package python3-venv Then try the following commands:

sudo apt install python3.6-venv
4

Try : python3.* -m venv myvenv -

And don't forget to replace * with your specific version of python

3

If your intention was to get python3.8 incl. pip and venv on Ubuntu 18.04:

  • sudo apt install python3.8 python3.8-venv python3-pip # there is no python3.8-pip package
  • python3.8 -m venv venv
  • source venv/bin/activate
  • python --version # -> python 3.8.0
  • pip --version # -> pip 9.0.1 from /home/user/venv/lib/python3.8/site-packages (python 3.8)
0
3

Try installing python3-distutils as well.
Altogether, for python 3.8, the following worked for me.

$ apt-get install python3.8 python3.8-venv python3.8-distutils python3.8-dev

3

I just ran across this issue on several Debian/Ubuntu systems. Same error as above.

Findings

I tried to create a venv manually with:

python -m venv venvdir

This failed in the same way as others have mentioned. But it did create the shell of the venv. So I tried running ensurepip:

venvdir/bin/python -m ensurepip

Traceback (most recent call last):
    File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main                       
        return _run_code(code, main_globals, None,                                             
    File "/usr/lib/python3.9/runpy.py", line 87, in _run_code                                  
        exec(code, run_globals)                                                                
    File "/usr/lib/python3.9/ensurepip/__main__.py", line 5, in <module>                       
        sys.exit(ensurepip._main())                                                            
    File "/usr/lib/python3.9/ensurepip/__init__.py", line 266, in _main                        
        return _bootstrap(                                                                     
    File "/usr/lib/python3.9/ensurepip/__init__.py", line 166, in _bootstrap                  
         copy_wheels(dependencies, venv_wheel_dir, sys.path)                                    
    File "/usr/lib/python3.9/ensurepip/__init__.py", line 144, in copy_wheels                  
        assert len(wheel_names) == 1, wheel_names                                            

AssertionError: ['/usr/share/python-wheels/pyparsing-2.2.0-py2.py3-none-any.whl', '/usr/s
hare/python-wheels/pyparsing-2.4.7-py2.py3-none-any.whl']

This seemed odd. A quick look into /usr/share/python-wheels/ revealed - indeed - multiple versions of the same wheel as the error suggests. I have no clue why ensurepip is also ensuring that it only finds one wheel file, go figure.

Solution

A check of dpkg -S /usr/share/python-wheels indicated that the python-pip-whl package is the source of those files. This appears to be true for both Ubuntu and Debian.

So, I did:

cd /usr/share
sudo mv python-wheels python-wheels.old
sudo apt reinstall python-pip-whl

And it worked - no more errors. There are no longer duplicates in /usr/share/python-wheels

Absolutely no clue how there were duplicates or why ensurepip is so sensitive to duplicates there... Probably a package upgrade gone wrong somewhere.

2

I had the same problem - the python env has 2 versions for 2.7 and 3.6.

All you need to do is:

  1. Install the latest version of pip by installing pyenv installer
  2. Make sure you follow the steps of installing pyenv found here

Good Luck!

2

The solution for installing python3-venv is accurate since debian/ubuntu split the python distribution across multiple packages, so you do not actually have a full python install. If you really do not want to install this apt package, here is an alternative

python3 -m pip install virtualenv
virtualenv .venv

This will create fully functioning venv.

1

I had the same problem for an existing project when executing python3 -m venv venv. I had just updated my Ubuntu and Python versions. After removing the already existing venv folder the issue was solved. (I have also tried the UTF-8 solution.)

0
1

My problem were related to permissions and ownership. I was logged in with a different user as the owner of the current directory, which led to this error. After reviewing and fixing all permissions I was able to install the venv regularl

1
  • This was my problem exactly. When I changed to the current users home directory the command ran fine. This must be a bug where it assumes python3-venv isn't installed when it's actually hitting a permissions issue.
    – Cosmicnet
    Oct 12, 2020 at 15:43
1

This worked for me... Firstly, I ran

sudo apt-get update

Then

sudo apt-get install -y python3-venv zip

1

Ran into the same issue recently. None of the solutions mentioned above worked for me. I eventually get it to working by installing pip3.

apt-get install python3-pip

# then run
python3.8 -m venv env
1
  • even though I'm also using python3.8, this solution didn't work for me Jul 21, 2021 at 10:34
1

If you came across this issue while trying to run python -m build to build a python package, this means there probabaly is a syntax issue in your setup.cfg or setup.py file that causes an error creating the temporary venv required for installing dependencies.

Using pip wheels . will give you a less misleading error message.

0
1

For Linux, it is not installed by default you have to install venv

// at first check python version
python --version

// install 
sudo apt update
sudo apt install python3.8-venv
sudo apt install python3.8-distutils

// create new env
python3 -m venv project-name
source project-name/bin/activate
0

All of these suggestions didn't help me.

$ apt list python3 -a
python3/now 3.6.7-1~18.04 amd64 [installed,local]
python3/bionic 3.6.5-3 amd64

So I did: sudo apt-get install python3/bionic

Now I have python 3.6.5 and apt-list showed a better list: $ apt list python3 -a python3/bionic 3.6.5-3 amd64

With sudo apt-get install python3-venv/bionic I could install pythno3-venv and everything worked.

3
  • I'm using ubuntu about 5 years actively but I never seen before that syntax "python3/bionic" :O your answer solved my problem thanks. Feb 26, 2020 at 19:07
  • Glad it works, I didn't know that syntax either, I simply tried because "apt list" did show the packages that way ;-) If that answer helped you, you might hit the "up"-button.
    – Stefan
    Mar 2, 2020 at 14:58
  • If you can use nonstandard package names, it means you have nonstandard package sources configured. apt-cache policy python3/bionic might help you figure out where this package is coming from. It's not from Ubuntu.
    – tripleee
    May 7, 2020 at 9:25
0

In my case, running sudo apt-get install python3.8-venv succeeds but it shows the same error when running python3 -m venv .venv.

Finally, this command works out without changing locale.

python3.8 -c 'import venv; venv.create(".venv", with_pip=True)'

0

Here is my answer for Ubuntu 14.04. I was able to make venv and pip work with various Python versions. Details:

  • 3.4: Ubuntu 14.04 has Python 3.4 (as package python3.4 etc.). It works:

    $ sudo apt-get install python3.4 python3.4-dev python3.4-venv gcc libc6-dev
    $ mkdir /tmp/try3.4
    $ python3.4 -m venv /tmp/try3.4
    $ . /tmp/try/bin/activate
    (try3.4) $ pip install print-hello-world
    ...
    (try3.4) $ print-hello-world
    Hello World!
    

    If python3.4-venv is removed from the apt-get install command above, then python3.4 -m venv displays the same error message as in the question. However, the error message mentions apt-get install python3-venv to solve it, but that doesn't work, there is no such package. (The correct package name is python3.4-venv.)

    Please note that Python 3.4 is fairly old, and some Python packages available in PyPI (via pip) don't work with it.

  • 3.5: It can be installed from the deadsnakes repository. It works:

    $ sudo add-apt-repository ppa:deadsnakes/ppa
    $ sudo apt-get update
    $ sudo apt-get install python3.5 python3.5-dev python3.5-venv gcc libc6-dev
    $ mkdir /tmp/try3.5
    $ python3.5 -m venv /tmp/try
    $ . /tmp/try/bin/activate
    (try3.5) $ pip install print-hello-world
    ...
    (try3.5) $ print-hello-world
    Hello World!
    
  • 3.6: Ditto, it can be installed from the deadsnakes repository. It works:

    $ sudo add-apt-repository ppa:deadsnakes/ppa
    $ sudo apt-get update
    $ sudo apt-get install python3.6 python3.6-dev python3.6-venv gcc libc6-dev
    $ mkdir /tmp/try3.6
    $ python3.6 -m venv /tmp/try3.6
    $ . /tmp/try3.6/bin/activate
    (try3.6) $ pip install print-hello-world
    ...
    (try3.6) $ print-hello-world
    Hello World!
    
  • 3.7: It doesn't work, because pip install fails with import _ssl, and python3.7 in the deadsnakes repo doesn't have that module, because Ubuntu 14.04 ships with on old version of OpenSSL which Python 3.7 doesn't support. See more details in this bug.

  • 3.8--: No Ubuntu 14.04 package for these Python versions in the Ubuntu or deadsnakes repositories.

1
  • FYI, this late answer, while technically correct and useful for Ubuntu-14.04, does not answer the original Q for Ubuntu-16.04 :-(
    – ankostis
    Jan 22, 2021 at 15:57
0

In my case, the command failed because I was still in a virtual env but did not notice it very quickly.

If it is the case, try calling deactivate.

0

I do not have sudo rights and I have to use python3.5:

  1. Install virtualenv: pip3 virtualenv
  2. Create virtualenv without pip: python3 -m venv --without-pip <path>
  3. Downloaded the proper pip bootstrap: https://bootstrap.pypa.io/pip/3.5/get-pip.py
  4. Run: <path>/bin/python3 get-pip.py

I did 'source <path>/bin/activate' and I got a working environment.

Not the answer you're looking for? Browse other questions tagged or ask your own question.