Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# Bash script to setup headless Selenium (uses Xvfb and Chrome)
# (Tested on Ubuntu 12.04) trying on ubuntu server 14.04
# Add Google Chrome's repo to sources.list
echo "deb stable main" | sudo tee -a /etc/apt/sources.list
# Install Google's public key used for signing packages (e.g. Chrome)
# (Source:
wget -q -O - | sudo apt-key add -
# Update apt sources:
sudo apt-get update
# Install Python, pip, Selenium:
#sudo apt-get -y install python python-pip
#sudo pip install selenium
different way to get chromedriver and selenium java jar:
sudo apt-get -y install default-jre # if not installed yet
sudo npm install protractor -g
sudo webdriver-manager update
sudo ln /usr/lib/node_modules/protractor/selenium/chromedriver /usr/bin/chromedriver
#sudo apt-get -y install unzip
# Download/Install chromedriver
# (
# For x86:
#wget -c
# For x86-64:
#wget -c
# Neither creating symbolic link nor adding dir to $PATH worked for me
# in a Vagrant VM. So, let's blatantly copy the binary:
#sudo cp ./chromedriver /usr/bin/
#sudo chmod ugo+rx /usr/bin/chromedriver
# Install Google Chrome:
sudo apt-get -y install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4
sudo apt-get -y install google-chrome-stable
# Dependencies to make "headless" chrome/selenium work:
sudo apt-get -y install xvfb gtk2-engines-pixbuf
sudo apt-get -y install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable
# Optional but nifty: For capturing screenshots of Xvfb display:
sudo apt-get -y install imagemagick x11-apps
# Make sure that Xvfb starts everytime the box/vm is booted:
echo "Starting X virtual framebuffer (Xvfb) in background..."
Xvfb -ac :99 -screen 0 1280x1024x16 &
export DISPLAY=:99
# Optionally, capture screenshots using the command:
#xwd -root -display :99 | convert xwd:- screenshot.png
Copy link

praphull27 commented Jun 10, 2015

Is this working for Ubuntu Server 14.04?

Copy link

praphull27 commented Jun 11, 2015

This script works perfect on Ubuntu 14.04.

Copy link

telemakhos commented Jul 4, 2015

What's up with the comments? I guess we can use this as reference at least...

Copy link

marcelaraujo commented Aug 19, 2015

It is working on Ubuntu 15.04

Copy link

tsl0922 commented Aug 26, 2015

Working for me on Ubuntu 14.04! I've installed ttf-wqy-microhei to display chinese words.

Install x11vnc and run x11vnc --listen -rfbport 5900 -display :99 -forever, then you can use vnc clients to connect to xvfb display.

Copy link

stuart-leitch commented Sep 23, 2015

Needs node ( & npm) installed as pre-reqs for protractor.

Line 19 is missing a comment char.

Copy link

Stephen-Williams commented Feb 5, 2016

Thank you so much for this script!
I've been banging my head against a wall for DAYS trying to get a headless protractor/selenium set up that works (ubuntu 15.04 wily, btw) and this script did the trick! To be honest, looking at these commands, I'm still not sure which part I was missing. Feels like I tried all this before, but maybe not in the right order?
Anyway, you're a life-saver. Thanks a ton!

Copy link

luckily commented Feb 23, 2016

thanks a lot !!!!!

Copy link

joviano-dias commented Jun 28, 2016

as @leitchst points out, some things are missing

sudo: npm: command not found
sudo: webdriver-manager: command not found

Copy link

nousacademy commented Jul 1, 2016

how do you view the html, once you get it running?? Whats the difference in running this vs phantomjs. I run Xvfb -ac :99 -screen 0 1280x1024x16 & export DISPLAY=:99 but how and where is it running so I can see it?

Copy link

DanielGGordon commented Nov 16, 2016

So how can this be integrated with my existing Java code? Will it just work automatically now and use headless mode every time I start my java app? I'm using selenium chromedriver.

Copy link

aijanai commented Jan 13, 2017

A little note for "webdriver-manager start" users:
the X display needs to be set for the webdriver server, since this is the one launching Chrome, not for the script that executes the tests.

