wsl2 getting to a usable desktop environment
Windows Subsystem for Linux is supposedly a great alternative to having a native Linux environment for those working in a Windows/MacOS development shop.
I chose to pursue this to avoid having to develop natively in Windows (or worse, reliving my time working in MacOS).
The initial setup was fine; however, I was immediately limited by the lack of a GUI interface when using my development tooling. My issues weren't rooted directly in needing a point-and-click interface, but rather that I was unable to utilize a fan-favorite screenshotting tool gnome-screenshot.
Solution
I worked through several different issues before arriving at a finished product, a working desktop environment on top of the WSL Ubuntu installation that I had installed.
Prerequisites
- WSL2 installed (I used Ubuntu)
- Administrative rights to your machine
Setup for the environment
Our setup will consist of 2 primary components:
- WSL itself, running a desktop session
- XLaunch
- PulseAudio will be a portion of this as well, which allows our WSL instance to send audio output
Setting up VcXsrv and PulseAudio
Our setup will consist of 2 primary components:
- WSL itself, running a desktop session
- XLaunch
- PulseAudio which allows our WSL instance to output audio
VcXsrv
The GUI interface we use will tie to a VcXsrv server that we run locally, to do so download the installer for Windows from SourceForge here.
Within the .bashrc
or .bash_profile
we'll need to place our configuration for
WSL to "talk" to the VcXsrv instance.
# inside our .bashrc or other config file
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
export LIBGL_ALWAYS_INDIRECT=1
After updating this, source
the file or restart your WSL instance to take
effect. This essentially ensures that we have our display settings for WSL
pointing to the same display that we'll have our VcXsrv listening to.
WSL2 desktop session
Next, we'll install a desktop interface utilizing our WSL install, I'm using Ubuntu
so I've installed lxde
:
sudo apt-get install lxde
PulseAudio
Setting up a Linux audio service on Windows is a bit tedious. Firstly, we'll need to download the service, extract it, then register it with Windows to run on startup.
Obtaining PulseAudio:
- Download this distribution of PulseAudio
- Extract the
.tar.gz
using the Command Prompt from inside yourDownloads
foldertar -xvzf x2goclient-contrib-4ad76bb.tar.gz
- Rename the parent folder from
x2goclient-contrib-4ad76bb
->pulse
- Place the
pulse
folder in theC:\
directory
PulseAudio configuration:
- Create a
.txt
file inside the newC:\pulse\
directory, then rename itconfig.pa
- Note: This is without the
.txt
extension
- Note: This is without the
- Paste the following into the config file, this will tell PulseAudio where to search for sound
- Note: The
172.16.0.0
is the default WSL local IP address range
- Note: The
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;172.16.0.0/12
load-module module-esound-protocol-tcp auth-ip-acl=127.0.0.1;172.16.0.0/12
load-module module-waveout sink_name=output source_name=input record=0
Installing PulseAudio as a service:
- Download NSSM (the non-sucking service manager)
- Extract the
.zip
to yourDownloads
folder - Move the
nssm.exe
to inside yourC:\pulse\
folder - Open PowerShell and run as an administrator to run the following to install PulseAudio
C:\pulse\nssm.exe install PulseAudio
- Fill in the prompt that opens we'll type our configuration
- Path:
C:\pulse\pulseaudio.exe
- Startup directory:
C:\pulse
- Arguments:
-F C:\pulse\config.pa --exit-idle-time=-1
- Disable termination due to inactivity
- Service name:
PulseAudio
- Path:
- Click the
Details
tab- Display name:
PulseAudio
- Display name:
- Install service
Starting PulseAudio:
- Open the
Task Manager
app - Navigate to the
Services
tab - Scroll down to
PulseAudio
- Right click ->
start
- Note: This will always be running from now on
Configure PulseAudio in WSL
- Install our audio library
sudo apt install libpulse0
- Open your WSL
.bashrc
or equivalent configuration file - Add the following to the
.bashrc
to allow discoverability of your PulseAudio service source
your configuration or restart your WSL instance
export HOST_IP="$(ip route |awk '/^default/{print $3}')"
export PULSE_SERVER="tcp:$HOST_IP"
Installing a web browser (extra)
Inside your WSL instance we can install a web browser using our terminal. I had some significant issues with the Google Chrome installation that Microsoft recommended. This was giving me some serious errors on startup, leading me to this open issue at time of writing.
Because this was quite tedious to work around I decided to use FireFox. Fortunately, this didn't raise any serious issues for me.
sudo apt-get install firefox
Running our new GUI environment
To launch the desktop GUI environment we'll utilize our new XLaunch application and WSL at the same time.
- Open your WSL instance
- Open the XLaunch application
- Select 1 large monitor (I don't use fullscreen)
- Change the display # from
-1
to0
(this is the display we set in our.bashrc
) - Click next until you see "Disable access control", check this box
- This will allow our WSL service to connect
- Optionally, you can save your config and it will act as a shortcut
- Within WSL launch your desktop service - mine was
lxde
startlxde
- this should now display to our window
Finished product: