How to Install WSL2

Windows Subsystem for Linux (WSL) has revolutionized how developers and power users interact with Linux environments directly on Windows. WSL2, the latest iteration, represents a significant leap forward, offering a full Linux kernel and enhanced performance, especially for I/O-intensive operations. This guide will walk you through the process of installing and configuring WSL2, enabling you to harness the power of both Windows and Linux seamlessly.

Understanding WSL2: The Evolution of Linux on Windows

WSL2 isn’t just an upgrade; it’s a reimagining of how Linux runs on Windows. Unlike its predecessor, WSL1, which used a translation layer to convert Linux system calls into Windows equivalents, WSL2 leverages a lightweight virtual machine (VM) running a real Linux kernel. This architectural shift brings several key advantages:

Key Advantages of WSL2

  • Full System Call Compatibility: With a real Linux kernel, WSL2 offers complete system call compatibility. This means you can run a wider range of Linux applications, including Docker, which was previously challenging or impossible on WSL1 due to its limited system call support.
  • Improved Performance: For file system operations, WSL2 provides a substantial performance boost. While file access between Windows and Linux file systems might see a slight performance degradation compared to WSL1, accessing files within the Linux file system is significantly faster, particularly for I/O-bound tasks common in development workflows.
  • Real Linux Kernel: You get to experience a genuine Linux kernel, allowing for a more authentic Linux environment. This includes features like systemd support (in newer versions), which is crucial for many Linux applications and services.
  • Docker Desktop Integration: WSL2 is the engine behind Docker Desktop’s integration with Windows, providing a robust and efficient platform for containerized development.

Architectural Differences: WSL1 vs. WSL2

To fully appreciate WSL2, it’s helpful to understand its divergence from WSL1:

  • WSL1: Employs a translation layer that converts Linux system calls into Windows system calls. This offers excellent interoperability between Windows and Linux files but lacks full system call compatibility.
  • WSL2: Utilizes a lightweight utility VM with a real Linux kernel. This VM is managed by Windows and shares the host machine’s networking and IP address. While it has a dedicated virtual disk for the Linux file system, it still allows for relatively seamless interaction with Windows files.

Prerequisites for Installing WSL2

Before embarking on the installation journey, ensure your system meets the necessary requirements. These are crucial for a smooth and successful setup.

System Requirements

  • Operating System:
    • For x64 systems: Version 1903 or higher, with Build 18362 or higher.
    • For ARM64 systems: Version 2004 or higher, with Build 19041 or higher.
    • Note: Windows Home edition is supported.
  • Hardware Virtualization:
    • Virtualization must be enabled in your system’s BIOS/UEFI. Most modern processors support this feature. You can check if it’s enabled by opening Task Manager, going to the “Performance” tab, and selecting “CPU.” Look for “Virtualization” on the right-hand side; it should say “Enabled.” If not, you’ll need to restart your computer and enter the BIOS/UEFI settings to enable it.

Checking Your Windows Version

To verify your Windows version and build number:

  1. Press Windows key + R to open the Run dialog.
  2. Type winver and press Enter.
  3. A window will pop up displaying your Windows version and OS Build.

Installing WSL2: A Step-by-Step Guide

The installation process for WSL2 has been significantly streamlined over time, with modern Windows versions offering a single command to set up both WSL and a default Linux distribution.

Method 1: The Simplified Command (Recommended for Modern Windows)

For Windows 10 version 2004 and higher, or Windows 11, the installation can be accomplished with a single administrative command.

  1. Open PowerShell or Command Prompt as Administrator:

    • Search for “PowerShell” or “Command Prompt” in the Windows search bar.
    • Right-click on the application and select “Run as administrator.”
  2. Execute the Installation Command:

    • Type the following command and press Enter:
      bash
      wsl --install
    • This command performs several actions:
      • Enables the optional WSL component.
      • Enables the Virtual Machine Platform component.
      • Downloads and installs the latest Linux kernel.
      • Sets WSL2 as the default.
      • Downloads and installs a default Linux distribution (usually Ubuntu).
  3. Restart Your Computer:

    • After the command completes, you will be prompted to restart your computer to finalize the installation. Do so.
  4. Initial Linux Distribution Setup:

    • Upon restarting, your chosen Linux distribution will launch automatically.
    • You will be prompted to create a username and password for your Linux environment. This is separate from your Windows credentials.

