Headless plotting and Wayland applications on Linux

On modern Linux installations, a program that saves plots to disk usually does not need a graphical desktop or display server. Matplotlib can render files with its non-interactive Agg backend.

Save plots without a display

Select Agg before importing pyplot, then save the figure:

import matplotlib
matplotlib.use("Agg")

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.savefig("plot.png")

Alternatively, select the backend when launching a script:

MPLBACKEND=Agg python plot.py

This is sufficient when the only graphical output needed is a saved image. No graphical desktop session is required.

Run an application that requires Wayland

Some GUI applications require a Wayland compositor even when no physical display is available. Install Weston, the reference Wayland compositor:

sudo apt install weston

Start its headless backend in one terminal:

weston --backend=headless --socket=wayland-headless

In another terminal running as the same user, launch the application with that socket:

WAYLAND_DISPLAY=wayland-headless application

Weston uses XDG_RUNTIME_DIR for its Wayland socket. Log-in sessions normally set this variable; if it is unset, use a properly owned, private runtime directory before starting Weston. Stop Weston with Ctrl+C when finished.

For applications that only need to save plots, prefer the application’s non-interactive plotting backend instead of starting a compositor. See the Matplotlib backend documentation and Weston’s running guide.