Windows Subsystem for Linux (WSL) is an awesome feature of Windows 10. No more VMware or Oracle VirtualBox is required to run Linux on Windows. WSL is faster and less resource consuming, zero or less configuration compared to any other traditional Virtual Machines (VM). Installation and setup of Linux Distribution is lightning-fast. Because of seamless integration between Windows Host OS and Linux Guest OS, no separate SSH client (e.g. Putty) or SCP client (e.g. WinSCP) is required.  Docker can run directly inside of WSL2 (a full Linux Kernel built by Microsoft). Easy to setup isolated AWS cloud development environment locally using Docker, LocalStack, AWS CLI, SAM CLI, VSCode etc. What else do we, developers need!

Prerequisite

For WSL2, Windows 10 has to be on

  • Version 1903 or higher with Build 18362 or higher for x64 systems.
  • Version 2004 or higher with Build 19041 or higher for ARM64 systems.

In case, if you are wondering how to verify this,
Open Windows Run command (Windows logo key + R), type winver and click "OK" button".

turn windows10 optional feature on

or, open Windows command prompt and type ver

c:\>ver
Microsoft Windows [Version 10.0.18363.1679]

Enable Windows Optional Feature

Next, we'll turn on following two windows feature

  • Windows Subsystem for Linux
  • Virtual Machine Platform

We can do it in two ways:
Option#1:

  1. Open Windows Run command (Windows logo key + R)
  2. Type optionalfeatures
  3. Click "OK" button".
  4. Select checkboxes for Windows Subsystem for Linux and Virtual Machine Platform features.
    turn windows10 optional feature on
  5. Click "OK" button to apply changes.
  6. Once done, "Windows needs to reboot your PC to finish installing the requested changes". So, click "Restart now" button.

Option#2:

  1. Open Windows PowerShell in "Run as Administrator".
  2. Execute following command
PS C:\> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux, VirtualMachinePlatform

Do you want to restart the computer to complete this operation now?
[Y] Yes  [N] No  [?] Help (default is "Y"): Y             
  1. To apply this change, you need to restart the system. Therefore, enter "Y" when you will be asked to restart.

Reference: Enable-WindowsOptionalFeature

Enable-WindowsOptionalFeature
      -FeatureName <String[]>
      [-PackageName <String>]
      [-All]
      [-LimitAccess]
      [-Source <String[]>]
      [-NoRestart]
      [-Online]
      [-WindowsDirectory <String>]
      [-SystemDrive <String>]
      [-LogPath <String>]
      [-ScratchDirectory <String>]
      [-LogLevel <LogLevel>]
      [<CommonParameters>]

Description:

The Enable-WindowsOptionalFeature cmdlet enables or restores an optional feature in a Windows image.

Use the Online parameter to specify the running operating system on your local computer, or use the Path parameter to specify the location of a mounted Windows image.

The FeatureName parameter specifies the feature to add. You can specify more than one feature in the same package. Separate feature names with a comma.

Notes:
Get-WindowsOptionalFeature cmdlet gives all the features availble in the running operating system.

PS C:\> Get-WindowsOptionalFeature -Online

FeatureName : Printing-PrintToPDFServices-Features
State       : Enabled

FeatureName : Printing-XPSServices-Features
State       : Enabled

FeatureName : TelnetClient
State       : Disabled

FeatureName : TFTP
State       : Disabled

FeatureName : LegacyComponents
State       : Disabled

FeatureName : DirectPlay
State       : Disabled

FeatureName : Printing-Foundation-Features
State       : Enabled

FeatureName : Printing-Foundation-InternetPrinting-Client
State       : Enabled

FeatureName : Printing-Foundation-LPDPrintService
State       : Disabled

FeatureName : Printing-Foundation-LPRPortMonitor
State       : Disabled

FeatureName : SimpleTCP
State       : Disabled

FeatureName : Windows-Identity-Foundation
State       : Disabled

FeatureName : NetFx3
State       : DisabledWithPayloadRemoved

FeatureName : WCF-HTTP-Activation
State       : Disabled

FeatureName : WCF-NonHTTP-Activation
State       : Disabled

FeatureName : IIS-WebServerRole
State       : Disabled

FeatureName : IIS-WebServer
State       : Disabled

FeatureName : IIS-CommonHttpFeatures
State       : Disabled

FeatureName : IIS-HttpErrors
State       : Disabled

FeatureName : IIS-HttpRedirect
State       : Disabled

FeatureName : IIS-ApplicationDevelopment
State       : Disabled

FeatureName : IIS-Security
State       : Disabled

FeatureName : IIS-RequestFiltering
State       : Disabled

FeatureName : IIS-NetFxExtensibility
State       : Disabled

FeatureName : IIS-NetFxExtensibility45
State       : Disabled

FeatureName : IIS-HealthAndDiagnostics
State       : Disabled

FeatureName : IIS-HttpLogging
State       : Disabled

FeatureName : IIS-LoggingLibraries
State       : Disabled

FeatureName : IIS-RequestMonitor
State       : Disabled

FeatureName : IIS-HttpTracing
State       : Disabled

