User Agents

Web browsers and everyday web tech, explained

Run Ubuntu Headless for Browser Automation

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. 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.target and rebooting. This boots to a text console, saving memory and CPU.
  2. Update system packages – Run sudo apt update && sudo apt upgrade -y to ensure all software is current.
  3. 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.
  4. 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.deb then install with sudo apt install ./google-chrome-stable_current_amd64.deb. For Firefox, use sudo apt install firefox.
  5. 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.
  6. Install automation libraries – Depending on your language: Python users can run pip install selenium or pip install playwright. Node.js users can run npm install selenium-webdriver or npm install puppeteer. Ensure Python pip is installed: sudo apt install python3-pip.
  7. 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 --headless flag) to avoid Xvfb entirely.
  8. 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.
  9. 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 install or install the chromium-browser package which pulls dependencies.
  • Using the wrong driver version – ChromeDriver must match the exact Chrome version. Check with google-chrome --version and 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-sandbox flag. Instead, create a dedicated user: sudo useradd -m automation and switch to it.
  • Ignoring headless flags – Using --headless in Chrome or headless=True in 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-chrome to avoid manual setup.
  • Add --disable-gpu to Chrome flags to prevent GPU-related errors on servers.
  • Use xvfb-run command 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.