how to set up vs code with wsl 2 windows 10 11
how to set up vs code with wsl 2 windows 10 11

how to set up vs code with wsl 2 windows 10 11

How to Set Up VS Code with WSL 2 on Windows 10 or 11

Greetings, readers!

In this comprehensive guide, we’ll embark on a detailed walkthrough to assist you in setting up Visual Studio Code (VS Code) with Windows Subsystem for Linux 2 (WSL 2) in Windows 10 or Windows 11. By leveraging the power of WSL 2, you’ll be able to seamlessly run Linux command-line tools and applications within your Windows environment. This guide will equip you with a step-by-step approach to configure VS Code to interact effortlessly with WSL 2, unlocking the benefits of both worlds.

Prerequisites

To get started, ensure you have the following prerequisites:

  • Windows 10 or Windows 11 operating system
  • Enable virtualization in BIOS or UEFI settings
  • Install Windows Subsystem for Linux 2

Step-by-Step Setup

1. Install Visual Studio Code

Visit the official VS Code website and download the appropriate installer for your system. Once downloaded, run the installer and follow the prompts to complete the installation.

2. Launch VS Code and Install Remote Development Extension Pack

Open VS Code and navigate to the Extensions tab. Search for "Remote Development" and install the "Remote Development" extension pack. This extension pack provides the necessary functionality to connect VS Code to WSL 2.

3. Install Ubuntu Distribution and WSL

Launch Windows PowerShell as an administrator and run the following command to install Ubuntu:

wsl --install -d Ubuntu

4. Configure WSL 2

Once Ubuntu is installed, configure WSL 2 by running the following command:

wsl --set-default-version 2

5. Open WSL Terminal

Launch a WSL terminal by running the following command in Windows PowerShell:

wsl -d Ubuntu

6. Update and Install Required Packages

Within the WSL terminal, update the package list and install essential packages:

sudo apt update
sudo apt install build-essential openssh-server

7. Enable SSH Server in WSL

Enable the SSH server within WSL to establish a connection from VS Code:

sudo systemctl enable ssh
sudo systemctl start ssh

Creating a New Project

1. Create a New Folder in WSL

Navigate to your desired project location within WSL and create a new directory:

mkdir my-project
cd my-project

2. Initialize a New Git Repository

Initialize a new Git repository within the project directory:

git init

Connecting VS Code to WSL

1. Connect to Remote WSL Instance

In VS Code, open the Command Palette (Ctrl + Shift + P) and search for "Connect to Remote WSL". Select the WSL instance you want to connect to.

2. Open the Project Folder

Once connected, navigate to the project folder you created in WSL:

/my-project

3. Set Up Remote Development Environment

Set up the remote development environment within VS Code by right-clicking on the project folder and selecting "Open in Local Terminal".

Debugging Applications

1. Configure Debugging in VS Code

Open the Debug configuration file (launch.json) and configure the following:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C++: WSL (GDB)",
      "cwd": "${workspaceFolder}",
      "program": "./my-app",
      "args": [],
      "showOutput": "always",
      "preLaunchTask": "build",
      "type": "cppdbg",
      "request": "launch",
      "gdbPath": "/usr/bin/gdb",
      "linuxDebugger": "gdb-multiarch",
      "miDebuggerPath": "/usr/bin/gdb-multiarch",
      "remote": true
    }
  ]
}

2. Build and Debug the Application

Press F5 to build and debug the application. The debugger will launch within the WSL environment.

Conclusion

Congratulations on successfully setting up VS Code with WSL 2 on Windows 10 or 11! This powerful combination unlocks the benefits of both worlds, allowing you to seamlessly develop applications using Linux tools and run them from within the Windows environment.

To further enhance your knowledge and skills, we recommend checking out our other articles on WSL 2 and VS Code:

  • Optimizing WSL 2 for Smooth Development
  • Advanced Techniques for Remote Development with VS Code

FAQ about setting up VS Code with WSL 2 on Windows 10/11

1. What is WSL 2?

  • WSL 2 is a virtualization tool that allows you to run Linux distributions within Windows, with full access to Linux tools and commands.

2. Why should I use WSL 2 for development in VS Code?

  • WSL 2 provides a powerful, Linux-based environment for developing in VS Code, offering better performance, stability, and compatibility.

3. How do I install WSL 2?

  • To install WSL 2, follow these steps:
    • Enable Hyper-V by searching for "Turn Windows Features On or Off" in the Start menu and checking the box for Hyper-V.
    • Install the Linux kernel update from the Microsoft Store.

4. How do I install a Linux distribution on WSL 2?

  • Open the Microsoft Store, search for a Linux distribution (e.g., Ubuntu), and install it.

5. How do I set up VS Code with WSL 2?

  • Install the "WSL" extension in VS Code.
  • Open VS Code and select "File" > "Open Folder" to open a folder in your WSL Linux distribution.

6. How do I access Linux files from Windows?

  • Open File Explorer and navigate to "wsl$<distribution_name>". You can also use the "Explorer" tab in the WSL terminal to view and manage Linux files.

7. How do I run Linux commands in VS Code?

  • Open a terminal window in VS Code (Ctrl + Shift + ‘`) and type Linux commands.

8. How do I use VS Code features, such as debugging, with WSL 2?

  • Install the "Remote Development" extension in VS Code. This allows you to debug and run code on your Linux distribution directly from VS Code.

9. How do I resolve issues with WSL 2?

10. Where can I find more resources on WSL 2 and VS Code?