'xfce4-session "cannot connect display" error [closed]

I'm trying to run xubuntu-desktop on WSL as per the tutorial given by many sites. But I can't seem to connect to the display of VcXsrv and it always shows:

xfce4-session: Cannot open display: .
Type 'xfce4-session --help' for usage.

If I run startxfce4, it says:

/usr/bin/startxfce4: X server already running on display muhammadsalmanafzal:0.0
xrdb: Connection refused
xrdb: Can't open display 'muhammadsalmanafzal:0.0'
xfce4-session: Cannot open display: .
Type 'xfce4-session --help' for usage.

Although, the VcXsrv window of Xlaunch is closed. Can anybody help me look for the error? What am I doing wrong? Also, when I first installed xubuntu-desktop, at the very end it said:

Errors were encountered while processing:
 blueman
E: Sub-process /usr/bin/dpkg returned an error code (1)

And then I read somewhere to remove it, so I did and reinstalled xubuntu-desktop and no error was given.



Solution 1:[1]

If you are running WSL 1 then then, you need to add following line to .bashrc in home:

export DISPLAY=:0.0

and run bash again. However, if you are running WSL 2 then you need get the IPv4 of you WSL network (since by converting to WSL 2, it becomes a network) by checking the ipconfig in Powershell and then export the relative to .bashrc

[EDIT]

I think after the windows update to 20H2, the above solution stopped working. So, as per the official recommendation of Ubuntu from their site, you can add the following lines in your ~/.bashrc and restart it.

 export DISPLAY=:0 # in WSL 1
 export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 # in WSL 2
 export LIBGL_ALWAYS_INDIRECT=1

Solution 2:[2]

a better solution is run:

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

wsl2 changed when restarted and can't be fixed currently.

https://qiita.com/baibai25/items/5841b0592727893d960f

Solution 3:[3]

The following two approaches worked for me. I use WSL2.

Approach -1:

The title bar of VcXsrv says something like VcXsrv Server Display MACHINENAME:0.0.

Start xfce4 with this command: xfce4-session --display=MACHINENAME:0.0

Replace MACHINENAME with the name of your PC.

Reference: https://github.com/Microsoft/WSL/issues/1800#issuecomment-455791220

Approach-2:

Add the following line to .bashrc file.

export DISPLAY=MACHINENAME:0.0

Again MACHINENAME should be the name of your PC

Save the file and restart WSL2.

Now you can just use the command xfce4-session.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3