FeatureName : IIS-URLAuthorization
State       : Disabled

FeatureName : IIS-IPSecurity
State       : Disabled

FeatureName : IIS-Performance
State       : Disabled

FeatureName : IIS-HttpCompressionDynamic
State       : Disabled

FeatureName : IIS-WebServerManagementTools
State       : Disabled

FeatureName : IIS-ManagementScriptingTools
State       : Disabled

FeatureName : IIS-IIS6ManagementCompatibility
State       : Disabled

FeatureName : IIS-Metabase
State       : Disabled

FeatureName : WAS-WindowsActivationService
State       : Disabled

FeatureName : WAS-ProcessModel
State       : Disabled

FeatureName : WAS-NetFxEnvironment
State       : Disabled

FeatureName : WAS-ConfigurationAPI
State       : Disabled

FeatureName : IIS-HostableWebCore
State       : Disabled

FeatureName : WCF-Services45
State       : Enabled

FeatureName : WCF-HTTP-Activation45
State       : Disabled

FeatureName : WCF-TCP-Activation45
State       : Disabled

FeatureName : WCF-Pipe-Activation45
State       : Disabled

FeatureName : WCF-MSMQ-Activation45
State       : Disabled

FeatureName : WCF-TCP-PortSharing45
State       : Enabled

FeatureName : IIS-StaticContent
State       : Disabled

FeatureName : IIS-DefaultDocument
State       : Disabled

FeatureName : IIS-DirectoryBrowsing
State       : Disabled

FeatureName : IIS-WebDAV
State       : Disabled

FeatureName : IIS-WebSockets
State       : Disabled

FeatureName : IIS-ApplicationInit
State       : Disabled

FeatureName : IIS-ASPNET
State       : Disabled

FeatureName : IIS-ASPNET45
State       : Disabled

FeatureName : IIS-ASP
State       : Disabled

FeatureName : IIS-CGI
State       : Disabled

FeatureName : IIS-ISAPIExtensions
State       : Disabled

FeatureName : IIS-ISAPIFilter
State       : Disabled

FeatureName : IIS-ServerSideIncludes
State       : Disabled

FeatureName : IIS-CustomLogging
State       : Disabled

FeatureName : IIS-BasicAuthentication
State       : Disabled

FeatureName : IIS-HttpCompressionStatic
State       : Disabled

FeatureName : IIS-ManagementConsole
State       : Disabled

FeatureName : IIS-ManagementService
State       : Disabled

FeatureName : IIS-WMICompatibility
State       : Disabled

FeatureName : IIS-LegacyScripts
State       : Disabled

FeatureName : IIS-LegacySnapIn
State       : Disabled

FeatureName : IIS-FTPServer
State       : Disabled

FeatureName : IIS-FTPSvc
State       : Disabled

FeatureName : IIS-FTPExtensibility
State       : Disabled

FeatureName : MSMQ-Container
State       : Disabled

FeatureName : MSMQ-DCOMProxy
State       : Disabled

FeatureName : MSMQ-Server
State       : Disabled

FeatureName : MSMQ-ADIntegration
State       : Disabled

FeatureName : MSMQ-HTTP
State       : Disabled

FeatureName : MSMQ-Multicast
State       : Disabled

FeatureName : MSMQ-Triggers
State       : Disabled

FeatureName : IIS-CertProvider
State       : Disabled

FeatureName : IIS-WindowsAuthentication
State       : Disabled

FeatureName : IIS-DigestAuthentication
State       : Disabled

FeatureName : IIS-ClientCertificateMappingAuthentication
State       : Disabled

FeatureName : IIS-IISCertificateMappingAuthentication
State       : Disabled

FeatureName : IIS-ODBCLogging
State       : Disabled

FeatureName : MediaPlayback
State       : Enabled

FeatureName : WindowsMediaPlayer
State       : Enabled

FeatureName : DataCenterBridging
State       : Disabled

FeatureName : SmbDirect
State       : Enabled

FeatureName : HostGuardian
State       : Disabled

FeatureName : SMB1Protocol-Deprecation
State       : Disabled

FeatureName : MSRDC-Infrastructure
State       : Enabled

FeatureName : TIFFIFilter
State       : Disabled

FeatureName : NetFx4-AdvSrvs
State       : Enabled

FeatureName : NetFx4Extended-ASPNET45
State       : Disabled

FeatureName : MultiPoint-Connector
State       : Disabled

FeatureName : MultiPoint-Connector-Services
State       : Disabled

FeatureName : MultiPoint-Tools
State       : Disabled

FeatureName : Windows-Defender-Default-Definitions
State       : Disabled

FeatureName : SearchEngine-Client-Package
State       : Enabled

FeatureName : MicrosoftWindowsPowerShellV2Root
State       : Enabled

FeatureName : MicrosoftWindowsPowerShellV2
State       : Enabled

FeatureName : Client-DeviceLockdown
State       : Disabled

FeatureName : Client-EmbeddedShellLauncher
State       : Disabled

FeatureName : Client-EmbeddedBootExp
State       : Disabled

FeatureName : Client-EmbeddedLogon
State       : Disabled

