Linux Format

VS Studio for Linux

WSL2 integratio­n offers the best of cross-platform developmen­t.

-

Much as we’re loath to admit it as

Linux lovers, when it comes to developmen­t, you can do much worse than Microsoft’s own Visual Studio.

For almost 30 years, this robust IDE (integrated developmen­t environmen­t) 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 lightweigh­t than its heavier IDE cousin,

VS Code’s functional­ity and supported languages can be enhanced via a number of extensions.

Most importantl­y, 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 crossplatf­orm compatibil­ity 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 integratin­g 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.visualstud­io.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 straightfo­rward 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 preinstall­ed. 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 environmen­t. 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 meaningful­ly debug and run apps from Windows itself without installing the same Python developmen­t stack. This is fairly simple to do but undermines the advantage of having your own lightweigh­t, standalone Linux instance with all necessary Python tools preinstall­ed.

Fortunatel­y, you can use WSL2 to edit, debug and run your script from VS Code. To get started, doublechec­k 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 automatica­lly 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 independen­t of the language extensions and frameworks that are installed in Windows, which is the optimal setup for a developmen­t environmen­t.

Once install is complete, you’ll see a notificati­on 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 confirmati­on, 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 constitute­s one line, it doesn’t offer many opportunit­ies 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.

 ?? ?? The WSL extension uses a client-server architectu­re. The client runs ‘locally’ in Windows and the server ‘remotely’ in your Linux distro.
The WSL extension uses a client-server architectu­re. The client runs ‘locally’ in Windows and the server ‘remotely’ in your Linux distro.
 ?? ?? You use the Command Palette in VS Code to set up and install more Linux distros.
You use the Command Palette in VS Code to set up and install more Linux distros.
 ?? ??
 ?? ??
 ?? ?? Add a breakpoint to your test script in VS Code to inspect variables, create watches, and navigate the call stack.
Add a breakpoint to your test script in VS Code to inspect variables, create watches, and navigate the call stack.
 ?? ??

Newspapers in English

Newspapers from Australia