Run Ubuntu Headless for Browser Automation
In this article
Running Ubuntu in headless mode for browser automation means operating the OS without a graphical desktop, while still executing browsers like Chrome or Firefox through automation tools such as Selenium or Puppeteer. This approach saves system resources, allows remote execution on servers, and is ideal for continuous integration or scheduled tasks. You'll need a machine with Ubuntu installed (server or desktop), SSH access, and basic command-line familiarity.
What You'll Need
- Ubuntu 20.04 or newer (64-bit)
- Root or sudo access
- SSH client (e.g., OpenSSH) for remote access
- Internet connection to install packages
- Optional: a separate user account for running automation (recommended for security)
Step-by-Step
- Set up a headless environment – If you installed Ubuntu Server, you already have no GUI. For Ubuntu Desktop, you can disable the graphical interface by running
sudo systemctl set-default multi-user.targetand rebooting. This boots to a text console, saving memory and CPU. - Update system packages – Run
sudo apt update && sudo apt upgrade -yto ensure all software is current. - Install necessary dependencies – For browser automation, you'll need curl, wget, and some libraries. Run
sudo apt install -y curl wget unzip xvfb. Xvfb (X virtual framebuffer) provides a virtual display for browsers that need one. - Install a browser – Choose Chrome or Firefox. For Chrome, download the .deb package:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debthen install withsudo apt install ./google-chrome-stable_current_amd64.deb. For Firefox, usesudo apt install firefox. - Install a driver – Selenium needs a driver to communicate with the browser. For Chrome, download ChromeDriver from the official site matching your Chrome version. For Firefox, install geckodriver via
sudo apt install firefox-geckodriver(or download manually). Place the driver in /usr/local/bin and make it executable. - Install automation libraries – Depending on your language: Python users can run
pip install seleniumorpip install playwright. Node.js users can runnpm install selenium-webdriverornpm install puppeteer. Ensure Python pip is installed:sudo apt install python3-pip. - Set up a virtual display – Even headless browsers often require a display. Use Xvfb to create a virtual screen: start it with
Xvfb :99 -screen 0 1920x1080x24 &and set the DISPLAY environment variable:export DISPLAY=:99. Alternatively, use the browser's built-in headless mode (e.g., Chrome's--headlessflag) to avoid Xvfb entirely. - Write and execute a test script – Create a simple Python script to verify everything works. Example: navigate to example.com, get the title, and print it. Run it with
python3 test.py. If it outputs the page title, your setup is functional. - Run as a service (optional) – For persistent automation, create a systemd service to start your script on boot. Write a unit file in /etc/systemd/system/ and enable it with
sudo systemctl enable myautomation.service.
Common Mistakes
- Forgetting to install all browser dependencies – Chrome and Firefox require specific libraries like libnss3, libx11-xcb1, etc. Missing them causes crashes. Use
sudo apt --fix-broken installor install thechromium-browserpackage which pulls dependencies. - Using the wrong driver version – ChromeDriver must match the exact Chrome version. Check with
google-chrome --versionand download accordingly. - Not setting DISPLAY when using Xvfb – If you don't export DISPLAY, the browser fails to start. Always set it in the same terminal or script.
- Running as root – Browsers often refuse to run as root without the
--no-sandboxflag. Instead, create a dedicated user:sudo useradd -m automationand switch to it. - Ignoring headless flags – Using
--headlessin Chrome orheadless=Truein Selenium avoids needing Xvfb, but many tutorials forget to mention it, leading to confusion.
Tips
- Use a virtual environment for Python to keep dependencies isolated.
- For production, consider Docker containers with pre-built images like
selenium/standalone-chrometo avoid manual setup. - Add
--disable-gputo Chrome flags to prevent GPU-related errors on servers. - Use
xvfb-runcommand to wrap your script:xvfb-run -a python3 test.py– it automatically starts a virtual display. - Set memory limits for browsers in your script to avoid crashes on low-RAM servers.
Troubleshooting
Browser won't start – Check the error message. Common issues: missing libraries (install with apt --fix-broken install), wrong permissions (run as non-root user), or missing DISPLAY (use Xvfb or headless mode).
ChromeDriver version mismatch – Download the exact version from the ChromeDriver site and replace the old binary. Verify with chromedriver --version.
Automation script times out – Increase timeouts in your code, or check if the site is blocking automated requests. Some sites require user-agent spoofing or disabling automation flags.
Xvfb not found – Install it with sudo apt install xvfb. If it's already installed, check the path with which Xvfb.
Permission denied when writing files – Ensure the user running automation has write permissions to the working directory. Use sudo chown -R automation:automation /path/to/dir.
Frequently Asked Questions
Do I need a GUI at all for headless browser automation?
No. Modern browsers have headless modes that run without a display. Xvfb is only needed if you're using an older browser or a tool that requires a full display.
Can I use this on a virtual private server (VPS)?
Yes, this setup is commonly used on VPS instances. Just ensure your VPS has enough RAM (at least 1GB for basic tasks) and CPU resources.
Which browser is better for headless automation: Chrome or Firefox?
Both work well. Chrome is more widely used and has better performance in headless mode, but Firefox is more privacy-oriented. Choose based on your project's compatibility.
How do I take screenshots in headless mode?
In Selenium, use driver.save_screenshot('file.png') or element.screenshot(). In Puppeteer, use page.screenshot({path: 'file.png'}).
Is it possible to run multiple browsers simultaneously?
Yes, but each instance consumes memory. Use separate user data directories or run them in separate Docker containers to avoid conflicts.
What should I do if the browser is detected as a bot?
Some websites use anti-bot measures. You can try setting a realistic user-agent, disabling the 'webdriver' flag, or using undetected-chromedriver. However, always respect the website's terms of service.