Skip to content

Instantly share code, notes, and snippets.

@derat
Created April 13, 2022 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derat/2d77b5f42842eff0501a06c4789ab37d to your computer and use it in GitHub Desktop.
Save derat/2d77b5f42842eff0501a06c4789ab37d to your computer and use it in GitHub Desktop.
Linux shell script to install the current version of ChromeDriver
#!/bin/sh -e
# This implements https://chromedriver.chromium.org/downloads/version-selection
# to install the latest version of ChromeDriver that's compatible with the
# current google-chrome binary.
# Location where the chromedriver executable will be installed.
# Change this line if you'd like to install it somewhere else.
# This is a symlink; version-suffixed binaries live alongside it.
destlink=${HOME}/local/bin/chromedriver
# Check that we'll be able to run the linux64 version of ChromeDriver.
hwname=$(uname -m)
if [ "$hwname" != x86_64 ]; then
echo "Unsupported hardware name '${hwname}' from 'uname -m'"
exit 1
fi
# google-chrome --version prints a line like "Google Chrome 96.0.4664.93 " (yes,
# with a trailing space): https://www.chromium.org/developers/version-numbers/
# To get the latest corresponding Chrome driver release, we need
# "MAJOR.MINOR.BUILD", e.g. "96.0.4664".
cver=$(google-chrome --version)
build=$(echo "$cver" | sed -nre 's/^Google Chrome ([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+\s*$/\1/p')
if [ -z "$build" ]; then
echo "Failed parsing Chrome version '${cver}'"
exit 1
fi
# Get the file containing the latest compatible ChromeDriver release number.
rel=$(wget --quiet -O- "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_${build}")
echo "Need ChromeDriver ${rel} for Chrome ${cver}"
# Download the appropriate version if we don't already have it.
destfile=${destlink}.${rel}
if [ ! -e "$destfile" ]; then
url=https://chromedriver.storage.googleapis.com/${rel}/chromedriver_linux64.zip
tmpdir=$(mktemp --directory -t chromedriver.XXXXXXXXXX)
zipfile=${tmpdir}/$(basename "$url")
echo "Downloading ${url} to ${tmpdir}"
wget --quiet -O "$zipfile" "$url"
echo "Extracting executable to ${destfile}"
unzip -q "$zipfile" chromedriver -d "$tmpdir"
mv "${tmpdir}/chromedriver" "$destfile"
chmod 0755 "$destfile"
rm -rf "$tmpdir"
fi
# If there's already a regular file in place of the link, move it away.
if [ -e "$destlink" ] && [ ! -L "$destlink" ]; then
echo "Moving ${destlink} to ${destlink}.old"
mv "$destlink" "${destlink}.old"
fi
ln -sf "$(basename "$destfile")" "$destlink"
echo "ChromeDriver ${rel} installed to ${destlink}"
@derat
Copy link
Author

derat commented Nov 13, 2023

This is outdated. You're better off using the version at https://codeberg.org/derat/nup/src/branch/main/build/install_chromedriver.sh.

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