WSL - GEDIT Unable to init server: Could not connect: Connection refused [closed]
Asked Answered
C

3

28

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:
Clop answered 18/2, 2020 at 15:39 Comment(0)
S
22

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:

Sterner answered 18/2, 2020 at 16:59 Comment(6)
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 localhostBogle
@Bogle Yes, this was tested on WSL1 and the question is tagged as such (not wsl-2).Sterner
For me the line required was export DISPLAY=:0, as per superuser.com/a/1078889Palladium
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]:0Ancohuma
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 WindowsCaracal
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..)Caravansary
R
10

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
Ranit answered 4/6, 2020 at 13:22 Comment(1)
Step 3- Select " Full screen " option fixed it for me. Wild!Meredith
N
1

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!)

Noah answered 18/2, 2020 at 15:59 Comment(3)
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?Clop
@AyushBasak If you get 1000, it means that you are not root user and gedit should open without any problem!Noah
well I'm getting an error.Clop

© 2022 - 2024 — McMap. All rights reserved.