GUI, Desktop, or Window Manager on Windows Subsystem for Linux (WSL)

WSL can run Linux GUI applications when an X server is running on Windows. The basic idea is:

  1. Install a desktop environment or GUI application inside WSL.
  2. Start an X server on Windows, such as VcXsrv or Xming.
  3. Set the DISPLAY environment variable in WSL.
  4. Start the Linux GUI session or application.

This example uses Xfce.

Install Xfce

sudo apt update
sudo apt install xfce4

Configure the Display Variable

Open .bashrc in your home directory:

cd ~
nano .bashrc

Add this line:

export DISPLAY=:0.0

Reload the shell configuration, or close and reopen the WSL terminal:

source ~/.bashrc

Before starting Xfce, make sure the Windows-side X server is already running.

Start Xfce

The original command was:

xfce4-session

However, it is better to start Xfce with:

startxfce4

startxfce4 prepares the session startup more cleanly than running xfce4-session directly.

Avoid Starting Duplicate Sessions

If you want a simple guard before starting Xfce, check whether an Xfce session is already running:

if ! pgrep -x "xfce4-session" > /dev/null
then
    startxfce4
fi

For a specific GUI application, such as gedit, the same pattern can be used:

if pgrep -x "gedit" > /dev/null
then
    echo "Running"
else
    gedit
fi

Notes

On older WSL setups, a separate Windows X server is required. On newer Windows systems with WSLg, Linux GUI applications may work without manually setting DISPLAY. Check your local WSL version and Windows build before changing the setup.

P.S. Running xfce4-session directly should be avoided. Use startxfce4 instead.

Leave a Reply