34

I am trying to configure venv on Windows Subsystem for Linux with Ubuntu.

What I have tried:

1) Installing venv through pip (pip3, to be exact)

pip3 install venv

I get the following error

ERROR: Could not find a version that satisfies the requirement venv (from versions: none)
ERROR: No matching distribution found for venv

2) Installing venv through apt and apt-get

sudo apt install python3-venv

In this case the installation seems to complete, but when I try to create a virtual environment with python3 -m venv ./venv, I get an error, telling me to do apt-get install python3-venv (which I just did!)

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: ['/mnt/c/Users/Vicubso/.../code/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

I have also read the following posts post 1, post 2, and several others. None of these seem to solve my problem.

Any help would be much appreciated.

vicubso
  • 443
  • 1
  • 4
  • 7

6 Answers6

56

Nothing here worked for me, but this did in WSL2:

sudo apt-get update
sudo apt-get install libpython3-dev
sudo apt-get install python3-venv
python3.8 -m venv whatever

Good luck!

Adam Birds
  • 394
  • 3
  • 21
trpt4him
  • 1,646
  • 21
  • 34
  • Recent updates to WSL have invalidated the currently accepted answer, I think. This worked for me via wsl.exe while the other suggestions gave lots of errors. – Paul Hulett Sep 18 '20 at 01:09
  • 2
    And you need sudo for the commands that you have mentioned. It works for me but not the previous commands anymore. +1 – Kashan Oct 28 '20 at 06:55
  • Worked for me with WSL2. I can't stress you enough just like the above comment: use sudo on apt-get. – Rahmat Nazali Salimi Jan 20 '22 at 03:37
  • 1
    works perfectly on WSL2. (tip: just use python3 -v as python could be updated to new version by the time you try it.. in our case we had python 3.10) – Prabhash Nov 10 '22 at 10:41
40

Give this approach a shot:

Install the pip:

sudo apt-get install python-pip

Install the virtual environment:

sudo pip install virtualenv

Store your virtual environments somewhere:

mkdir ~/.storevirtualenvs

Now you should be able to create a new virtualenv

virtualenv -p python3 yourVenv

To activate:

source yourVenv/bin/activate

To exit your new virtualenv, just deactivate

de_classified
  • 1,927
  • 1
  • 15
  • 19
  • 1
    It seems to work. But I don't know if it is the ideal solution. When I create a virtual environment with `virtualenv yourVenv` (maybe there's a typo in your answer?) It only works if I use `sudo`. Also... is there any advantage in using `virtualenv` instead of `venv`? But thanks a lot, though. At least now I have something to continue my work. – vicubso Apr 30 '20 at 17:08
  • Yep. See this [link](https://stackoverflow.com/questions/41573587/what-is-the-difference-between-venv-pyvenv-pyenv-virtualenv-virtualenvwrappe). It talks about the different variations and environments to run your programs. Some of the explanation is for windows and not **linux** systems. – de_classified Apr 30 '20 at 19:46
  • 1
    Thanks so much! Now have a better understanding now of all those options. I am accepting your answer as it solved my problem. – vicubso May 01 '20 at 20:24
  • I ran into some issues with finding the correct repositories ... adding `--fix-missing` when installing `python-pip` seemed to fix things. – BenedictWilkins Feb 10 '21 at 09:18
  • 1
    Some people may need to use: `sudo apt-get install python3-pip` – doublespaces Jun 13 '22 at 16:52
13

This was more of a headache than it needed to be. It seems that it relates to WSL<->Windows file system mapping issues. This blog post perhaps describes it better, but the net is you need to store additional metadata with files on a particular mount, as described in this MS devblog.

I fixed the issue by running:

sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o metadata

After which I was able to create python venv without needing to sudo.

Rollie
  • 4,391
  • 3
  • 33
  • 55
5

The error occurs when you're in /mnt/XXX (under Windows part).

Switch to Linux part by cd and run python3 -m venv ./venv again and that should be fine

Douglas Ferreira
  • 707
  • 2
  • 5
  • 22
kangbo
  • 51
  • 2
  • 1
0

I was getting the same error message, I have WSL(Ubuntu) installed on my computer, finally I found this doc: https://learn.microsoft.com/en-us/windows/python/web-frameworks#open-a-wsl---remote-window Ironically the only difference from what I was using as command was the name, I was using venv, then I run the command again using .venv so that the files become hidden files instead, and it worked. Hopefully it'll help someone else :)

0

You need to install also python3.8-venv via sudo apt install python3.8-venv

this fixed the problem for me.

werber bang
  • 173
  • 3
  • 11