VS Studio for Linux
WSL2 integration offers the best of cross-platform development.
Much as we’re loath to admit it as
Linux lovers, when it comes to development, you can do much worse than Microsoft’s own Visual Studio.
For almost 30 years, this robust IDE (integrated development environment) has been used to develop everything from basic scripts to websites and apps.
Visual Studio IDE is soon to be Windows only (VS Studio for Mac is due to be retired in August 2024). However, the standalone source code editor Visual
Studio Code is available for Windows,
Mac OS and Linux. While much more lightweight than its heavier IDE cousin,
VS Code’s functionality and supported languages can be enhanced via a number of extensions.
Most importantly, there’s an extension to connect VS Code with WSL2. This means you can develop apps on Windows while also running them in a dedicated Linux kernel. This makes it much easier to ensure crossplatform compatibility for your apps. It’s also hugely more efficient than other options, such as trying to mirror code across both physical and virtual machines.
Starting the Studio
Before integrating WSL2 with VS Code, we recommend repeating the steps on the previous pages to set up a dedicated Linux instance for running your chosen apps.
Next, you need to install VS Code via the Microsoft website: https://code.visualstudio.com/download.
The installer is available for Windows 10 and 11. By default, the download icon accesses the User Installer, which installs to your User folder, so doesn’t require admin privileges. A System installer is also available.
The installer is straightforward and offers numerous extra options. Choose freely among the rest, such as creating a dedicated Start Menu icon, but make sure the option Add to PATH is checked.
Once install is complete, open VS Code via your new desktop shortcut or the Start Menu. Look to the lefthand pane and click the extensions icon. In the search box that appears, type wsl . Click Install to continue.
Sharp-eyed users will no doubt have noticed a new status bar item, bottom-left. This is the Remote Status section, which you use to check if you’re running VS Code in a ‘local’ (Windows) or ‘remote’ (WSL) context.
Prod the python
Next use the Start Menu to launch your chosen Linux distro, or open Windows Terminal and run wsl .
For the purposes of this guide, we’re going to create a simple script in Python. Unlike most Unix systems, Windows doesn’t come with the language installed by default. As far as we can tell, all the Linux distros offered for install via WSL2 via the Microsoft Store do have Python 3 preinstalled. You can double-check this in Windows Terminal by running:
$ python3 – version
If Python 3 isn’t installed, simply run:
$ sudo apt-get update
$ sudo apt-get install python3 python3-pip
Make a folder for testing purposes and switch to it:
$ mkdir helloWorld
$ cd helloWorld
Next, create a simple test script:
$ echo ‘print(“Hi, don’’t forget to subscribe to Linux
Format!”)’ >> hello.py
Test it by running locally from the Linux instance: $ python3 hello.py
Linux coding in Windows
At this stage, you have created your own Python script within what amounts to a remote Linux environment. It
can be run from within the distro. You can even edit it using Nano or Vim.
Still, if you’re intent on developing a cross-platform app, remember that as things stand, you can’t meaningfully debug and run apps from Windows itself without installing the same Python development stack. This is fairly simple to do but undermines the advantage of having your own lightweight, standalone Linux instance with all necessary Python tools preinstalled.
Fortunately, you can use WSL2 to edit, debug and run your script from VS Code. To get started, doublecheck the terminal to make sure you’re still in the same folder as your test script, then run code . – the . tells VS Code to open in the current folder.
The terminal now displays a message stating Installing VS Code Server . You’ll also see a number after this – this is the version, which automatically matches that of the VS Code client you installed directly in Windows earlier.
In simplest terms, in running this command, you’re having VS Code install a small server to which the VS Code client on Windows itself can connect.
The VS Code server also installs host extensions in WSL, so that they run in the context of the tools and frameworks installed therein. This is independent of the language extensions and frameworks that are installed in Windows, which is the optimal setup for a development environment.
Once install is complete, you’ll see a notification that VS Code is connecting to WSL. You may also be prompted to authorise access to the Node.js-based server. The program itself then starts.
The first pop-up window asks you to confirm that you trust all files in the parent folder – in the case of this test, this is your home directory. Click Yes, I Trust The Authors to continue.
You can now check the Remote Status section at the bottom-left to confirm that VS Code is accessing your chosen WSL instance.
For further confirmation, take a moment to hover your mouse over the test script hello.py and note the file path. You’ll see that it matches the path in your chosen Linux distro.
Click on hello.py to begin editing. As this is a Python script, in this case VS Code prompts you to install the Microsoft Python extension for rich editing and improved debugging. Click Install to proceed.
Once this is complete, take a moment to click the extensions icon in the left-hand pane, or list them using Ctrl+Shift+X. From here you can see the ‘locally’ installed WSL extension, as well as a new section entitled WSL: [OS Name] – Installed. This shows that the extension is installed on the WSL side.
While you’re here, you can also search for and install other extensions into your WSL instance via VS Code,
such as the Pylint linter. This provides a richer palette as well as completion for most commands, making debugging much easier.
Next, take your code for a test spin by hitting F5, then choosing Python Debugger from the drop-down.
Bugging out
Given that our test script only constitutes one line, it doesn’t offer many opportunities to allow you to step through the code or show off VS Code’s debugging
features. Still, you can engineer a crisis yourself by clicking on line 1 and then pressing F9 to create a breakpoint.
Try to run the code again using F5, then select Python File. Your code now starts and hits the breakpoint. You’ll see you can now inspect variables, create watches and navigate the call stack. You can also press F10 to see the output of the print statement in the debug console.
To access other scripts stored in your WSL instance from VS Code, simply use File > Open. You’ll notice the directory tree here is for your Linux instance, though as before, you can still access Windows files via /mnt – for example, /mnt/c/.
If you do want to switch back to Windows files only, simply click the button marked Show Local. This launches the standard Windows Open dialog.
To end your WSL session and return to running code locally, select File > Close Remote Connection.