67

I just installed python3.7 on my 18.04LTS via the deadsnakes ppa:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.7 -y

Now I want to create a virtual environment with python3.7 -m venv env but I get

Error: Command '['/path/to/desired/env/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

python3 -m venv env0 worked fine before.

Why does this happen?
What can I do to create a virtual environment for python3.7 on Ubuntu 18.04LTS?

8 Answers 8

104

I am using python3.9. The command below solved the issue for me:

sudo apt-get install python3.9-venv
6
  • 1
    Logical suggestion, however running the command resulted in E: Unable to locate package python3.9-venv Jan 20, 2021 at 12:54
  • 2
    @MatejaPetrovic Install deadsnakes ppa first: sudo add-apt-repository ppa:deadsnakes/ppa Aug 8, 2021 at 17:28
  • @JoelGMathew Package 'python3.7-venv' has no installation candidate. Even with deadsnakes.
    – Gulzar
    Jan 24, 2022 at 13:58
  • @Gulzar It's definitely there. Make sure you ran sudo apt-get update first to refresh your local package listing.
    – Cerin
    Feb 20, 2022 at 2:47
  • @Cerin I later found out the problem was old ubuntu. see this
    – Gulzar
    Feb 20, 2022 at 8:12
6

I realised that python3.7 comes with bionic and removed ppa:deadsnakes/ppa as well as python3.7. After installing it regularly, I got the following:

$ python3.7 -m venv v2
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/.../v2/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']

python3-venv was already installed, but python3.7-venv wasn't, and with that I could create the environment.

1
  • You can create the virtual environment without installing pip by default with python3.7 -m venv --without-pip v2.
    – cserpell
    Sep 7, 2022 at 21:37
4

On my machine I've noticed that while python -m venv does not work, this command does:

virtualenv --python=$(which python3.7) venv

Edit:

There is a more modern version of this command these days. It should work on python3.7 and up.

python3.7 -m venv venv

As mentioned in the commands, this command should also work:

python3.7 -m virtualenv venv
5
  • What is your machine running?
    – muru
    May 17, 2021 at 13:48
  • Works for me on Amazon Linux AMI / RHEL Fedora! Jul 30, 2021 at 23:18
  • But virtualenv is not the same as venv.
    – wovano
    Oct 19, 2021 at 16:41
  • BTW: I think that python3.7 -m virtualenv venv does the same as your command (but is easier, IMHO).
    – wovano
    Oct 19, 2021 at 16:43
  • 1
    I really do not understand why but python3 -m venv venv does not work and python3 -m virtualenv venv works on my case. I'm using Python 3.9.7 in Pure OS 10 (Linux based OS )
    – vpz
    Jan 28, 2022 at 10:49
4

This worked for me (Thx Joseph..):
sudo apt-get install python3.10-dev
sudo apt-get install python3.10-venv
After this: python3.10 -m venv venv works fine!

2

Use this apt-get install python3.7-dev python3.7-venv to install python3.7-dev and python3.7-venv packages and you are good to go

0

This error occurred to me in a very different scenario. Hope it ends up helping someone.

I was working on my ntfs drives and they were being auto-mounted at boot in /etc/fstab using ntfs-3g with root permissions by default.

When trying to create virtualenv it showed me the same error. This was fixed by adding uid and gid particular flags in /etc/fstab

UUID=<uuid> <mount-path> ntfs <other-flags>,uid=<uid of user>,gid=<gid of user> 0 0
0

Maybe it's useful. Under WSL I had to:

$ sudo python3.8 -m venv venv
1
  • Solved it for me. Shame about the downvotes. Apr 25, 2023 at 16:57
0

I had python 2.7 on my system, and the default pip command was pointing to that installation. I think that's why python3 -m venv DIR failed. This, however, worked:

sudo pip3 install virtualenv
python3 -m virtualenv --python=python3 DIR
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Apr 12, 2023 at 10:07

You must log in to answer this question.

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