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.
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:orGtk-WARNING: cannot open display. echo $DISPLAYprints nothing after you log in overssh.- 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 0appears when you connect, or a program printsNo 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
-Xor-Y(orForwardX11 yes). The ssh man page saysDISPLAYis set automatically only when forwarding is on and the local side hasDISPLAYset. - The server refuses forwarding
xauthmay be missing, orDisableForwardingset.- No X server on your own computer
ssh -Xonly 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,
MPLBACKENDormatplotlib.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)
If the program only needs to produce a file, such as a plot image, it doesn't need a display at all.
- Run the script with the non-interactive Agg backend and save the figure to a file instead of calling
plt.show().MPLBACKEND=Agg python script.py - 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.
import matplotlib matplotlib.use("Agg") - If this doesn't help, check for a conflicting setting. Precedence is rcParams, then
MPLBACKEND, thenmatplotlib.use(), and the last one applied wins. The docs discourage exportingMPLBACKENDglobally in.bashrcor.profile.
2.Turn on X11 forwarding for SSH
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.
- Make sure an X server is running on your own computer, the one you connect from.
- Connect with forwarding enabled.
ssh -X user@host - If it still fails, set
X11Forwarding yeson the server in/etc/ssh/sshd_config. Also installxauth(the package name varies by distro).X11Forwarding yes - Restart sshd, then reconnect.
sudo systemctl restart sshd - 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.echo $DISPLAY
Check that it worked
echo $DISPLAYprints a value such as:0orlocalhost:10.0in the same shell where you run the program.- A simple test program such as
xclockorxeyesopens a window without errors. - Your script runs to the end and writes its output file.
If it still fails
- Run
ssh -v -X user@hostand read the X11 lines in the output to see where forwarding is refused. - Check the server's sshd configuration for
X11ForwardingandDisableForwarding, and confirmxauthis installed. - Confirm you are running the program in the same session and user that has
DISPLAY, not through cron or a freshsudothat 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
- Backends, Matplotlib 3.11.2 docsOfficial · Matplotlib
- ssh(1)Official · OpenBSD / OpenSSH
- sshd_config(5)Official · OpenBSD / OpenSSH
- xhost(1) man page (X11R7.6)Official · X.Org
- Xserver(1) man page (X11R7.6)Official · X.Org