Matplotlib Error: no display name and no $DISPLAY environment variable (Headless Server)

Default interactive GUI backend (`TkAgg`) failed on a headless Linux VM or CI runner.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Matplotlib Error Platform: Python
Common Root Causes:
  • Default interactive GUI backend (`TkAgg`) failed on a headless Linux VM or CI runner.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Set `MPLBACKEND=Agg` or call `matplotlib.use("Agg")` before importing `pyplot`.
Safe Diagnostic Command:
echo "$MPLBACKEND" "$DISPLAY"
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Default interactive GUI backend (`TkAgg`) failed on a headless Linux VM or CI runner. Recommended diagnostic: `echo "$MPLBACKEND" "$DISPLAY"`. Remediation: Set `MPLBACKEND=Agg` or call `matplotlib.use("Agg")` before importing `pyplot`.

Worked Examples

  • Run `echo "$MPLBACKEND" "$DISPLAY"` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-py-matplotlib-no-display-tkinter · Independently verified: 2026-11-25

Related Resources