Running and Debugging Your Application in Linux with WSL and Visual Studio Code

February 10, 2023#Software Development
Article
Author image.

Philippe Vaillancourt, Senior Consultant

In this quick tutorial, you will learn how to run and debug your application in Linux using WSL (Windows Subsystem for Linux) and Visual Studio Code. The demonstration uses a .NET C# application, but the process should work with any application that can be run in Visual Studio Code.

Step 1: Open Visual Studio Code and Install the WSL Extension

The first thing you need to do is open Visual Studio Code and go to the extension panel. From there, you’ll be looking for the official Microsoft extension, WSL. This extension is specifically designed for running applications in WSL through Visual Studio Code. Once you’ve installed the extension, you can close Visual Studio Code or leave it open – it doesn’t matter.

Step 2: Open a Terminal Window and Access WSL

Next, you’ll want to open a terminal window and access WSL. If you don’t have WSL installed on your system, you will need to look for videos and information online about how to install it. Once you have WSL installed, you can open it in the command line by typing wsl. This will open the Windows Subsystem for Linux app in the folder or directory you were in when you opened the terminal.

Step 3: Open Visual Studio Code in WSL

Now that you are in WSL, you can open Visual Studio Code in the same way you would on the Windows side of things. Simply type code . to open an instance of Visual Studio Code in that particular folder. You can tell that you’re running Visual Studio Code in Linux by looking at the integrated terminal and the Linux path, but other than that, the UI is the same.

Step 4: Install Extensions for WSL

Before you can start running and debugging your application, you’ll want to go back to the extension panel and make sure that the extensions you need are installed in the WSL section of the panel. Not all extensions that you have installed on the Windows side will be available in WSL, so you will need to explicitly install them. Microsoft has made this process easy by providing a cloud button that will install all your local extensions in WSL, or you can do it one at a time if you prefer.

Step 5: Debug Your Application

With the extensions installed, you can use Visual Studio Code in the exact same way as you would on the Windows side. To demonstrate this, the video accompanying this post sets a breakpoint and runs a debug test. When you hit the breakpoint, you can step through the process just as you would in the Windows environment.

Step 6: Run Your Application

Finally, if you want to run your application, you can do so straight from the command line. Simply navigate to the root of your project and type dotnet run. The application will build and run, and be available in the browser through localhost. At which point you can use VS Code to attach to the process and debug it, just as you would if it was running in the Windows environment.

In conclusion, running and debugging your application in Linux with WSL and Visual Studio Code is a simple and efficient process. You can use Visual Studio Code in the same way you would on the Windows side, with the added benefit of having your application run in Linux.


Copyright © 2024 NimblePros - All Rights Reserved