Ubuntu – Opening Ubuntu 20.04 Desktop on WSL2

command linevirtual-consolewindows 10windows-subsystem-for-linux

I installed Ubuntu 20.04 (server/terminal) from the Windows 10 app store, and then after launching the Ubuntu Terminal via WSL2 (Windows Subsystems for Linux) , I installed Ubuntu Desktop and did several steps to try and get a desktop instance of Ubuntu 20.04 launched. In short, the steps involved editing the .bashrc file, installing Xming, and then attempting to open the Ubuntu desktop in Xming with the "startxfce4" command. However, "startxfce4" just doesn't open the desktop for me. I can get programs to open with "grun" and the file explorer to open with "gopen" though, so it is working partially.

Does anyone have any alternative suggestions to how I might get the terminal to open up the full Ubuntu Desktop? Obviously, this wasn't what Windows had in mind with WSL2 so this is kind of a reach, but any input would be helpful. Could I somehow do this from the file explorer or with a program that could opened from "grun" or "gopen"?

Best Answer

  • I managed to get the full Ubuntu 20.04 Unity desktop GUI running using VcXsrv by installing the GNOME Desktop the ubuntu-wsl2-systemd-script and then appending the following to my ~/.bashrc file.

    Initially, Ubuntu is root access only, so create a normal account with sudo privilages;-

    sudo adduser username
    sudo usermod -aG sudo username
    exit
    

    From now on, start WSL using that account.

    wsl.exe -d distroname -u username
    

    NOTE:
    The ubuntu-wsl2-systemd-script calls systemd using --system-unit=basic.target thus does not start the gdm or lightdm service, so edit the enter-systemd-namespace and start-systemd-namespace files and change --system-unit=basic.target to --system-unit=default.target prior to installation.

    # runs only if 1st level shell
    if [ $SHLVL = 1 ] &&
       [ -d "${srv_path="/mnt/c/Program Files/VcXsrv"}" ] &&
       [ -f "${srv_exec="$srv_path"/"${srv_name=vcxsrv.exe}"}" ] &&
       [ -d "${sys_path="/mnt/c/Windows/System32"}" ] &&
       [ -f "${tasklist="$sys_path"/tasklist.exe}" ] &&
       [ -f "${taskkill="$sys_path"/taskkill.exe}" ]; then
       # find a Windows Process ID
       function find_wpid {
         echo $("$tasklist" | grep "$1" | awk '{print $2; exit}')
         }
       # kill a Windows Process ID
       function kill_wpid {
         "$taskkill" /PID $1 /F /T >/dev/null
         }
       # export the Windows %UserProfile% path in Linux Format
       export WIN_HOME="$(wslpath "$($sys_path"/cmd.exe" /c "<nul set /p=%UserProfile%" 2>/dev/null)")"
       # Find the current WSL2 local IP address
       srv_addr="$(ip route | awk '{print $3; exit}')";
       # shutdown old instance of VcXsrv if running
       if [ "${gfx_wpid=$(find_wpid $srv_name)}" ]; then
         kill_wpid $gfx_wpid
         unset gfx_wpid
       fi
       # start a new instance of VcXsrv
       "$srv_exec" :0 -ac -wgl -fullscreen -notrayicon -dpms &
       # wait for startup success
       while [ -z $gfx_wpid ]; do gfx_wpid=$(find_wpid $srv_name); done
       # Start Windows Pulse Audio if installed
       unset srv_exec srv_name 
       if [ -f "${srv_exec="$srv_path"/pulse/bin/"${srv_name="pulseaudio.exe"}"}" ]; then
          # shutdown old instance of Pulse if running
          if [ ${snd_wpid=$(find_wpid $srv_name)} ]; then
            kill_wpid $snd_wpid
            unset snd_wpid
          fi
          # delete the expired Pulse config
          if [ -d "${snd_conf=$WIN_HOME"/.config/pulse"}" ]; then
            rm -r "$snd_conf"
          unset snd_conf
          fi
          # Start a new instance of Pulse
          "$srv_exec" --use-pid-file=false --disable-shm=true --exit-idle-time=-1 &
          while [ -z $snd_wpid ]; do snd_wpid=$(find_wpid $srv_name); done
          export PULSE_SERVER=tcp:"$srv_addr";
       fi
       # setup Ubuntu Desktop
       export DISPLAY="$srv_addr":0; 
       # setting below: 0 = llvmpipe (faster), 1 = software rasterizer
       export LIBGL_ALWAYS_INDIRECT=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
       # cleanup expired vars
       unset tasklist sys_path srv_path srv_exec srv_name srv_addr
       # script stays here until gnome-session logout or failure
       gnome-session
       # shutdown VcXsrv
       if [ $gfx_wpid ]; then
         kill_wpid $gfx_wpid
         unset gfx_wpid 
       fi
       # shutdown Pulse
       if [ $snd_wpid ]; then
         kill_wpid $snd_wpid
         unset snd_wpid
       fi
       unset taskkill
       # shutdown the shell
       exit 0
    fi
    

    NOTE:
    When all is up and running, go to Ubuntu's Settings -> Privacy -> Screen Lock and turn off the screen lock options. Trying to log back in after a screen lock for some reason results in an infinite loop when asking for your password. If that happens, just shutdown the WSL distro and on next start all is OK again.

    Optional Audio Support

    Download and unpack mingw64-pulseaudio-13.0.7z. Edit the \mingw\etc\pulse\default.pa file and replace line 38 containing load-module module-waveout sink_name=output source_name=input with:

    load-module module-waveout sink_name=output source_name=input record=0
    load-module module-native-protocol-tcp listen=0.0.0.0 auth-anonymous=1
    

    Save the changes, rename the mingw folder as pulse and move it into your C:\Program Files\VcXsrv folder.
    I am unsure if this step is required, however I also installed these apps via...

    sudo apt-get install alsa-tools-gui pavumeter pavucontrol
    

    GDM3

    Solved a problem that the GDM service starts instead of GDM3;-

    sudo systemctl start gdm3.service    
    sudo dpkg-reconfigure gdm3
    

    HELP

    Can someone solve the problem that when running gui apps like Synaptic and
    BleachBit as root are marked as still running when they have been closed down.

  • Related Question