How to Set Up a Linux GUI on WSL2 in Windows 10
WSL2 (Windows Subsystem for Linux 2) allows you to run Linux commands and tools on Windows effortlessly. However, to use a full graphical user interface (GUI), additional setup is required. This guide will walk you through the steps to set up a Linux GUI on WSL2 in Windows 10.
Step 1: Update and Configure WSL2
Before installing a GUI, make sure WSL is updated to the latest version:
- Open PowerShell as an administrator and run the following command:
wsl --update
- Check which version of WSL you have installed by running:
wsl -l -v
- Ensure WSL2 is the default version. If WSL1 is set as default, switch to WSL2 with this command:
wsl --set-default-version 2
Step 2: Install a Linux Distribution
To work with a GUI, you'll need a Linux distribution. Install Ubuntu or any other preferred distribution from the Microsoft Store.
- Open the Microsoft Store and search for the Linux distribution of your choice, such as Ubuntu.
- Click Install and follow the instructions.
Step 3: Install an X Server for Windows
Since WSL2 doesn't have native GUI support, you'll need to install an X server to display Linux windows on Windows. A popular choice is VcXsrv.
- Download and install VcXsrv.
- Launch the program, select Multiple Windows, and check Disable Access Control for easier setup.
Step 4: Set the DISPLAY Variable
For Linux applications to interact with the X server, you need to set the DISPLAY
environment variable:
- Open your Linux distribution in WSL2 and enter:
echo "export DISPLAY=$(grep -oP '(?<=nameserver ).*' /etc/resolv.conf):0.0" >> ~/.bashrc
- Apply the changes with:
source ~/.bashrc
Step 5: Install a Desktop Environment
With the environment ready, install a desktop environment. Lightweight options include LXDE or Xfce:
sudo apt update
sudo apt install xfce4
Once installed, launch the desktop environment with:
startxfce4
Step 6: Running GUI Applications
After setting up the X server and desktop environment, you can run any Linux GUI application via WSL2. For example, try launching a web browser or text editor:
sudo apt install firefox
firefox