This is end of 2023, WSL2, Ubuntu and GNOME is mature enough to start display manager automatically under basic.target when launching systemd. So, no need to modify ubuntu-wsl2-systemd-script. Below is a complete self-contained solution of WSL2 + Ubuntu GNOME connected via X forwarding. Please note that this answer is for X forwarding. If you wish to use Windows Remote Desktop to connect GNOME, please follow the working instructions here.
Environment:
- Host: Dell desktop PC, CPU: i5-4690, memory: 16GB DDR3
- Windows: Windows 10 22H2, Education 64 bit, [Version 10.0.19045.3693]
- WSL2: 2.0.9.0
- Guest OS: Ubuntu 22.04.3 LTS
- GUI desktop: GNOME 42.9
- X server: VcXsrv 1.20.14.0
Solution:
- In cmd.exe (you don't need to be Administrator) of Windows, run
wsl --install -d ubuntu
Reboot Windows as required.
- When WSL2 + Ubuntu installation is done, in Ubuntu command line, run
sudo apt update
sudo apt-mark hold acpid acpi-support
sudo apt upgrade
- Install GNOME:
sudo apt install ubuntu-desktop gnome.
- Install ubuntu-wsl2-systemd-script:
cd ~
git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script/
bash ubuntu-wsl2-systemd-script.sh
- Exit WSL and Ubuntu by typing
exit. In Windows command line, run
wsl --shutdown
wsl
- When you are back in the Ubuntu command line in WSL, export the following environment variables:
export DISPLAY="$(ip route | awk '{print $3; exit}')":0;
export XDG_CONFIG_HOME=$HOME/.config
export XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
export XDG_SESSION_DESKTOP=ubuntu
export XDG_SESSION_TYPE=x11
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
export XDG_SESSION_CLASS=user
export XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_RUNTIME_DIR="/run/user/1000"
sudo mkdir -p /run/user/1000
sudo chmod 700 /run/user/1000
sudo chown $(whoami): /run/user/1000
- Go to https://sourceforge.net/projects/vcxsrv/ to download VcXsrv Windows X Server. The version I am using is 1.20.14.0. Install it in Windows.
- Start VcXsrv by running XLaunch in Windows (if a previous VcXsrv is running, exit it. The settings of VcXsrv matter). Select "Fullscreen" in the first page. Fill up the "Additional parameters for VcXsrv" field in the third page with
-ac -wgl -dpms.
When VcXsrv starts, you might see a whole black screen. That's the effect of full screen without contents yet. Press Alt-Tab to switch to Ubuntu command line in WSL.
- Run
dbus-launch --exit-with-session gnome-session
- Go to the full-screen VcXsrv window. The Ubuntu GNOME desktop should show up now. Enjoy!
- You can just leave the GNOME alone when you don't use it. But if you really wanna quit GNOME, either close the VcXsrv window or press Ctrl-C on the Ubuntu command line in WSL.