Run PyGame through WSL2 in 3 Steps

Mike
4 min readOct 7, 2022

--

Hope there are less programmer struggles like me.

No Shit Talks. Let’s begin.

Step 0 (Prerequisite): Properly Setup WSL2

I assume you’ve installed WSL2. In case you are sitting on the fence, I recommend you giving it a try. Please follow the instruction here for basic installation.

It’s quite easy to meet some error after the first installation, don’t worry, most of them are listed here. Basically, you may need to set up BIOS and run some command as administrator. It took me 30-40 minutes in this step, depends on your hardware. Most of the time spent on waiting.

Step1: Install VcXsrv

On Linux, there’s an awesome system called X Window that help you display all the windows on your screen. To run Pygame (or other GUI app) in WSL2, we need to create an X Window server that run in the win10/11 and tell WSL2 to communicate with it. VcXsrv is a lightweight tool that helps us on this.

Go here for installation. Just download, double click and run.

Now refers to your start menu, you’ll see XLanuch.

Click it and follow the screenshots

Just Google “VcXsrv Fullscreen” for more examples.
No need to change here.
##### Check Disable access control #####

Click finish. You’ll see a new icon appears in the system tray.

The indicator said DESKTOP-E5TN54K:0.0-2clients, it means:

  • DESKTOP-E5TN54K: my computer name, you’ll see different one
  • :” : at
  • 0.0 : <display>.[screen] format. At display 0 and screen 0.
  • 2 clients: 2 clients attach to this X Window server, you’ll see 0 client

Step 2: Edit your ~/.bashrc file

In some old tutorial, they’ll teach you to add the following command at the end of your ~/.bashrc file:

export DISPLAY=:0.0

This will work for WSL1, but for WSL2.

This command will tell WSL2 where to find the X Window Server. You can ignore the part export DISPLAY= , it’s just the shell script for system variable setting. The :0.0 is where the different happens. The format of this command is actually [host]:<display>.[screen]. When [host] is not given, it’ll be localhost or 127.0.0.1, so :0.0 is equivalent to localhost:0.0.

At the end of step 1, the explanation of the indicator helps here. We need to tell WSL the location of the X Window Server is

  • “external” Windows computer (DESKTOP-E5TN54K)
  • at (:)
  • display 0.screen 0 (0.0)

In WSL1, Linux and Windows share the same IP addresses, so :0.0 will work, localhostwill refer to DESKTOP-E5TN54K. But in WSL2, IP addresses are different. Run the following command in WSL2, it’ll tell you the IP address of Win10/11.

grep nameserver /etc/resolv.conf | awk '{print $2}'
To understand how this command work. Google “grep” and “awk” for more information.

So, we need to set

DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0

(i.e. DISPLAY=172.31.160.1:0.0 in my case)

Add this command at the end of your ~/.bashrc

Step 3: Test and Run

Reopen WSL2, install some testing program.

sudo apt update
sudo apt install x11-apps

Run xeyes to test if there’s something wrong.

If you see the same window, congrats!

Now run the pygame app

References

wsl-2-run-graphical-linux-desktop-applications-from-windows-10-bash-shell-erro

--

--