Method 2: Manual Installation (For Older Windows Versions or Specific Control)

If the simplified command doesn’t work or you prefer a more granular approach, you can install WSL components manually.

Step 1: Enable WSL Component

  1. Open PowerShell as Administrator: (Same as in Method 1).
  2. Run the command to enable WSL:
    bash
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Step 2: Enable Virtual Machine Platform

  1. Run the command to enable Virtual Machine Platform:
    bash
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  2. Restart your computer. This step is crucial for the changes to take effect.

Step 3: Download and Install the Linux Kernel Update Package

WSL2 requires a specific Linux kernel. Microsoft provides an update package:

  1. Download the latest WSL2 Linux kernel update package: You can find the download link on the official Microsoft WSL documentation pages. Search for “WSL2 Linux kernel update package.”
  2. Run the downloaded installer: Follow the on-screen prompts to install the kernel update.

Step 4: Set WSL2 as the Default Version

  1. Open PowerShell as Administrator.
  2. Run the command to set WSL2 as the default:
    bash
    wsl --set-default-version 2

    • If this command produces an error, ensure you have completed steps 1-3 correctly, including restarting your computer.

Step 5: Install a Linux Distribution

You can install a Linux distribution from the Microsoft Store.

  1. Open the Microsoft Store.
  2. Search for your desired Linux distribution. Popular choices include:
    • Ubuntu (various versions)
    • Debian
    • Kali Linux
    • openSUSE
    • Alpine Linux
  3. Click “Get” or “Install” on the distribution’s page.

Step 6: Launch and Set Up Your Distribution

  1. Launch the installed distribution: You can find it in your Start Menu or by typing its name (e.g., ubuntu) in PowerShell or Command Prompt.
  2. Initial Setup: The first time you launch, it will take a few minutes to unpack and set up. You will then be prompted to create a username and password for your Linux environment.

Configuring WSL2 for Optimal Performance

Once WSL2 is installed, a few configuration steps can further enhance your experience and ensure smooth operation.

Updating Your Linux Distribution

It’s best practice to update your Linux distribution’s packages after installation.

  1. Open your WSL distribution.
  2. Run the following commands:
    bash
    sudo apt update
    sudo apt upgrade

    • sudo apt update refreshes the list of available packages and their versions.
    • sudo apt upgrade installs the latest versions of all installed packages.

Installing Windows Terminal

While you can use PowerShell or Command Prompt to launch your WSL distributions, the Windows Terminal offers a superior experience with tabs, multiple profiles, and customizable settings.

  1. Open the Microsoft Store.
  2. Search for “Windows Terminal.”
  3. Install Windows Terminal.
  4. Once installed, open Windows Terminal. You should see your WSL distributions listed as profiles in the dropdown menu. You can also configure it to launch a specific distribution by default.

Managing WSL Distributions

You can use the wsl command-line tool to manage your installed distributions.

  • List installed distributions:

    wsl --list --verbose
    # or shorter: wsl -l -v
    

    This will show the name of your distributions, their state (Running or Stopped), and the WSL version they are using (WSL1 or WSL2).

  • Set a distribution to use WSL2: If you have a distribution still set to WSL1, you can convert it:

    wsl --set-version <DistributionName> 2
    

    Replace <DistributionName> with the actual name of your distribution (e.g., Ubuntu-20.04).

  • Set a distribution to use WSL1: (Less common, but possible if needed)

    wsl --set-version <DistributionName> 1
    
  • Terminate a running distribution:

    wsl --terminate <DistributionName>
    
  • Unregister (uninstall) a distribution:
    bash
    wsl --unregister <DistributionName>

    • Caution: This will delete all data within that distribution’s file system.

Configuring .wslconfig

