BLOG POSTS
    MangoHost Blog / How to Install Windows Subsystem for Linux 2 on Windows 10
How to Install Windows Subsystem for Linux 2 on Windows 10

How to Install Windows Subsystem for Linux 2 on Windows 10

Windows Subsystem for Linux 2 (WSL2) represents a major evolution in running Linux environments directly on Windows machines, utilizing a lightweight virtual machine with a real Linux kernel rather than the translation layer approach of WSL1. This technology has become essential for developers who need to work with Linux tools, containers, and server environments while maintaining their Windows development setup, offering near-native Linux performance with seamless integration into the Windows ecosystem. This guide will walk you through the complete installation process, troubleshoot common issues, and explore practical applications that make WSL2 an indispensable tool for modern development workflows.

How WSL2 Works Under the Hood

WSL2 fundamentally differs from its predecessor by running a complete Linux kernel inside a lightweight Hyper-V virtual machine. Unlike WSL1, which translated Linux system calls to Windows equivalents, WSL2 provides full system call compatibility and significantly improved file system performance for Linux operations.

The architecture consists of three main components:

  • A lightweight utility VM running the Linux kernel
  • The WSL2 VM management system integrated into Windows
  • A network bridge that enables seamless communication between Windows and Linux environments
Feature WSL1 WSL2 Native Linux VM
Boot Time Instant 2-3 seconds 30-60 seconds
Memory Usage Low Moderate (dynamic) High (fixed)
File System Performance Slow for Linux operations Native Linux speed Native Linux speed
System Call Compatibility Limited 100% 100%
Cross-OS File Access Fast Slower across boundaries Network/shared folders only

Prerequisites and System Requirements

Before diving into installation, verify your system meets these requirements:

  • Windows 10 version 2004 (Build 19041) or higher, or Windows 11
  • UEFI firmware with Secure Boot capability
  • At least 4GB RAM (8GB recommended for development workloads)
  • Virtualization support enabled in BIOS/UEFI
  • Administrator privileges on the Windows machine

Check your Windows version by running:

winver

Verify virtualization support in Task Manager under Performance > CPU, or use:

systeminfo | findstr /i "Hyper-V"

Step-by-Step Installation Guide

Method 1: Using the Windows Store (Recommended)

The simplest approach leverages Microsoft’s streamlined installation process introduced in recent Windows updates.

Open PowerShell as Administrator and run:

wsl --install

This command automatically:

  • Enables the WSL and Virtual Machine Platform features
  • Downloads and installs the Linux kernel update
  • Sets WSL2 as the default version
  • Installs Ubuntu as the default distribution

After installation completes, restart your machine and launch Ubuntu from the Start menu to complete the initial setup.

Method 2: Manual Installation Process

For environments requiring more control or when the automated method fails, follow these detailed steps:

Step 1: Enable Windows Features

Open PowerShell as Administrator and execute:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Alternatively, use the GUI method through “Turn Windows features on or off” and enable:

  • Windows Subsystem for Linux
  • Virtual Machine Platform

Step 2: Restart and Download Kernel Update

After restarting, download the WSL2 Linux kernel update from Microsoft’s official documentation and install it.

Step 3: Set WSL2 as Default

wsl --set-default-version 2

Step 4: Install Linux Distribution

Install your preferred distribution from the Microsoft Store or use:

wsl --list --online
wsl --install -d Ubuntu-22.04

Configuration and Optimization

WSL Configuration File

Create a .wslconfig file in your Windows user directory to optimize WSL2 performance:

[wsl2]
memory=8GB
processors=4
swap=2GB
swapFile=%USERPROFILE%\\AppData\\Local\\Temp\\swap.vhdx
localhostForwarding=true
nestedVirtualization=false
debugConsole=false

Distribution-Specific Configuration

Create a wsl.conf file inside your Linux distribution at /etc/wsl.conf:

[automount]
enabled = true
root = /mnt/
options = "metadata,umask=22,fmask=11"
mountFsTab = false

[network]
generateHosts = true
generateResolvConf = true

[interop]
enabled = true
appendWindowsPath = true

Common Issues and Troubleshooting

Virtualization Not Enabled

If you encounter “Virtualization is not enabled” errors:

  • Access BIOS/UEFI settings during boot
  • Enable Intel VT-x or AMD-V virtualization
  • Enable Intel VT-d or AMD IOMMU if available
  • Disable any conflicting virtualization software

Installation Hangs or Fails

For stuck installations, try these solutions in sequence:

# Reset WSL service
wsl --shutdown
net stop LxssManager
net start LxssManager

# Reset specific distribution
wsl --terminate Ubuntu-22.04
wsl --unregister Ubuntu-22.04
wsl --install -d Ubuntu-22.04

Network Connectivity Issues

Common DNS and network problems can be resolved by:

# Inside WSL2 distribution
sudo rm /etc/resolv.conf
sudo bash -c 'echo nameserver 8.8.8.8 > /etc/resolv.conf'
sudo chattr +i /etc/resolv.conf

