Windows Subsystem for Linux (WSL) and Python
What we want to achieve is a fully-fledged Linux development environment inside Windows. Specifically, we want to run Visual Studio Code within Windows but the rest of the development tools (Python, Git, flake8, etc.) within the Windows Subsystem for Linux (WSL). To set this up, do the following:
Install Anaconda within WSL
Run in WSL:
sudo apt update sudo apt upgrade
to update Ubuntu packages to their latest versions. Then run
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh
to download the Anaconda installer. Start the installer using
bash Anaconda3-2022.05-Linux-x86_64.sh
Follow the prompts. Answer, 'yes' when the installer asks if you want to initialize Anaconda3. Once the installer has finished, restart the WSL Ubuntu terminal.
You should now have a working Anaconda installation in WSL. You can check this by running
python --version
You can also start a Jupyter server in WSL by running
jupyter notebook --no-browser
You can then connect to the Jupyter server by opening a browser in the host Windows and going to the localhost address shown in the WSL terminal.
Install Visual Studio Code in Windows (not WSL)
Go to https://code.visualstudio.com/ and download the VS Code installer for Windows. Run the installer and follow the prompts. Make sure that the option to add VS Code to the path is checked during installation.
Start VS Code from Windows (not WSL). Install the "Remote Development" extension pack in VS Code. Close VS Code. Then restart it from WSL using
code .
You should see a prompt that says that WSL is installing a VS Code server. Once that's finished, VS Code should start in the host Windows. This is a special VS Code session that is linked to WSL. You know that this step was successful if you see a green indicator in the lower left-hand corner of the VS Code window indicating that it is connected to WSL.
Now install the Python extension in VS Code. You should see that it will be installed as a remote extension in WSL instead of the host Windows system. Installing the Python extension should also install the Jupyter extension by default.
Note about graphical displays
The only limitation with the above setup is that it might not be able to open graphical displays from WSL. You can test this by running
anaconda-navigator
on WSL command line which should open a graphical user interface for Anaconda. If you're running the latest version of Windows 11 and WSL, this might work out-of-the-box for you. If it doesn't or if you're running Windows 10, you'll need to install an X window server to host the graphics in Windows. You can for example use MobaXterm (https://mobaxterm.mobatek.net/download.html) for this. Download and install MobaXterm and then start the WSL session from MobaXterm. You should now be able to see graphical displays, including those created by Python scripts even when run from VS Code.
Additional Resources
- https://www.how2shout.com/how-to/install-anaconda-wsl-windows-10-ubuntu-linux-app.html
- https://gist.github.com/kauffmanes/5e74916617f9993bc3479f401dfec7da