Skip to content

Instantly share code, notes, and snippets.

@cgoldberg
Last active August 18, 2023 10:20
Show Gist options
  • Save cgoldberg/4097efbfeb40adf698a7d05e75e0ff51 to your computer and use it in GitHub Desktop.
Save cgoldberg/4097efbfeb40adf698a7d05e75e0ff51 to your computer and use it in GitHub Desktop.
download and install latest geckodriver for linux or mac (selenium webdriver)
#!/bin/bash
# download and install latest geckodriver for linux or mac.
# required for selenium to drive a firefox browser.
install_dir="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
if [[ $(uname) == "Darwin" ]]; then
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("macos"))')
elif [[ $(uname) == "Linux" ]]; then
url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
else
echo "can't determine OS"
exit 1
fi
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$install_dir"
echo "installed geckodriver binary in $install_dir"
@yogesh-desai
Copy link

Thank you. 👍

@diemol
Copy link

diemol commented Oct 9, 2017

Hi, thanks for this script! Very helpful.

I copied and used it for Chromedriver as well, here it is:

#!/bin/bash
# download and install latest chromedriver for linux or mac.
# required for selenium to drive a Chrome browser.

install_dir="/usr/local/bin"
version=$(wget -qO- https://chromedriver.storage.googleapis.com/LATEST_RELEASE)
if [[ $(uname) == "Darwin" ]]; then
    url=https://chromedriver.storage.googleapis.com/$version/chromedriver_mac64.zip
elif [[ $(uname) == "Linux" ]]; then
    url=https://chromedriver.storage.googleapis.com/$version/chromedriver_linux64.zip
else
    echo "can't determine OS"
    exit 1
fi
curl -s -L "$url" | tar -xz
chmod +x chromedriver
sudo mv chromedriver "$install_dir"
echo "installed chromedriver binary in $install_dir"

I'll create a gist with both and quote you as the source, thanks!

EDIT: Here is the gist https://gist.github.com/diemol/635f450672b5bf80420d595ca0016d20

@zhisme
Copy link

zhisme commented Feb 2, 2018

Before I could run this script, I had to install jq.
brew install jq
macos. System version.

Darwin MacBook-zh.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:21:16 PDT 2017; 
root:xnu-3789.60.24~6/RELEASE_X86_64 x86_64

FYI:
read article

@ORESoftware
Copy link

@cgoldberg

put set -e; at the top of your bash scripts

@maxpeterson
Copy link

You could also use Python:

url=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest | python -c "import sys, json; print(next(item['browser_download_url'] for item in json.load(sys.stdin)['assets'] if 'linux64' in item.get('browser_download_url', '')))")

@Jambon1510
Copy link

Jambon1510 commented Jun 23, 2018

This is not working for me, url return is empty

uname 
Linux

bash -x ./geckodriver-install.sh
.......
+ url=
+ tar -xz
+ curl -s -L ''

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
+ chmod +x geckodriver
chmod: cannot access 'geckodriver': No such file or directory
+ sudo mv geckodriver /usr/local/bin
mv: cannot stat 'geckodriver': No such file or directory
+ echo 'installed geckodriver binary in /usr/local/bin'
installed geckodriver binary in /usr/local/bin

@1ycx
Copy link

1ycx commented Jul 20, 2018

Working in Ubuntu 18.04.
sudo apt install jq was necessary.

@Jambon1510 :

This is not working for me, url return is empty.

Well, you might be on a 32-bit machine. Try changing linux64 in this line :
10 : url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
to linux32.

@manosnoam
Copy link

Without using jq command, you can simply grep for the file address:

geckodriver_gz=$(curl https://api.github.com/repos/mozilla/geckodriver/releases/latest \
    | grep -Eoh 'https.*linux64\.tar\.gz')
curl -sL "$geckodriver_gz" | tar -xz --checkpoint=.10

(The tar checkpoint option is to print dots progress)

@manosnoam
Copy link

Hi, thanks for this script! Very helpful.

I copied and used it for Chromedriver as well

For chrome driver, it is a ZIP not GZ archive, so you can use JAR command to extract:
curl -L $chromedriver_zip_url | jar xv

@BrooksCunningham
Copy link

Thanks for sharing!

@jlegido
Copy link

jlegido commented May 8, 2020

First of all many thanks to all people that has contributed to this script.

@cgoldberg in case that you want to include couple of lines to install jq dependency here it is the code (I can't submit a Pull Request in gist).

Thanks.

@brunoao86
Copy link

Thanks for the contribution, guys! ✌️


I got this error on linux:

gzip: stdin: unexpected end of file
tar: Child returned status 1
tar: Error is not recoverable: exiting now
chmod: cannot access 'geckodriver': No such file or directory
mv: cannot stat 'geckodriver': No such file or directory

I realized the the value of $url was returning 2 urls:

https://github.com/mozilla/geckodriver/releases/download/v0.27.0/geckodriver-v0.27.0-linux64.tar.gz https://github.com/mozilla/geckodriver/releases/download/v0.27.0/geckodriver-v0.27.0-linux64.tar.gz.asc

So I splitted the $url and got the first one in the script below using url=$(echo $url | cut -f1 -d" ").

#!/bin/bash
# download and install latest geckodriver for linux or mac.
# required for selenium to drive a firefox browser.

install_dir="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
if [[ $(uname) == "Darwin" ]]; then
    url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("macos"))')
elif [[ $(uname) == "Linux" ]]; then
    url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64"))')
else
    echo "can't determine OS"
    exit 1
fi
url=$(echo $url | cut -f1 -d" ") # new line
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$install_dir"
echo "installed geckodriver binary in $install_dir"

I hope it helps someone. 🚀

@miken
Copy link

miken commented Sep 6, 2020

I had trouble with the original script - @brunoao86's script works.

@ktnoc
Copy link

ktnoc commented Jan 4, 2021

import json
import requests
import platform
import sys
import tarfile
import urllib
import os
latest_release = requests.get("https://api.github.com/repos/mozilla/geckodriver/releases/latest")
host_version = "linux"+platform.architecture()[0][:2]
for version in latest_release.json()['assets']:
    if host_version in version['name'] and ".asc" not in version['name']:
        local_filename, headers = urllib.request.urlretrieve(version['browser_download_url'])
        tarfile.open(local_filename).extractall(os.path.dirname(sys.executable))
        urllib.request.urlcleanup()

installs on virtualenv of the executed script

@MichaelCurrin
Copy link

@DonFruendo
Copy link

A different version of the script modification provided by @brunoao86, that I use is to utilize jq further and dropping the asc urls right in the search. See below for the full script

#!/bin/bash
# download and install latest geckodriver for linux or mac.
# required for selenium to drive a firefox browser.

install_dir="/usr/local/bin"
json=$(curl -s https://api.github.com/repos/mozilla/geckodriver/releases/latest)
if [[ $(uname) == "Darwin" ]]; then
    url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("macos")) | select(contains("asc") | not)')
elif [[ $(uname) == "Linux" ]]; then
    url=$(echo "$json" | jq -r '.assets[].browser_download_url | select(contains("linux64")) | select(contains("asc") | not)')
else
    echo "can't determine OS"
    exit 1
fi
curl -s -L "$url" | tar -xz
chmod +x geckodriver
sudo mv geckodriver "$install_dir"
echo "installed geckodriver binary in $install_dir"

@geoashis
Copy link

Will the latest version of geckodriver driver work with the latest version of firefox-esr at any point in time?

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