For advanced configurations, especially regarding memory and processor allocation for the WSL2 VM, you can create or modify the .wslconfig file.

  1. Locate the configuration file: This file should be placed in your Windows user profile directory: %USERPROFILE%.wslconfig. If it doesn’t exist, you can create it.
  2. Example .wslconfig:
    ini
    [wsl2] memory=4GB # Limits the WSL2 VM to 4GB of RAM
    processors=4 # Limits the WSL2 VM to 4 CPU cores
    # swap=0 # Disables swap space
    # localhostForwarding=true # Enables port forwarding from WSL2 to localhost
  3. Apply Changes: After modifying .wslconfig, you must terminate all running WSL distributions for the changes to take effect:
    bash
    wsl --shutdown

    Then, relaunch your WSL distribution.

Working with Files Between Windows and WSL2

A key aspect of WSL2 is its ability to access both Windows and Linux file systems.

Accessing Windows Files from WSL2

Your Windows drives are automatically mounted under /mnt/ within your WSL distribution.

  • For example, your C: drive is available at /mnt/c/.
  • You can navigate to it using the cd command:
    bash
    cd /mnt/c/Users/<YourWindowsUsername>/Documents

Accessing WSL2 Files from Windows

You can access your WSL2 file system directly from Windows File Explorer.

  1. Open File Explorer.
  2. In the address bar, type \wsl$ and press Enter.
  3. You will see a list of your installed Linux distributions. Clicking on one will allow you to browse its file system.
    • Note: While convenient, directly modifying files in the Linux file system from Windows applications is generally not recommended, as it can lead to performance issues or file corruption due to differing file locking mechanisms and line endings. It’s best to perform file operations within their native environment.

Common Use Cases and Next Steps

With WSL2 installed, the possibilities are vast. Here are some common scenarios and how to leverage them:

Development Environments

WSL2 is a game-changer for developers. You can:

  • Run Linux-based development tools: Node.js, Python, Ruby, Go, etc., with native Linux performance.
  • Use Docker: Install Docker Desktop, which natively uses WSL2 as its backend, providing a seamless containerization experience.
  • Compile code natively: Compile C/C++ or other code using Linux toolchains directly.
  • Test web servers: Run Apache, Nginx, or other web servers in their native Linux environment.

Command-Line Tools and Scripting

Access a vast ecosystem of Linux command-line utilities and scripting languages.

  • SSH into remote servers: Use ssh within WSL2 as you would on a native Linux machine.
  • Automate tasks: Write shell scripts (bash, zsh) to automate complex workflows.
  • Utilize powerful text editors: vim, emacs, nano are readily available.

Migrating from WSL1

If you have existing WSL1 distributions and want to upgrade to WSL2:

  1. Ensure WSL2 is set as the default: wsl --set-default-version 2
  2. Convert your distribution: wsl --set-version <DistributionName> 2
    • This process can take time depending on the size of your distribution’s file system.

Troubleshooting Common Issues

While WSL2 is robust, you might encounter occasional issues.

“WSL 2 installation is incomplete” Error

This often indicates that the Linux kernel update package was not installed correctly or is out of date. Re-download and run the latest kernel update package from Microsoft’s documentation and restart your computer.

Network Connectivity Problems

Sometimes, network configurations can cause issues.

  • Ensure that your VPN or proxy settings are not interfering.
  • Try restarting the WSL networking components by running wsl --shutdown in PowerShell and then relaunching your distribution.

Performance Degradation

  • File Access: Remember that accessing Windows files from WSL2 can be slower. For optimal performance, keep your project files within the WSL2 file system (e.g., in ~).
  • Resource Allocation: If your system feels sluggish, check the .wslconfig file to ensure you’re not allocating too many resources to the WSL2 VM.

By following these steps, you can successfully install, configure, and utilize WSL2, unlocking a powerful and flexible Linux environment directly within your Windows workflow. This synergy between operating systems opens up new avenues for development, productivity, and exploration.

Leave a Comment

Your email address will not be published. Required fields are marked *

FlyingMachineArena.org is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliates. As an Amazon Associate we earn affiliate commissions from qualifying purchases.
Scroll to Top