xvfbwrapper is a Python library for managing X11 virtual displays
with Xvfb (X virtual framebuffer), a virtual X server that runs
without a physical display. It provides the Xvfb class, a Python interface for
configuring and controlling the Xvfb program.
See the module API documentation for more information.
- Copyright (c) 2012-2026 Corey Goldberg
- Development: GitHub
- Releases: PyPI
- License: MIT
| Latest Version |
|
| Tests (CI) |
|
| API Docs (CI) |
|
| Supported Python Versions |
|
Xvfb (X virtual framebuffer) is a display server implementing the X11 display
server protocol. It runs in memory and does not require a physical display or
input device. Only a network layer is necessary.
Xvfb allows GUI applications that use X Windows to run on a headless system.
Official releases are published on PyPI:
pip install xvfbwrapper
- Python 3.11+
- X Window System
- Xvfb (
sudo apt install xvfb,sudo dnf install xorg-x11-server-Xvfb, etc) - Support for file locking using
fcntl(POSIX systems)
Always ensure that the virtual display is stopped, even if an exception occurs.
Xvfb can be used as a context manager or managed explicitly with
try/finally.
from xvfbwrapper import Xvfb
with Xvfb():
# Runs inside the virtual display
main()The virtual display is automatically stopped when the context exits, including if an exception occurs.
from xvfbwrapper import Xvfb
xvfb = Xvfb()
xvfb.start()
try:
# Runs inside the virtual display
main()
finally:
xvfb.stop()Xvfb(width=1280, height=720)When running Xvfb in a Wayland session, GUI toolkits may try to use the
Wayland backend instead of connecting to Xvfb. Setting
set_xdg_session_type=True forces XDG_SESSION_TYPE=x11 in the Python process
and all child processes, ensuring that GUI apps use the X11 backend and can
render on the virtual display.
Xvfb(set_xdg_session_type=True)The Xvfb executable accepts several types of command line arguments.
The most common is an argument with a - prefix and a parameter (i.e.
-nolisten tcp). These can be added as keyword arguments when creating an
xvfbwrapper.Xvfb instance. For example:
Xvfb(nolisten="tcp")Xvfb will be called with the -nolisten tcp argument.
There are other possible types of arguments:
- unary argument (i.e.
ttyxx) - unary argument with a
+prefix (i.e.+xinerama) - unary argument with a
-prefix (i.e.-nocursor) - argument with a parameter (i.e.
c 100) - argument with a
+prefix and a parameter (i.e.+extension RANDR)
Any type of argument can be added as an extra_args sequence when creating an
xvfbwrapper.Xvfb instance. For example:
from xvfbwrapper import Xvfb
xvfb = Xvfb(extra_args=("ttyxx", "-nocursor", "+extension", "RANDR"))Xvfb will be called with the ttyxx -nocursor +extension RANDR arguments.
When running multiple Xvfb instances, provide each a separate
os.environ.copy() via the environ argument to prevent them from modifying
each other's environment variables.
import os
from xvfbwrapper import Xvfb
xvfb1 = Xvfb(environ=os.environ.copy())
xvfb2 = Xvfb(environ=os.environ.copy())
xvfb1.start()
xvfb2.start()
try:
# Use each Xvfb instance independently
...
finally:
xvfb1.stop()
xvfb2.stop()The following example uses selenium and xvfbwrapper to run
Chrome tests inside a virtual display.
import unittest
from selenium import webdriver
from xvfbwrapper import Xvfb
class TestPage(unittest.TestCase):
def setUp(self):
xvfb = Xvfb(set_xdg_session_type=True)
xvfb.start()
self.addCleanup(xvfb.stop)
self.driver = webdriver.Chrome()
self.addCleanup(self.driver.quit)
def test_selenium_homepage(self):
self.driver.get("https://selenium.dev")
self.assertIn("Selenium", self.driver.title)The same setup can be used with pytest by managing the virtual display and
browser through a fixture.
import pytest
from selenium import webdriver
from xvfbwrapper import Xvfb
@pytest.fixture
def driver():
with Xvfb(set_xdg_session_type=True):
driver = webdriver.Chrome()
yield driver
driver.quit()
def test_selenium_homepage(driver):
driver.get("https://selenium.dev")
assert "Selenium" in driver.title