WSL - GEDIT Unable to init server: Could not connect: Connection refused

118,294

Solution 1

You need an X server to run graphical applications like gedit. I use VcXsrv or Xming on my Windows desktops, both are very small and easy to install, but there exist other servers like Cygwin/X.

Short instructions for VcXsrv/Xming:

  • download and install the Windows package
  • start XLaunch or Xming
  • export the DISPLAY variable in your WSL terminal and start gedit

    export DISPLAY=0:0
    gedit
    

I have the problem that some checkboxes in gedit's preferences are not working (dconf error, I don't care), but other than that the editor is usable.

Related:

Solution 2

To fix this problem

Step 1- Download and Install this Windows X Server https://sourceforge.net/projects/vcxsrv/

Step 2- Open XLaunch

Step 3- Select " Full screen " option

Step 4- Choose " Start no client " option

Step 5- Simply do next and finish

Step 6- Open terminal and type :

export DISPLAY=0:0                                 
gedit filename

In case your Linux machine is available with host name = 'hostname' and you'd want to ssh into the hostname and then open gedit then execute the following commands:

  1. Enable X11Forwarding in the ssh server
[hostname] $ vim /etc/ssh/sshd_config
             # and set X11Forwarding to yes
             X11Forwarding yes
  1. Set the DISPLAY env in the client terminal and then ssl with -X flag
export DISPLAY=0:0
ssh -X yourusername@hostname
                                 
[hostname] $ gedit filename

Solution 3

It's probably because you are running it with root user. Run it as non-root user.

(If you don't know that if you are running it as root or not, enter the command below and if it returns 0, it means you are root and you must to login to non-root user: id -u)

(If you had run sudo su or something like that, It means you are root and you must use exit to go back!)

Share:
118,294
Ayush Basak
Author by

Ayush Basak

cs undergrad, loves tech and sleep

Updated on April 08, 2021

Comments

  • Ayush Basak
    Ayush Basak over 3 years

    I'm using Ubuntu 18.04 as Windows Subsystem for Linux on my Windows 10 machine.
    I've installed gedit without any errors or warnings.

    But whenever I try to use the command gedit something (here something is a filename)
    I get this error:

    Unable to init server: Could not connect: Connection refused
    
    (gedit:48): Gtk-WARNING **: 21:03:26.729: cannot open display:
    
  • Ayush Basak
    Ayush Basak over 4 years
    When I type id -u, it returns 1000 and when I type sudo su, then it returns 0. So I believe I'm not a root user?
  • Farbod Ahmadian
    Farbod Ahmadian over 4 years
    @AyushBasak If you get 1000, it means that you are not root user and gedit should open without any problem!
  • Ayush Basak
    Ayush Basak over 4 years
    well I'm getting an error.
  • Gabriel Fair
    Gabriel Fair over 3 years
    Step 3- Select " Full screen " option fixed it for me. Wild!
  • mschr
    mschr over 3 years
    This is not viable, maybe it is simply outdated or only works in WSL 1. To make this work, one would need to either ssh to HOST network and then host VcXsrv/Xming via that. The problem is, that the WSL localhost is NOT the same network interface as the HOST's localhost
  • Freddy
    Freddy over 3 years
    @mschr Yes, this was tested on WSL1 and the question is tagged as such (not wsl-2).
  • Chris A
    Chris A almost 3 years
    For me the line required was export DISPLAY=:0, as per superuser.com/a/1078889
  • Olivier de Rivoyre
    Olivier de Rivoyre over 2 years
    Note that with WSL2, you need to look in /etc/resolv.conf for the IP address of your windows, and export DISPLAY=[put the ip here]:0
  • lugger1
    lugger1 over 2 years
    To make it work with xming, I needed to use my local network IP (via router, found by using ipconfig comand in cmd, like 192.168.1.23), not external IP. export DISPLAY=192.168.1.23:0 works fine then, and Linux GUI apps are shown in Windows
  • Tonsic
    Tonsic over 2 years
    for me, starting VcXSrv, then at the WSL2 ubuntu terminal: export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 then run gedit and it worked. (I also checked the disable access control option, if that makes a difference or not..)