This is particularly critical for Jenkins users, which may have selenium launched in a separate job and the tests launched in another.

Copy link

bradisbell commented Mar 9, 2017

@nousacademy This is headless, so the whole point is that you don't see it. If you need to debug, you can use VNC to spin up a quick server:

x11vnc --listen -rfbport 5900 -display :99

Then, connect to VNC on localhost with any VNC client.

Copy link

HarishMungaram commented Apr 3, 2017

I have Jenkins install on master and selenium stuff on a slave, after installing chrome, xvfb, chromedriver and dependencies to make "headless" chrome/selenium work[install on salve] when i build the script i am having " org.openqa.selenium.NoSuchSessionException: no such session " and Capabilities [{message=unknown error: Chrome failed to start: exited abnormally

I have not install selenium through.
#sudo apt-get -y install python python-pip
#sudo pip install selenium
No idea why i'm getting this error.
Pls help

Copy link

evserykh commented Jan 18, 2018

Here is working Dockerfile:

FROM ruby:2.5                                                                                                              
ENV DEBIAN_FRONTEND noninteractive                                                                                         
ENV CONTAINER_LOCALE en_US                                                                                                 
RUN apt-get update && \                                                                                                    
    apt-get install -y locales                                                                                             
RUN sed -i -e "s/# ${CONTAINER_LOCALE}.UTF-8 UTF-8/${CONTAINER_LOCALE}.UTF-8 UTF-8/" /etc/locale.gen && \                  
    dpkg-reconfigure --frontend=noninteractive locales && \                                                                
    update-locale LANG=${CONTAINER_LOCALE}.UTF-8                                                                           
ENV LANG ${CONTAINER_LOCALE}.UTF-8                                                                                         
RUN curl -sL | bash - && \                                                            
    apt-get install -y --no-install-recommends nodejs                                                                      
RUN curl -sS | apt-key add - && \                                                 
    echo "deb stable main" | tee /etc/apt/sources.list.d/yarn.list && \                     
    apt-get update && \                                                                                                    
    apt-get install -y --no-install-recommends yarn                                                                        
RUN echo "deb stable main" | tee -a /etc/apt/sources.list && \                      
    curl -sS | apt-key add - && \                                    
    apt-get update && \                                                                                                    
    apt-get -y --no-install-recommends install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4 && \                   
    apt-get -y --no-install-recommends install google-chrome-stable                                                        
RUN apt-get -y --no-install-recommends install xvfb gtk2-engines-pixbuf && \                                               
    apt-get -y --no-install-recommends install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable && \
    apt-get -y --no-install-recommends install imagemagick x11-apps                                                        
RUN apt-get -y --no-install-recommends install default-jre && \                                                            
    npm install protractor -g && \                                                                                         
    webdriver-manager update 2.35 --gecko false && \                                                    
    ln /usr/lib/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.35 /usr/bin/chromedriver 
RUN touch /usr/bin/docker-entrypoint && { \                                                                                
      echo '#!/bin/bash'; \                                                                                                
      echo 'echo "Starting X virtual framebuffer (Xvfb) in background..."'; \                                              
      echo 'Xvfb -ac :99 -screen 0 1280x1024x16 &'; \                                                                      
      echo 'export DISPLAY=:99'; \                                                                                         
      echo 'bash -c "$@"'; \                                                                                               
    } > /usr/bin/docker-entrypoint && \                                                                                    
    chmod +x /usr/bin/docker-entrypoint                                                                                    
ENTRYPOINT ["docker-entrypoint"]                                                                                           
CMD ["bash"] 

Here is working inside the container ruby script:

require 'selenium-webdriver'                                                                                               
# Selenium::WebDriver.logger.level = :debug                                                                                
options =                                                                         
driver = Selenium::WebDriver.for :chrome, options: options                                                                 
driver.manage.window.resize_to(1280, 1024)                                                                         ""                                                                                    
driver.save_screenshot "image.png"

Copy link

josuehenrique commented May 15, 2018

tnks a lot guy... you save my life

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment