Installing Multiple Ubuntu Systems in WSL2
This guide exlpains how to install multiple Ubuntu systems within Windows Subsystem for Linux version 2 (WSL2), specifically demonstrating how to import an additional Ubuntu 16.04 system using a tarballl.
Prerequisites
Ensure you have Docker installed and check its version:
docker --version
Setting Up WSL2
Before importing another distribution, configure WSL to use version 2 by default:
# Set WSL 2 as the default version
wsl --set-default-version 2
# Update existing Ubuntu 20.04 installation to WSL 2
wsl --set-version Ubuntu-20.04 2
Obtaining the Ubuntu 16.04 Tar File
To get the required tar file containing all Linux binaries of the desired distribution, we'll use Docker inside our current Ubuntu environment.
Installing Docker Inside Ubuntu 20.04
Create a script called setup-docker.sh with the following commands to set up Docker's APT repository:
#!/bin/bash
# setup-docker.sh
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
Run the setup script:
sudo sh ./setup-docker.sh
Install Docker components:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Verify the installation:
sudo docker run hello-world
Creating and Exporting the Ubuntu 16.04 Container
Pull the Ubuntu 16.04 image and create a container from it:
sudo docker pull ubuntu:16.04
sudo docker run -it ubuntu:16.04 /bin/bash
After exiting the container, export it to a tar file. First, identify the container ID:
CONTAINER_ID=$(sudo docker container ls -a | grep -i ubuntu:16.04 | awk '{print $1}')
sudo docker export $CONTAINER_ID > /mnt/c/temp/ubuntu1604.tar
Note: Ensure that
/mnt/c/temp/exists and is accessible from both Windows and WSL.
Importing the Distribution into WSL
With the tar file ready, import it into WSL using PowerShell:
-
Create directories if they don't exist:
cd C:\temp mkdir E:\wslDistroStorage\ubuntu1604 -
Import the tar file:
wsl --import Ubuntu-16.04 E:\wslDistroStorage\ubuntu1604 .\ubuntu1604.tar -
Verify the imported distribution:
wsl -l -vYou should see output similar to:
NAME STATE VERSION * Ubuntu-18.04 Running 2 CentOS Stopped 2 docker-desktop-data Running 2 docker-desktop Running 2 Ubuntu-16.04 Stopped 2
Configuring the Default User
Launch the newly imported Ubuntu 16.04 instance:
wsl -d Ubuntu-16.04
Inside the instance, update packages and install sudo:
apt-get update
apt-get install sudo
Add a new user with administrative privileges:
NEW_USER="ztl"
useradd -m -G sudo -s /bin/bash "$NEW_USER"
passwd "$NEW_USER"
Set this user as the default login user by creating or editing /etc/wsl.conf:
tee /etc/wsl.conf <<_EOF
[user]
default=${NEW_USER}
_EOF
Exit and restart the WSL instance so changes take effect:
exit
wsl -t Ubuntu-16.04
wsl -d Ubuntu-16.04
Final, set the root password for switching users via su:
sudo passwd root