FeatureName : Client-KeyboardFilter
State       : Disabled

FeatureName : Client-UnifiedWriteFilter
State       : Disabled

FeatureName : AppServerClient
State       : Disabled

FeatureName : WorkFolders-Client
State       : Enabled

FeatureName : Client-ProjFS
State       : Disabled

FeatureName : SMB1Protocol
State       : Disabled

FeatureName : SMB1Protocol-Client
State       : Disabled

FeatureName : SMB1Protocol-Server
State       : Disabled

FeatureName : Internet-Explorer-Optional-amd64
State       : Enabled

FeatureName : Microsoft-Windows-Subsystem-Linux
State       : Disabled

FeatureName : HypervisorPlatform
State       : Disabled

FeatureName : VirtualMachinePlatform
State       : Disabled

FeatureName : Containers-DisposableClientVM
State       : Enabled

FeatureName : Microsoft-Hyper-V-All
State       : Disabled

FeatureName : Microsoft-Hyper-V
State       : Disabled

FeatureName : Microsoft-Hyper-V-Tools-All
State       : Disabled

FeatureName : Microsoft-Hyper-V-Management-PowerShell
State       : Disabled

FeatureName : Microsoft-Hyper-V-Hypervisor
State       : Disabled

FeatureName : Microsoft-Hyper-V-Services
State       : Disabled

FeatureName : Microsoft-Hyper-V-Management-Clients
State       : Disabled

FeatureName : DirectoryServices-ADAM-Client
State       : Disabled

FeatureName : Windows-Defender-ApplicationGuard
State       : Disabled

FeatureName : ServicesForNFS-ClientOnly
State       : Disabled

FeatureName : ClientForNFS-Infrastructure
State       : Disabled

FeatureName : NFS-Administration
State       : Disabled

FeatureName : Containers
State       : Disabled

Download and Install Linux Kernel update package

Based on your System type download corresponding Linux Kernel update package.

Installation Steps:

  1. Double click to run the downloaded Windows Installer Package.
  2. In User Access Control Wizard, click "Yes" when it will ask "Do you want to allow this app to make changes to your device?"
  3. Click "Finish" button to complete.
turn windows10 optional feature on

Set WSL default version to 2 (WSL2)

Run the below command in Windows Command Prompt or Windows PowerShell to change the default installation version for new Linux distribution.

C:\Users\Abhijit>wsl  --set-default-version 2
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
The operation completed successfully.

Install Linux Distribution

Finally, we are good to install the preferred Linux distribution. We can do it in multiple ways.

Option#1:

  1. Run the following command in Windows Command Prompt or Windows PowerShell to display a list of available distributions for installation with wsl --install
    C:\Users\Abhijit>wsl --list --online
    The following is a list of valid distributions that can be installed.
    Install using 'wsl --install -d <Distro>'.
    
    NAME            FRIENDLY NAME
    Ubuntu          Ubuntu
    Debian          Debian GNU/Linux
    kali-linux      Kali Linux Rolling
    openSUSE-42     openSUSE Leap 42
    SLES-12         SUSE Linux Enterprise Server v12
    Ubuntu-16.04    Ubuntu 16.04 LTS
    Ubuntu-18.04    Ubuntu 18.04 LTS
    Ubuntu-20.04    Ubuntu 20.04 LTS
    
  2. Now, pass the desired distribution name while running wsl --install
    C:\Users\Abhimun>wsl --install --distribution Ubuntu-20.04
    Downloading: Ubuntu 20.04 LTS
    Installing: Ubuntu 20.04 LTS
    Ubuntu 20.04 LTS has been installed.
    Launching Ubuntu 20.04 LTS...
    
  3. Once installed, it will be Launched automatically.

Option#2:

  1. Open Microsoft Store
  2. Choose your preferred Linux Distributions (e.g. Ubuntu).
  3. Click "Install" button to start.
    turn windows10 optional feature on

Option#3:

  1. In case if Microsoft Store app is retricted by your organization or not avilable, then you can download offline installer.

Downloading distributions:

This will cause the .appx packages to download to a folder of your choosing.

  1. Double click the downloaded package.
  2. Click "Install" to start.
    turn windows10 optional feature on

Launch Linux Distribution

At the end of installation, you will get option to Launch the installed Linux Distribution. In my case, it is Ubuntu 20.04 and I am seeing the following in my screen.

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: abhijit
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04 LTS (GNU/Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Thu Aug 26 01:36:36 EDT 2021

  System load:  0.0                Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 1%                 IPv4 address for eth0: 172.23.22.192
  Swap usage:   0%

0 updates can be installed immediately.
0 of these updates are security updates.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once once a day. To disable it please create the
/home/abhijit/.hushlogin file.
abhijit@playground:~$

Bonus Section

If you're wondering how to change the hostname permanently on Ubuntu running on WSL2, then following video will help you.

If you prefer reading, then check the following article on How to change hostname on Ubuntu running on Windows WSL?

Summary

Because of "..seamless integration between Windows and Linux, fast boot times, a small resource footprint, and requires no VM configuration or management", lightning-fast installation & setup of Linux distribution, WSL2 is becoming the popular choice for local development.

References