Install Chrome Driver on Ubuntu 18.04 for Python

Recently I have been playing around with Amazon’s EC2 Instances. Most of my virtual machines are running Ubuntu Server 18.04. One of my projects use chromedriver to automate a few processes. I was having trouble installing and running chromedriver. I hope this helps those with similar problems.

Installation

Note that although we are installing for chromium (open-source project behind Google Chrome) it will still work

  1. Update package lists
    sudo apt-get update
    
  2. Install Chromium Chromedriver
    sudo apt-get install chromium-chromedriver
    
  3. Check Installation Destination (Optional)
    whereis chromedriver
    Expected Return: /usr/lib/chromium-browser/chromedriver
    

Usage

If running on a server you must use the following options

  1. Initalize Chromedriver Options
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    
  2. Initalize Chromedriver
     driver= webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", options=options)
    

Testing

Using a slightly altered sample test from chromedriver’s website (with our options), we can test our installation.

import time
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=options) # Custom path and added options parameter
driver.get('https://williamakirschner.com);

time.sleep(5)
print(driver.current_url) # Print the current URL
time.sleep(5)

driver.quit()