Docker Firefox Puppeteer Issue: How to Fix 'no DISPLAY environment variable' Error

I encounter a ‘no DISPLAY environment variable specified’ error using Puppeteer with Firefox in Docker despite setting HEADLESS and Xvfb. How can I resolve this problem?

I’ve dealt with this exact problem in my Docker setup. What finally worked for me was using the ‘firefox-esr’ package instead of regular Firefox. It’s more stable in headless environments. Also, make sure you’re setting the correct environment variables in your Dockerfile:

ENV DISPLAY=:99
ENV MOZ_HEADLESS=1

Then, in your entrypoint script, start Xvfb before your main application:

Xvfb :99 -ac &
sleep 1

These changes resolved the DISPLAY issue for me. If you’re still having trouble, check your Puppeteer configuration. Sometimes specifying the Firefox binary path explicitly can help:

const browser = await puppeteer.launch({
product: ‘firefox’,
executablePath: ‘/usr/bin/firefox-esr’
});

Hope this helps you get past this frustrating error!

I’ve encountered this issue before when working with Puppeteer and Firefox in Docker. One solution that worked for me was to explicitly set the DISPLAY environment variable in your Dockerfile or docker-compose file. Try adding ‘ENV DISPLAY=:99’ to your configuration. Additionally, ensure you’re running Xvfb correctly before starting your application. You might need to add a command like ‘Xvfb :99 -ac &’ in your startup script. If these steps don’t resolve the issue, consider using a pre-configured Docker image specifically designed for running browsers in headless environments. They often have these settings pre-configured, saving you some troubleshooting time.

hey, had similar issue. try adding --no-sandbox flag to ur puppeteer launch options. also make sure ur using latest firefox version in docker image. if that doesnt work, check ur xvfb setup - might need to tweak display settings. good luck!

Using Puppeteer with Firefox on Ubuntu 22.04

Thanks so much for this thread!
I’ve looked everywhere on the internet until I finally found this.

Here’s the complete guide to getting Puppeteer + Firefox running on Ubuntu 22:


Step 1: Prevent Chrome Download

Set the environment variable before installing Puppeteer:

ENV PUPPETEER_SKIP_DOWNLOAD=1

Step 2: Install Firefox for Puppeteer

Use pnpm to install the desired Firefox version for Puppeteer:

pnpm puppeteer browsers install firefox@esr
# Optionally specify a version:
# pnpm puppeteer browsers install firefox@esr_128.9.0esr

Step 3: Install Required System Libraries

Install the necessary dependencies:

sudo apt-get install xvfb pciutils libegl1-mesa --no-install-recommends

Step 4: Start Virtual Display (Xvfb)

To run headless browsers that need a display:

Xvfb :99 -ac &
export DISPLAY=:99

Step 5: Test It Manually

Verify that Firefox is working in headless mode:

/path/to/puppeteer/firefox/linux-esr_128.9.0esr/firefox/firefox

It should launch silently (no user interface visible).


Step 6: Configure Puppeteer with Firefox

Use the following setup in your Puppeteer script:

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch({
  browser: 'firefox',
  executablePath: '/home/app/puppeteer/firefox/linux-esr_128.9.0esr/firefox/firefox',
  args: ['--no-sandbox'],
});

All done! Puppeteer should now be using Firefox successfully on Ubuntu 22.
Test it on https://get.webgl.org/