Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Remote WSL Tutorial

Introduction to Remote WSL

Windows Subsystem for Linux (WSL) allows you to run a Linux distribution alongside your existing Windows installation. Remote WSL is a feature in Visual Studio Code (VS Code) that enables you to develop inside WSL seamlessly. This tutorial will guide you through the process of setting up and using Remote WSL with VS Code.

Prerequisites

Before you begin, make sure you have the following:

  • Windows 10 or Windows 11 with WSL installed.
  • A Linux distribution installed within WSL (e.g., Ubuntu).
  • Visual Studio Code installed on your Windows machine.
  • The Remote - WSL extension installed in VS Code.

Installing WSL

To enable WSL on your Windows machine, follow these steps:

  1. Open PowerShell as Administrator and run the following command:
  2. wsl --install
  3. Restart your computer if prompted.
  4. After the restart, open the Microsoft Store and search for your favorite Linux distribution (e.g., Ubuntu) and install it.

Installing the Remote - WSL Extension

To install the Remote - WSL extension in VS Code, follow these steps:

  1. Open VS Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side.
  3. Search for "Remote - WSL" in the search bar.
  4. Click Install on the "Remote - WSL" extension from Microsoft.

Connecting to WSL from VS Code

Once you have WSL and the Remote - WSL extension set up, you can connect to your Linux environment by following these steps:

  1. Open VS Code.
  2. Press Ctrl + Shift + P to open the Command Palette.
  3. Type and select Remote-WSL: New Window.
  4. VS Code will open a new window connected to your WSL environment.

You should see a green indicator in the lower left corner that says "WSL: Ubuntu" (or the name of your Linux distribution).

Working with Files in WSL

You can access files in your WSL environment directly through VS Code. The file explorer will show your WSL file system. For example, you can navigate to your home directory in WSL by selecting /home/your-username.

Example: Opening a file in WSL

code my_script.py

In this example, my_script.py will open in VS Code, allowing you to edit it directly.

Running Commands in WSL

You can easily run terminal commands in your WSL environment directly from VS Code. Open the integrated terminal by going to Terminal > New Terminal. This terminal will automatically start in your WSL environment.

Example: Running a command in the integrated terminal

ls -la

This command lists all files, including hidden ones, in the current directory.

Debugging in WSL

VS Code also supports debugging applications running in WSL. You can set breakpoints and use the integrated debugger just like you would in a local environment.

To debug a Python script, for example, you can:

  1. Open your Python file.
  2. Set breakpoints by clicking in the gutter next to the line numbers.
  3. Press F5 to start debugging.

Conclusion

Remote WSL in Visual Studio Code provides a powerful and seamless development experience for users working in a Linux environment on Windows. By following this tutorial, you should be able to set up and start using Remote WSL effectively for your development tasks.