Error: Can't open display / DISPLAY environment variable not set: how to fix it

The program wants to draw a window, but the DISPLAY variable is empty or points at an X server it can't reach. If you don't need a window, run the program headless (for matplotlib, use the Agg backend). If you do need one over SSH, connect with ssh -X and make sure the server allows X11 forwarding.

Applies to:Linux and Unix hosts running X11 client programs (servers, SSH sessions, WSL, CI runners, cron)

What it means

A graphical program on Linux or Unix needs to know where to draw its window. It finds out from the DISPLAY environment variable, which looks like host:display.screen, for example :0 or localhost:10.0. If the variable is empty, or names an X server that the program can't reach or isn't allowed to use, the program gives up with one of the messages below.

This is common on machines with no screen: remote servers, cron jobs and CI runners. It usually doesn't mean anything is broken. The machine simply has no graphical session for the program to use, so the fix is either to stop asking for a window or to forward one from your own computer.

Symptoms and causes

You are likely dealing with this problem if:

  • A GUI program (xclock, xeyes, a browser, a Tk or Qt app) exits immediately with Error: Can't open display:, cannot open display: or Gtk-WARNING: cannot open display.
  • echo $DISPLAY prints nothing after you log in over ssh.
  • A Python script that plots or uses Tk fails only on the server with _tkinter.TclError: no display name and no $DISPLAY environment variable.
  • X11 forwarding request failed on channel 0 appears when you connect, or a program prints No protocol specified.

The usual causes, most common first:

No graphical session at all
The machine is a headless server, cron job or CI job, and the program needs a display that doesn't exist.
SSH connection without X11 forwarding
The client didn't use -X or -Y (or ForwardX11 yes). The ssh man page says DISPLAY is set automatically only when forwarding is on and the local side has DISPLAY set.
The server refuses forwarding
xauth may be missing, or DisableForwarding set.
No X server on your own computer
ssh -X only carries the window to an X server running on the machine you sit at. If none is running there, there is nowhere to show it.
Access to the X server is denied
"No protocol specified" means the Xauthority cookie or the xhost list doesn't allow this client, for example when running as another user.
Python or matplotlib forced onto a GUI backend
The matplotlib docs say that on Linux it falls back to Agg when it can't reach an X or Wayland display. So the error usually means a GUI backend was forced through rcParams, MPLBACKEND or matplotlib.use(), or Tkinter is used directly.

How to fix it

Work through these in order. They start with the quickest, safest checks and move to deeper repairs; stop as soon as the problem is gone.

1.Run without a window (matplotlib and similar)

Easy

If the program only needs to produce a file, such as a plot image, it doesn't need a display at all.

  1. Run the script with the non-interactive Agg backend and save the figure to a file instead of calling plt.show().
    Terminal
    MPLBACKEND=Agg python script.py
  2. Or set the backend inside the script before any figure is created. The matplotlib docs warn that calling it too late may fail with ImportError.
    Text
    import matplotlib
    matplotlib.use("Agg")
  3. If this doesn't help, check for a conflicting setting. Precedence is rcParams, then MPLBACKEND, then matplotlib.use(), and the last one applied wins. The docs discourage exporting MPLBACKEND globally in .bashrc or .profile.

2.Turn on X11 forwarding for SSH

ModerateChanges settings

When you really need the window on your own screen, SSH can carry it for you and set DISPLAY automatically.

Before you start: Back up /etc/ssh/sshd_config before editing it, and keep a second session open while you restart sshd so a mistake doesn't lock you out. Use -Y only if -X breaks an app: per the ssh man page -X is subject to the X11 SECURITY extension restrictions and -Y is not.

  1. Make sure an X server is running on your own computer, the one you connect from.
  2. Connect with forwarding enabled.
    Terminal
    ssh -X user@host
  3. If it still fails, set X11Forwarding yes on the server in /etc/ssh/sshd_config. Also install xauth (the package name varies by distro).
    Config
    X11Forwarding yes
  4. Restart sshd, then reconnect.
    Terminalneeds sudo / root
    sudo systemctl restart sshd
  5. After login, check the variable. It should look like localhost:10.0. Don't set it by hand: the ssh man page says doing so makes the connection insecure.
    Terminal
    echo $DISPLAY

Check that it worked

  • echo $DISPLAY prints a value such as :0 or localhost:10.0 in the same shell where you run the program.
  • A simple test program such as xclock or xeyes opens a window without errors.
  • Your script runs to the end and writes its output file.

If it still fails

  • Run ssh -v -X user@host and read the X11 lines in the output to see where forwarding is refused.
  • Check the server's sshd configuration for X11Forwarding and DisableForwarding, and confirm xauth is installed.
  • Confirm you are running the program in the same session and user that has DISPLAY, not through cron or a fresh sudo that may not carry it over.
  • Do not turn access control off with a bare xhost +: the xhost man page says it lets everyone connect, and Xserver(1) says a program that can connect to a display has full run of the screen.
  • If a tool insists on a display even though nobody will watch it (some browsers and test runners), or you need a GUI inside a container, this article doesn't cover it. Check that tool's or container platform's own documentation.

Questions

Should I use ssh -Y instead of -X?

Try -X first. -Y is the trusted mode and is less secure, so use it only if -X makes an application misbehave.

Does this also affect Wayland desktops?

XWayland sets its own DISPLAY, but ssh -X still needs an X server on the client side. Wayland specifics weren't covered in the sources used here.

Sources

  1. Backends, Matplotlib 3.11.2 docsOfficial · Matplotlib
  2. ssh(1)Official · OpenBSD / OpenSSH
  3. sshd_config(5)Official · OpenBSD / OpenSSH
  4. xhost(1) man page (X11R7.6)Official · X.Org
  5. Xserver(1) man page (X11R7.6)Official · X.Org