Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing and Running Linux Distributions with WSL2 on Windows

Tech 2

The Windows Subsystem for Linux (WSL) is a Windows feature that enables developers to install and run Linux distributions directly on Windows without the overhead of a traditional virtual machine or dual-boot setup.

WSL offers several conveniences:

  • Launch a Linux terminal direct from the Windows terminal without starting a full virtual machine.
  • Access Windows files from Linux without configuring shared folders.

This guide covers enabling WSL2 and installing a Linux distribution.

Enabling the WSL2 Feature

System Requirements for WSL2

  • Windows 10:
    • x64 systems: Version 1903 or later, Build 18362.1049 or higher.
    • ARM64 systems: Version 2004 or later, Build 19041 or higher.
  • Windows 11: All versions are supported.

To check your Windows version:

  1. Press Win + R to open the Run dialog.
  2. Type winver and press Enter.

Enabling Required Windows Features (Windows 11 Example)

  1. Open the Control Panel by pressing Win + R, typing control, and pressing Enter.
  2. Click Programs.
  3. Click Turn Windows features on or off.
  4. Scroll down and check the boxes for Windows Subsystem for Linux and Virtual Machine Platform.
  5. Click OK and restart your computer when prompted.

Setting WSL2 as the Default Version and Updating

Open a terminal (Command Prompt or PowerShell) as Administrator and run the following commands:

  1. Set WSL2 as the default version:
wsl --set-default-version 2
  1. Update the WSL kernel:
wsl --update

Verification: Run wsl -v to confirm the installation was successful.

Installing a Linux Distribution

You can install Linux distributions designed for WSL. Microsoft provides several methods.

Method 1: Microsoft Store

  1. Open the Microsoft Store.
  2. Search for your desired distribution (e.g., "Ubuntu 22.04").
  3. Click Get or Install.

After installation, launch the distribution from the Start Menu. On first launch, you will be prompted to create a new username and password.

Method 2: Using WSL Command Line

You can view available distributions with:

wsl --list --online

To install a specific distribution (e.g., Ubuntu 24.04):

wsl --install -d Ubuntu-24.04

If the --list --online command fails due to network issues, using the Microsoft Store is recommended.

Managing Linux Distributions with WSL Commands

Listing Installed Distributions

wsl --list --verbose

The output shows installed distributions, their state (Running/Stopped), and the WSL version (1 or 2). The default distribution is marked with an asterisk (*).

Running Linux

  • To start the default distribution: wsl
  • To set a new default distribution:
wsl --set-default <DistributionName>
  • To start a specific distribution with a specific user:
wsl --distribution <DistributionName> --user <UserName>

Changing the Default User for a Distribution

After initial setup, the default user is the one you created. To change it:

<DistributionName> config --default-user <UserName>

Example for Ubuntu 22.04:

ubuntu2204 config --default-user root

Exporting and Importing Linux Distributions

To move a distribution to free up space on the system drive (e.g., C:), you can export and import it.

  1. Shut down WSL:
wsl --shutdown
  1. Export the distribution to a tar file:
wsl --export <DistributionName> <ExportFilePath>

Example:

wsl --export Ubuntu-22.04 E:\Backup\ubuntu2204.tar
  1. Unregister (uninstall) the original distribution:
wsl --unregister <DistributionName>
  1. Import the distribution from the tar file to a new location:
wsl --import <DistributionName> <InstallLocation> <ExportFilePath>

Example:

wsl --import Ubuntu-22.04 E:\WSL\Ubuntu22 E:\Backup\ubuntu2204.tar

After a successful import, you can delete the original .tar export file.

Note: The imported distribution will use root as the default user. To set a different default user, you must modify the /etc/wsl.conf file within the distribution.

  1. Start the distribution.
  2. Run the following command, replacing <YourUsername> with an existing username:
echo -e "[user]\ndefault=<YourUsername>" >> /etc/wsl.conf
  1. Cloce the terminal and restart the distribution for the change to take effect.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.