Steps to set up Python virtual environments in Ubuntu Linux (20.04 LTS: Focal Fossa) running on Windows Subsystem for Linux (WSL) for Windows 10. I’m running WSL 2 but I imagine WSL 1 works the same way. This is also almost certainly fine on an actual Ubuntu machine but I’ve been futzing around with my Linux laptop so can’t test that right now.
This came up as I try to figure out how to get a test suite running (as yet unsuccessfully) on different versions of Python 3.
- Add the
deadsnakesPersonal Package Archive (PPA) to your
$ sudo apt-add-repository ppa:deadsnakes/ppa
$ python -m pip install virtualenv
- Install whichever version(s) of Python you need e.g. Python 3.6, as here:
$ sudo apt install python3.6
- Depending on what you’re doing (for me, some work involving compilation with
F2PY), you might need the accompanying
header files and static library:
$ sudo apt install python3.6-dev
(you don’t need this to set up the virtual environment and can always install it later without needing to redo any of these other steps)
- Create a new virtual environment with the command:
$ virtualenv -p /usr/bin/python3.6 --clear ~/python/python36
-poption points to the Python version
--clearremoves (clears) the destination folder (if found)
- the last argument,
~/python/python36, specifies the destination folder
To activate the environment (changing the path to match the destination folder above as needed):
$ source ~/python/python36/bin/activate
pip etc should work as usual.