Solution 1:

Solved problem with executing this command

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

This command can also be added in shell initialization files, for example, .bashrc, .zshrc etc.

Also make sure to allow public access of X server running in Windows 10. For VcXsrv, -ac option is most important because it allows public access. For Xming, run through XLaunch and in the part "Specify parameter settings" select "No Access Control".

For more see this discussion Microsoft/WSL: Can't use X-Server in WSL2.

Solution 2:

I encountered this issue after changing the version of my WSL Ubuntu distribution from 1 to 2. Using

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

was not sufficient. According to https://github.com/microsoft/WSL/issues/6181 I needed to reconfigure the firewall. This can be done by

  • going to Advanced Security -> Inbound rules (admin permissions required)
  • delete all rules for Vcxsvr
  • restarting Vcxsvr, which prompts you to set Windows Firewall for this program
  • allow all (or enough) access

After these 2 changes, the problem was solved.