Performance Issues with Cross-Filesystem Operations

Keep project files within the Linux filesystem for optimal performance:

# Good: Working within Linux filesystem
cd ~
mkdir projects
cd projects

# Avoid: Working on Windows drives from Linux
# cd /mnt/c/Users/username/projects

Real-World Use Cases and Examples

Development Environment Setup

WSL2 excels in creating isolated development environments. Here’s a typical Node.js setup:

# Update system and install Node.js
sudo apt update && sudo apt upgrade -y
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

# Install development tools
sudo apt install -y git vim curl wget build-essential

# Verify installation
node --version && npm --version

Docker Development

WSL2 provides the optimal backend for Docker Desktop on Windows:

# Install Docker inside WSL2
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install docker-ce

# Add user to docker group
sudo usermod -aG docker $USER

Database Development

Running databases in WSL2 provides better performance than Windows alternatives:

# PostgreSQL installation
sudo apt install postgresql postgresql-contrib
sudo service postgresql start
sudo -u postgres createuser --interactive
sudo -u postgres createdb testdb

# MySQL installation  
sudo apt install mysql-server
sudo mysql_secure_installation

Advanced Integration Techniques

VS Code Integration

Microsoft’s Remote-WSL extension enables seamless development:

  • Install the “Remote – WSL” extension in VS Code
  • Access WSL2 projects directly from VS Code
  • Run integrated terminals within the Linux environment
  • Debug applications running in WSL2

Port Forwarding and Networking

Access WSL2 services from Windows or external networks:

# Forward WSL2 port to Windows
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=172.x.x.x

# Find WSL2 IP address
wsl hostname -I

File System Integration

Efficiently work with files across both systems:

# Access Windows files from WSL2
ls /mnt/c/Users/username/Documents

# Access WSL2 files from Windows
# Navigate to \\wsl$\Ubuntu-22.04\home\username in Explorer

# Set up symbolic links for common directories
ln -s /mnt/c/Users/username/Documents ~/documents

Performance Optimization and Best Practices

Memory Management

WSL2 dynamically allocates memory but may not release it efficiently. Monitor usage with:

# Check WSL2 memory usage
wsl -l -v
Get-Process vmmem

Force memory reclamation when needed:

wsl --shutdown
# Wait 8 seconds for complete shutdown
# Restart your distribution

Storage Optimization

WSL2 virtual disks can grow large over time. Compact them using:

# Shutdown WSL2
wsl --shutdown

# Compact the virtual disk (PowerShell as Admin)
diskpart
select vdisk file="C:\Users\username\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu22.04LTS_79rhkp1fndgsc\LocalState\ext4.vhdx"
compact vdisk
exit

Development Workflow Optimization

  • Keep source code within the Linux filesystem for faster I/O operations
  • Use WSL2-native tools rather than Windows equivalents when possible
  • Configure your shell with aliases and environment variables for efficiency
  • Leverage systemd services for background processes (where supported)

Security Considerations

WSL2 introduces specific security considerations that differ from traditional Linux deployments:

  • WSL2 instances share the Windows host’s network stack
  • File permissions between Windows and Linux filesystems behave differently
  • Windows Defender scans WSL2 filesystems, potentially impacting performance
  • SSH keys and credentials should be managed separately from Windows

Configure Windows Defender exclusions for WSL2 directories:

# Add exclusion for WSL2 virtual disk
Add-MpPreference -ExclusionPath "C:\Users\username\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu*"

Comparison with Alternative Solutions

WSL2 competes with several virtualization and containerization solutions for development workflows:

Solution Setup Complexity Resource Usage Integration Performance Best Use Case
WSL2 Low Moderate Excellent High Daily development on Windows
VirtualBox/VMware High High Poor Moderate Full OS isolation needed
Docker Desktop Low Moderate Good High Containerized applications
Dual Boot High None None Native Dedicated Linux workstation
Cloud VPS Low None local Network-based Variable Remote development

For developers who need the flexibility of a Linux environment while maintaining Windows productivity tools, WSL2 offers the optimal balance. Teams running complex server infrastructure might benefit from dedicated VPS solutions for staging and production environments, while keeping WSL2 for local development.

Integration with Server Infrastructure

WSL2 serves as an excellent bridge between local development and production server environments. Developers can replicate server configurations locally, test deployments, and maintain consistency with production dedicated server setups.

Common integration patterns include:

  • Using WSL2 to run identical software stacks as production servers
  • Testing infrastructure-as-code scripts locally before deployment
  • Running monitoring and logging tools in development environments
  • Developing and testing containerized applications that will run on Linux servers

WSL2 represents a significant advancement in bridging Windows and Linux development workflows, offering developers unprecedented flexibility and performance. By following this comprehensive setup guide and implementing the optimization strategies outlined, you’ll have a robust Linux development environment that seamlessly integrates with your existing Windows infrastructure while maintaining the performance characteristics needed for serious development work.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

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