Coding

Windows Subsystem for Linux and Visual Studio Code

It feels like magic when it all works out of the box on the first try.

The free open-source IDE “Visual Studio Code” is one of the finest out there for coding. Combine that with a convenient platform like Windows Subsystem for Linux which allows running your favourite Linux distro with the frameworks and tools you need, and you have a perfect system to do development especially on Python.

Today, after a fresh install of Windows 10 on my AMD Ryzen 7 desktop, I went with enabling support for WSL through the following commands (or through Add/Remove Programs) and then setting the default version to WSL2:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2

You can then install the favourite Linux distro of your choice from Microsoft Store app. Once done you can check the version of the WSL instance by the first command below and if necessary convert the instance to WSL2 (in my case the instance was called “Ubuntu-20.04”).

wsl -l -v 
wsl --set-version Ubuntu-20.04 2

installing Chocolatey package manager, Visual Studio Code, Windows Terminal, Windows Subsystem for Linux (WSL) and then the extensions for VS Code including Python & Remote-WSL.

I felt happy when it all worked out of the box perfectly. I was able to launch a Linux console from Windows Terminal, which had my Windows user folders already mapped and mounted in Linux, then type:

code . 

to launch Visual Studio code from the current folder, set a breakpoint on a line, press F5 to run the debugger and get it all working.

Visual Studio Code running Python code in WSL Ubuntu Linux
Visual Studio Code running Python code in WSL Ubuntu Linux
Visual Studio Code debugging Python code in WSL Ubuntu Linux
Visual Studio Code debugging Python code in WSL Ubuntu Linux

After installing Ubuntu from Microsoft Store, remember to run

sudo apt update
sudo apt upgrade

Then run:

nano ~/.bashrc

to insert the following lines to have an alias for Python3 and Pip3:

alias python='usr/bin/python3.8'
alias pip=pip3

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.