The Windows Subsystem for Linux (WSL) allows you to run Linux distributions directly on Windows without the overhead of a traditional virtual machine. With WSL, you can install multiple distributions and even multiple versions of the same distribution, such as different versions of Ubuntu. This is particularly useful for developers who need to test applications across different environments or for users who want to explore various Linux setups.

Below is a step-by-step guide on how to use WSL to install multiple versions of Ubuntu on your Windows machine.


Prerequisites

Before you begin, ensure that you have the following:

  • Windows 10 Version 2004 or higher (Build 19041 or higher), or Windows 11.
  • WSL 2 enabled on your system.

Enabling WSL 2

  1. Enable the Windows Subsystem for Linux feature:
  • Open PowerShell as an administrator.
    • Press Win + X and select Windows PowerShell (Admin).
  • Run the following command: dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  1. Enable the Virtual Machine Platform feature:
  • Run this command in the same PowerShell window: dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. Restart your computer to apply the changes.
  2. Set WSL 2 as the default version:
  • After rebooting, open PowerShell as administrator again.
  • Run: wsl --set-default-version 2
  1. Install the Linux kernel update package (if prompted):

Method 1: Installing Multiple Ubuntu Versions from the Microsoft Store

The Microsoft Store offers several versions of Ubuntu that you can install side by side.

Steps:

  1. Open the Microsoft Store:
  • Click on the Microsoft Store icon in the taskbar or search for it in the Start menu.
  1. Search for Ubuntu:
  • In the search bar, type Ubuntu.
  1. Select the Desired Ubuntu Versions:
  • You’ll see multiple versions available, such as:
    • Ubuntu (usually the latest LTS version)
    • Ubuntu 20.04 LTS
    • Ubuntu 18.04 LTS
    • Ubuntu 22.04 LTS
  1. Install Each Version:
  • Click on the version you want and then click Get or Install.
  • Repeat this step for each version you wish to install.
  1. Launch and Set Up Each Distribution:
  • After installation, click Launch or find the app in the Start menu.
  • The first time you launch each Ubuntu version, you’ll be prompted to:
    • Create a UNIX username and password.
    • Set up your environment.
  1. Accessing Each Version:
  • Each installed version will appear as a separate app.
  • You can launch them individually from the Start menu.

Note: Each Ubuntu app from the Microsoft Store runs independently, allowing you to use different versions side by side without conflicts.


Method 2: Installing Multiple Instances Using wsl --import

If you want more control, such as installing multiple instances of the same Ubuntu version or using custom configurations, you can use the wsl --import command.

Steps:

  1. Download the Root Filesystem Tarball:
  • Go to the Ubuntu Cloud Images page.
  • Select the Ubuntu version you want (e.g., 20.04).
  • Download the rootfs tarball (e.g., ubuntu-20.04-server-cloudimg-amd64-root.tar.xz).
  1. Create a Directory for Each Instance:
  • Decide where you want to store your WSL distributions.
  • For example, create folders like D:\WSL\Ubuntu20.04_Instance1 and D:\WSL\Ubuntu20.04_Instance2.
  1. Import the Distribution:
  • Open PowerShell as administrator.
  • Run the following command to import the root filesystem: wsl --import Ubuntu20.04_Instance1 D:\WSL\Ubuntu20.04_Instance1 C:\Path\To\ubuntu-20.04-server-cloudimg-amd64-root.tar.xz --version 2
    • Replace Ubuntu20.04_Instance1 with a unique name for this instance.
    • Replace D:\WSL\Ubuntu20.04_Instance1 with the path to your instance directory.
    • Replace C:\Path\To\... with the path to your downloaded tarball.
  • Repeat this step for each instance, changing the names and paths accordingly.
  1. Initialize the New Distribution:
  • Launch the new distribution: wsl -d Ubuntu20.04_Instance1
  • You’ll start as the root user.
  1. Create a New User:
  • Inside the WSL terminal, add a new user: adduser your_username
  • Follow the prompts to set up the user.
  1. Set the Default User:
  • Create or edit the /etc/wsl.conf file: nano /etc/wsl.conf
  • Add the following content: [user] default=your_username
  • Save and exit (Ctrl + X, then Y, then Enter).
  1. Exit and Restart the Distribution:
  • Exit the WSL terminal: exit
  • Relaunch the distribution: wsl -d Ubuntu20.04_Instance1
  • You should now be logged in as your new user.
  1. Repeat for Additional Instances:
  • Follow the same steps for each additional instance you wish to create.

Advantages of Using wsl --import:

  • Customization: Allows you to import any Linux distribution or version.
  • Multiple Instances: You can have multiple instances of the same distribution.
  • Controlled Storage Location: Specify where the files are stored.

Managing Your WSL Distributions

Listing Installed Distributions

To see all installed WSL distributions and their statuses:

wsl --list --verbose

This will display information like:

  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  Ubuntu-20.04           Stopped         2
  Ubuntu20.04_Instance1  Running         2

Setting the Default Distribution

The default distribution is used when you run wsl without any arguments.

wsl --set-default <DistributionName>

Replace <DistributionName> with the name of your preferred distribution.

Changing WSL Version (1 or 2)

To change the WSL version of a distribution:

wsl --set-version <DistributionName> <VersionNumber>

For example:

wsl --set-version Ubuntu20.04_Instance1 2

Unregistering (Removing) a Distribution

To remove a distribution completely:

wsl --unregister <DistributionName>

Warning: This will delete all files and data in that distribution.


Accessing Linux Files from Windows

You can access the files of your WSL distributions directly from Windows Explorer:

  • Open Explorer and enter the path:
  \\wsl$\<DistributionName>\
  • Replace <DistributionName> with the name of your distribution (e.g., Ubuntu20.04_Instance1).

This allows you to easily transfer files between Windows and your Linux distributions.


Additional Tips

  • Updating Your Distribution: After setting up your distribution, it’s good practice to update packages:
  sudo apt update && sudo apt upgrade
  • Installing Common Tools: You can install development tools, editors, and utilities as needed:
  sudo apt install build-essential git curl wget
  • Switching Between Distributions:
  • You can open multiple distributions simultaneously.
  • Each runs independently with its own environment and installed packages.
  • Customizing the Environment:
  • Set up environment variables, aliases, and shell configurations in each distribution to suit your workflow.

Troubleshooting

WSL Version Issues

If you encounter issues related to the WSL version:

  • Check the Version:
  wsl --list --verbose
  • Set the Version:
  wsl --set-version <DistributionName> 2

Kernel Update Required

If prompted to update the kernel:

  • Download the latest WSL 2 kernel update from the Microsoft website.
  • Install the update and restart your computer.

Networking Issues

  • Connectivity Problems:
  • Ensure that your firewall settings allow WSL to access the network.
  • Sometimes restarting the LxssManager service can help: Restart-Service LxssManager

Permissions Issues

  • Running Commands as Administrator:
  • Some wsl commands require elevated privileges.
  • Always run PowerShell or Command Prompt as administrator when performing system-level changes.

Conclusion

By leveraging WSL, you can effortlessly run multiple versions and instances of Ubuntu on your Windows machine. Whether you’re testing applications in different environments, learning Linux, or setting up isolated development spaces, WSL provides a flexible and efficient solution without the need for virtual machines or dual-boot setups.


If you have any more questions or need further assistance, feel free to ask!