Xvfbwrapper
xvfbwrapper is a Python library for controlling X11 virtual displays with Xvfb (X virtual framebuffer). Xvfb is a display server implementing the X11 protocol that runs entirely in memory, without requiring a physical screen or input devices. This enables GUI applications designed for X Windows to execute on headless systems, which is particularly useful for automated testing. The current version is 0.2.23, and it is actively maintained.
Warnings
- gotcha The `Xvfb` binary must be installed on your system. `xvfbwrapper` is a Python wrapper, not a complete Xvfb distribution. Failure to install the system-level `Xvfb` will result in runtime errors.
- gotcha Always use `Xvfb` as a context manager (`with Xvfb(): ...`) or ensure `Xvfb().start()` is paired with `Xvfb().stop()` in a `try/finally` block. Failing to stop the display can leave behind temporary lock files in `/tmp`, leading to resource leaks and potential issues with future Xvfb sessions.
- gotcha When running GUI toolkits in a Wayland session, they may default to the Wayland backend instead of connecting to Xvfb. This can lead to applications not rendering correctly on the virtual display.
- gotcha Prior to version 0.2.20 (released early 2025), if the `DISPLAY` environment variable was set to an empty string, `xvfbwrapper` could fail with an exception. This issue has since been resolved.
Install
-
pip install xvfbwrapper
Imports
- Xvfb
from xvfbwrapper import Xvfb
Quickstart
import os
from xvfbwrapper import Xvfb
# Ensure Xvfb is available on your system (e.g., sudo apt-get install xvfb)
# Using Xvfb as a context manager is recommended for safe cleanup
with Xvfb(width=1280, height=720, colordepth=24):
print("Xvfb virtual display started.")
print(f"DISPLAY environment variable is: {os.environ.get('DISPLAY')}")
# Your application that needs a display runs here
# Example: launch a headless browser or GUI application
# The display will be stopped automatically when exiting the 'with' block
print("Application finished, Xvfb will stop.")
print("Xvfb virtual display stopped.")