How do I connect VSCode to WSL for development?

To connect Visual Studio Code (VSCode) to Windows Subsystem for Linux (WSL) for development, follow these steps:

  1. Install VSCode on Windows:

    • If you haven't already, download and install Visual Studio Code from the official website.
  2. Install the Remote - WSL Extension:

    • Open VSCode.

    • Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.

    • Search for 'Remote - WSL' in the Extensions marketplace search bar.

    • Click on the install button for the 'Remote - WSL' extension.

  3. Open a WSL Project:

    • Once the extension is installed, you can open a WSL project in several ways:

      • From VSCode Command Palette:

        • Press Ctrl+Shift+P to open the Command Palette.

        • Type 'Remote-WSL: New Window' and press Enter. This will open a new VSCode window connected to WSL.

        • Open your project folder by going to File > Open Folder and selecting a folder within your WSL file system.

      • From Windows File Explorer:

        • Navigate to your WSL project folder using Windows File Explorer.

        • Right-click on the folder and select 'Open with Code'. This will open the folder in VSCode within the WSL context.

      • From WSL Terminal:

        • Open your WSL terminal.

        • Navigate to your project directory using cd.

        • Type code . to open the current directory in VSCode.

  4. Work on Your Project:

    • VSCode will now operate as if it's running directly on Linux. You can use the integrated terminal in VSCode to run Linux commands, and you can install extensions and tools as you would on a Linux machine.
  5. Manage WSL Instances:

    • You can switch between different WSL distributions or instances by opening the Command Palette (Ctrl+Shift+P) and typing 'Remote-WSL: New Window Using Distro...' and selecting the distribution you want to use.

By following these steps, you'll be able to develop software using VSCode on your Windows machine while taking full advantage of the Linux environment provided by WSL.