Ubuntu 20.04 with GUI running on WSL2

Joel Kell

16/10/2020

This tutorial consists of my efforts to compile the multiple sources and my own findings I had to use to get Ubuntu running on Windows Subsystem for Linux 2. Most of this tutorial is found from the great work done by Steve Brown here.

Install WSL2

Follow the Microsoft Documentation for installing WSL2 here if you're having trouble.

powershell

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

powershell

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Download WSL2 Linux Kernel update package for x64 or for ARM64.

powershell

wsl --set-default-version 2

Got to Windows Store and Download Ubuntu 20.04 LTS



Run Ubuntu and follow the instructions to set up the distrobution.
In Powershell check to see if Ubuntu is running on WSL 2.

powershell

wsl -l -v

If Ubuntu is running on version 1 run the following code to set the version to 2.

powershell

wsl.exe --set-version Ubuntu 2

Open up your Ubuntu shell again and run the following commands.

Ubuntu shell

sudo do-release-upgrade -d

Ubuntu shell

sudo apt-get update

Ubuntu shell

sudo apt-get upgrade

Ubuntu shell

sudo apt install tasksel

Ubuntu shell

sudo tasksel

Use space to select Ubuntu Desktop, language packs, and fonts. Then tab to <ok> and hit Enter to confirm.

Ubuntu shell

sudo apt install tigervnc-standalone-server

Ubuntu shell

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

Ubuntu shell

sudo dpkg -i packages-microsoft-prod.deb

Ubuntu shell

sudo apt update

Ubuntu shell

sudo apt install dotnet-runtime-3.1

Ubuntu shell

curl -s https://packagecloud.io/install/repositories/arkane-systems/wsl-translinux/script.deb.sh | sudo bash

Ubuntu shell

sudo apt install systemd-genie

Ubuntu shell

vncpasswd

Ubuntu shell

sudo vncpasswd

Ubuntu shell

sudo -u gdm vncpasswd

Ubuntu shell

sudo mv /usr/bin/Xorg /usr/bin/Xorg_old

Ubuntu shell

sudo nano /usr/bin/Xorg

Make sure to change the command to the resolution of your computer, mine is set 10 1920x1080.

Xorg


#!/bin/bash
for arg do
    shift
    case $arg in
         # Xvnc doesn't support vtxx argument. So we convert to ttyxx instead
        vt*)
            set -- "$@" "${arg//vt/tty}"
            ;;
        # -keeptty is not supported at all by Xvnc
        -keeptty)
            ;;
        # -novtswitch is not supported at all by Xvnc
        -novtswitch)
            ;;
        # other arguments are kept intact
        *)
            set -- "$@" "$arg"
            ;;
    esac
done

# Here you can change or add options to fit your needs
command=("/usr/bin/Xvnc" "-geometry" "1920x1080" "-PasswordFile" "${HOME:-/root}/.vnc/passwd" "$@") 

systemd-cat -t /usr/bin/Xorg echo "Starting Xvnc:" "${command[@]}"

exec "${command[@]}"
                            

Ubuntu shell

sudo chmod 0755 /usr/bin/Xorg

Ubuntu shell

mkdir /usr/lib/genie

Ubuntu shell

sudo nano /usr/lib/genie/deviated-preverts.conf

deviated-preverts.conf


{
    "daemonize": "/usr/bin/daemonize"
}
                        

Ubuntu shell

sudo apt install lightdm

Ubuntu shell

sudo dpkg-reconfigure lightdm

Use to lightdm Then tab to <ok> and hit Enter to confirm.

Ubuntu shell

sudo apt install gnome-session gdm3

Download VcXsrv

Download VcXsrv Windows X Server here and follow the installation instructions.

Ubuntu shell

sudo nano /etc/lightdm/lightdm.conf

lightdm.conf


[LightDM]
start-default-seat=false

[XDMCPServer]
enabled=true
port=177                        
                        

Ubuntu shell

sudo service lightdm restart

Create .wslconfig

Create a .wslconfig file with notepad and put it in C:\Users\username

.wslconfig


[wsl2]
memory=4GB
swap=2GB
localhostforwarding=true
                            

Make Ubuntu Desktop Recognise Internet Connection

Using your Ubuntu terminal again:

Ubuntu shell

sudo rm -rf /etc/netplan/*.yaml

Ubuntu shell

sudo nano /etc/netplan/01-network-manager-all.yaml

01-network-manager-all.yaml


# Let NetworkManager manage all devices on this system
network:
    version: 2
    renderer: NetworkManager                            
                        

Ubuntu shell

sudo netplan generate

Ubuntu shell

sudo netplan apply

Ubuntu shell

sudo service network-manager restart

Creating a desktop shortcut to launch Xlaunch

Create a folder somewhere on your Windows Machine. The end result should look like the image below.



Create a file with notepad and save it as config.xlaunch in the folder you just created. Copy the code below into the file.

config.xlaunch


<?xml version="1.0" encoding="UTF-8"?>
<XLaunch WindowMode="Windowed" ClientMode="XDMCP" LocalClient="False" Display="-1" LocalProgram="xcalc" RemoteProgram="xterm" RemotePassword="" PrivateKey="" RemoteHost="" RemoteUser="" XDMCPHost="" XDMCPBroadcast="False" XDMCPIndirect="False" Clipboard="True" ClipboardPrimary="False" ExtraParams="" Wgl="True" DisableAC="True" XDMCPTerminate="False"/>
                            

Create a file with notepad and save it as startup.sh in the folder you just created. Copy the code below into the file. If you get errors while running later make sure that the double and single quotes are the correct format. Change the path at the end of the file to the location on your PC where you saved the config.xlaunch file, making sure that it's preceeded by /mnt/.

startup.sh


#!/bin/bash
genie -i
sed -i -E "s/(XDMCPHost=\")[0-9.]*/\1`ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'`/" /mnt/d/VisualUbuntu/config.xlaunch
                            

Create a file with notepad and save it as run-ubuntu.ps1 in the folder you just created. Copy the code below into the file. Change the path at the end of the file to the location on your PC where you saved the startup.sh file, making sure that it's preceeded by /mnt/.

run-ubuntu.ps1

wsl "/mnt/d/VisualUbuntu/startup.sh;"

Open Powershell as Administrator and run the following commands

powershell

set-executionpolicy remotesigned

powershell

wsl --set-default Ubuntu-20.04

Create a file with notepad and save it as Ubuntu.bat in the folder you just created. Change the path in the PowerShell command to the location on your PC where you saved the run-ubuntu.ps1 file.

Ubuntu.bat


PowerShell -command "D:\VisualUbuntu\run-ubuntu.ps1"
start config.xlaunch
                            

The Ubuntu.bat script will start the wsl and change the value of the XDMCPHost in the config.xlaunch file to the ip of the wsl instance. We need to do this because the ip is variable and changes when you reboot your PC.
Right click on the Ubuntu.bat file and select Send to > and then Desktop(create shortcut)
Save the following .ico image. Then right click on the shortcut you created on your desktop and select Change Icon... Select the downloaded image.

Icon:



Now if you double-click on the shortcut on your desktop you should be able to login to Ubuntu visually.