---
slug: "ディスプレイが無いLinuxで、Firefox+SeleniumをPython上で起動して自動操作する"
title: "Running Firefox with Selenium on Headless Linux for Automated Operations in Python"
description: "\n\n\nThis is a method to launch Selenium from Python on a headless Linux system."
url: "https://www.ytyng.com/en/blog/ディスプレイが無いLinuxで、Firefox+SeleniumをPython上で起動して自動操作する"
publish_date: "2015-08-11T02:06:09Z"
created: "2015-08-11T02:06:09Z"
updated: "2026-02-26T23:17:20.056Z"
categories: ["Python"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/14461c5cd86f4a388423a6181eb0e5ce.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Running Firefox with Selenium on Headless Linux for Automated Operations in Python

<div class="document">


<p>This is a method to launch Selenium from Python on a headless Linux system.</p>
<p>Prerequisites</p>
<pre class="literal-block">$ sudo apt-get install xvfb

$ pip install selenium

$ pip install pyvirtualdisplay
</pre>
<p>Instead of a physical display, we use a virtual framebuffer called xvfb.</p>
<p>Pyvirtualdisplay makes it easy to use xvfb from Python.</p>
<p>Python code</p>
<pre class="literal-block">from pyvirtualdisplay import Display
from selenium import webdriver

display = Display(visible=0, size=(800, 600))
display.start()

fx = webdriver.Firefox()
fx.get("http://example.com")
fx.save_screenshot("/tmp/webdriver-firefox-screenshot.png")

fx.close()
fx.quit()
display.stop()
</pre>
<p>We open <a class="reference external" href="http://example.com">http://example.com</a> with Firefox and save a screenshot.</p>
<p>If the termination process is not handled properly, xvfb and Firefox might become zombie processes. In such cases, you can terminate them using killall.</p>
<p>Check if the processes are still running</p>
<pre class="literal-block">$ ps -eafw|grep -i "firefox"
$ ps -eafw|grep "xvfb"
</pre>
<p>Moreover, if you want to develop with a display on a Mac and run without a display on Linux, you can use</p>
<pre class="literal-block">import platform
use_virtual_display = platform.system() == 'Linux'
</pre>
<p>By comparing with platform.system(), you can achieve this relatively easily.</p>
</div>
