Running Linux on Windows with WSL

Running Linux on Windows with WSL

The Windows Subsystem for Linux (WSL) allows you to run a Linux environment directly on Windows without the need for a traditional virtual machine or dual-boot setup. This blog post dives into WSL2, the second generation of this powerful tool, explaining what it is, how it works, why you might want to use it, and some of its limitations.

What is WSL2?

WSL2 is an enhanced version of the original Windows Subsystem for Linux, introduced by Microsoft to provide better performance and compatibility for running Linux distributions on Windows. Unlike WSL1, which translated Linux system calls to Windows, WSL2 uses a lightweight virtual machine (VM) to run a full Linux kernel. This VM is optimized to use minimal resources while providing near-native performance.

How WSL2 Works

  • Lightweight VM: WSL2 uses Hyper-V technology to create a minimal virtual machine that runs the Linux kernel. This VM is highly optimized, booting quickly and consuming fewer resources than a traditional VM.
  • Linux Kernel: WSL2 includes a real Linux kernel, which Microsoft maintains and updates. This allows for full Linux system call compatibility, unlike WSL1's translation layer.
  • File System Integration: WSL2 supports seamless file sharing between Windows and Linux. Linux files are stored in a virtual disk (VHDX), and you can access Windows files from Linux via the /mnt directory. For performance-sensitive tasks, accessing Linux files from Windows using \\wsl$\ is recommended over accessing Windows files from Linux.
  • Networking: WSL2 uses a virtualized network stack, which provides good compatibility and isolation. In recent Windows builds, localhost forwarding works well, but some networking configurations (e.g., bridged networking or multicast traffic) may still require workarounds.
  • Interoperability: You can run Linux commands, scripts, and tools from Windows (e.g., via PowerShell or Command Prompt) and even invoke Windows executables from Linux.

When you install a Linux distribution (e.g., Ubuntu, Debian, or Kali) via WSL2, it runs in this VM environment, allowing you to use Linux tools alongside Windows applications.

Why Run Linux in a Windows Environment?

Development and Testing

Cross-Platform Development: Developers working on projects that need to run on both Linux and Windows can test their code in a native Linux environment without leaving Windows.

Example: A web developer can use WSL2 to run a Linux-based web server (e.g., Apache or Nginx) to test a Node.js or Python application in a Linux-like environment, ensuring compatibility with production servers.

Access to Linux Tools

Native Linux Utilities: Many powerful tools, like grep, awk, sed, or bash scripting, are native to Linux and work best in that environment.

Example: A data scientist can use Linux-based tools like jq for JSON processing or pandas in a Linux Python environment while still using Windows for visualization tools like Power BI.

Learning and Experimentation

Safe Linux Environment: WSL2 is ideal for learning Linux commands, scripting, or system administration in a safe, integrated environment.

Example: A student learning Docker can install it in WSL2 to practice containerization without needing a separate Linux machine.

DevOps and Cloud Workflows

Tooling Compatibility: Many cloud platforms (e.g., AWS, Azure, Google Cloud) use Linux-based servers. WSL2 allows you to run tools like kubectl, terraform, or ansible in a Linux environment that mirrors production systems.

Example: A DevOps engineer can manage Kubernetes clusters using kubectl in WSL2, ensuring commands behave as they would on a Linux server.

Simplified Setup

Quick Installation: WSL2 eliminates the need for complex VM configurations or dual-boot setups, making it easy to get started with Linux. You can install distributions from the Microsoft Store and start using them in minutes.

Limitations of WSL2

Performance Overhead

Although optimized, WSL2 runs in a VM, which introduces slight performance overhead compared to a native Linux installation. Disk I/O operations, especially on Windows file systems accessed via /mnt, can be slower.

Example: Compiling large codebases or running I/O-heavy tasks (e.g., database operations) may be slower compared to a native Linux machine.

Networking Complexity

WSL2’s virtualized network stack has improved, especially in recent Windows 11 updates where localhost bridging works well. However, some advanced networking features (e.g., raw sockets, multicast) may still be limited.

Example: Running a web server in WSL2 and accessing it from a Windows browser now often works out of the box, but more complex scenarios might require specific port forwarding or firewall rules.

GPU and Hardware Access

WSL2 supports GPU acceleration for machine learning and rendering, but this is limited to supported hardware (e.g., NVIDIA GPUs with CUDA on Windows 11). USB device access is still limited.

Example: You might struggle to connect a USB device to a Linux application in WSL2 without using additional tools like usbipd-win.

Systemd Limitations

As of Windows 11 version 22H2 and WSL version 0.67.6 or later, systemd is supported in WSL for compatible distributions like Ubuntu 22.04 and Debian. However, it must be enabled manually.

Example: Running certain system services (e.g., snapd or full Docker daemons) is now possible with systemd, but requires configuring the distribution’s settings.

Resource Consumption

WSL2’s VM can consume significant memory and disk space, especially if multiple distributions are installed or if the virtual disk grows large.

Example: A default Ubuntu installation might use several gigabytes of disk space, and memory usage can spike during intensive tasks. Disk space can be reclaimed by compacting the VHDX.

Getting Started with WSL2

  1. Check System Requirements:
    WSL2 requires Windows 10 version 1903 or higher (Build 18362+) or Windows 11, and virtualization must be enabled in your BIOS/UEFI.
  2. Enable Required Windows Features:
    Open PowerShell as Administrator and run the following commands to ensure that both VirtualMachinePlatform and Windows Subsystem for Linux are enabled:
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
        

    After enabling these features, restart your computer.
  3. Install WSL and Set WSL2 as Default:
    In PowerShell (run as Administrator), execute:
    wsl --install

    This installs the WSL2 components and a default Linux distribution (usually Ubuntu).
    Alternatively, to ensure WSL2 is used for future installs:

    wsl --set-default-version 2


  4. Install a Specific Linux Distribution (Optional):
    Use the Microsoft Store or this command:
    wsl --install -d <distro-name>

    Replace <distro-name> with a distribution like Debian, Ubuntu-22.04, etc.
  5. Update and Configure Your Distro:
    Open the installed Linux distribution and update it:

    sudo apt update && sudo apt upgrade

Conclusion

WSL2 is a game-changer for developers, IT professionals, and enthusiasts who want the power of Linux without leaving the Windows ecosystem. Its lightweight VM, full Linux kernel, and seamless integration make it ideal for development, testing, and learning. However, limitations like networking nuances, partial hardware access, and systemd configuration steps mean it’s not a complete replacement for a native Linux setup. By understanding its strengths and limitations, you can leverage WSL2 to enhance your workflow and bridge the gap between Windows and Linux.