Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install Chrome, ChromeDriver and Selenium on Ubuntu 16.04
#!/usr/bin/env bash
# https://developers.supportbee.com/blog/setting-up-cucumber-to-run-with-Chrome-on-Linux/
# https://gist.github.com/curtismcmullan/7be1a8c1c841a9d8db2c
# http://stackoverflow.com/questions/10792403/how-do-i-get-chrome-working-with-selenium-using-php-webdriver
# http://stackoverflow.com/questions/26133486/how-to-specify-binary-path-for-remote-chromedriver-in-codeception
# http://stackoverflow.com/questions/40262682/how-to-run-selenium-3-x-with-chrome-driver-through-terminal
# http://askubuntu.com/questions/760085/how-do-you-install-google-chrome-on-ubuntu-16-04
# Versions
CHROME_DRIVER_VERSION=`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`
SELENIUM_STANDALONE_VERSION=3.4.0
SELENIUM_SUBDIR=$(echo "$SELENIUM_STANDALONE_VERSION" | cut -d"." -f-2)
# Remove existing downloads and binaries so we can start from scratch.
sudo apt-get remove google-chrome-stable
rm ~/selenium-server-standalone-*.jar
rm ~/chromedriver_linux64.zip
sudo rm /usr/local/bin/chromedriver
sudo rm /usr/local/bin/selenium-server-standalone.jar
# Install dependencies.
sudo apt-get update
sudo apt-get install -y unzip openjdk-8-jre-headless xvfb libxi6 libgconf-2-4
# Install Chrome.
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
sudo apt-get -y update
sudo apt-get -y install google-chrome-stable
# Install ChromeDriver.
wget -N http://chromedriver.storage.googleapis.com/$CHROME_DRIVER_VERSION/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip
sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver
# Install Selenium.
wget -N http://selenium-release.storage.googleapis.com/$SELENIUM_SUBDIR/selenium-server-standalone-$SELENIUM_STANDALONE_VERSION.jar -P ~/
sudo mv -f ~/selenium-server-standalone-$SELENIUM_STANDALONE_VERSION.jar /usr/local/bin/selenium-server-standalone.jar
sudo chown root:root /usr/local/bin/selenium-server-standalone.jar
sudo chmod 0755 /usr/local/bin/selenium-server-standalone.jar
#!/usr/bin/env bash
# Run Chrome via Selenium Server
start-chrome() {
xvfb-run java -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone.jar
}
start-chrome-debug() {
xvfb-run java -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone.jar -debug
}
# Run Chrome Headless
start-chrome-headless() {
chromedriver --url-base=/wd/hub
}
# Start
# start-chrome
# start-chrome-debug
# start-chrome-headless
@jeremyrader

This comment has been minimized.

Copy link

jeremyrader commented Apr 27, 2017

Dude, you are a lifesaver! Thank you!

@aolave

This comment has been minimized.

Copy link

aolave commented May 12, 2017

Thank you!

Taking into consider the following permission:

chown root:root /usr/local/share/chromedriver

@reense

This comment has been minimized.

Copy link

reense commented May 14, 2017

Thank you so much!

I didn't have unzip installed. So i ran sudo apt-get install unzip. For everyone running into problems. :)

@mikaelkundert

This comment has been minimized.

Copy link

mikaelkundert commented May 24, 2017

Thanks! I used different version of chromedriver and selenium and I noticed that the -debug option wasn't working on my setup, so I left it out.

@partinder

This comment has been minimized.

Copy link

partinder commented Jul 3, 2017

Sweet!

@jeniaefimov

This comment has been minimized.

Copy link

jeniaefimov commented Jul 4, 2017

Thank you guy, so much!

@ralexrong

This comment has been minimized.

Copy link

ralexrong commented Jul 10, 2017

thank you

@mangena-dave

This comment has been minimized.

Copy link

mangena-dave commented Jul 17, 2017

defiantly a Lifesaver!

@sanasa

This comment has been minimized.

Copy link

sanasa commented Jul 17, 2017

Thank youuu !!!

@derryberni

This comment has been minimized.

Copy link

derryberni commented Jul 19, 2017

Thank youuu

@545314690

This comment has been minimized.

Copy link

545314690 commented Jul 20, 2017

thank you

@ghost

This comment has been minimized.

Copy link

ghost commented Jul 25, 2017

thank you

@ofonimefrancis

This comment has been minimized.

Copy link

ofonimefrancis commented Jul 26, 2017

Thank you, i keep coming back for this

@sarikabagga7

This comment has been minimized.

Copy link

sarikabagga7 commented Aug 15, 2017

Hello, Thanks for sharing. I have added shared code in my project but I am facing below error;

Exception: unknown error: Chrome failed to start: exited normally
  (Driver info: chromedriver=2.20.353124 (035346203162d32c80f1dce587c8154a1efa0c3b),platform=Linux 4.4.0-89-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.04 seconds
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'a39f04d0b59d', ip: '172.17.0.4', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-89-generic', java.version: '1.8.0_131'

@Xosmond

This comment has been minimized.

Copy link

Xosmond commented Aug 31, 2017

Thank you so muuch

@rohit12sh

This comment has been minimized.

Copy link

rohit12sh commented Sep 3, 2017

Thanks!!

@HoangJerry

This comment has been minimized.

Copy link

HoangJerry commented Sep 6, 2017

I am beginer, please noted me how to do step by step.

@onassar

This comment has been minimized.

Copy link

onassar commented Sep 9, 2017

Anyone tested this on Ubuntu 14.04.5 LTS? Tried and no luck. Getting:
./start-chrome.sh: line 5: xvfb-run: command not found

@sunilkhuwal

This comment has been minimized.

Copy link

sunilkhuwal commented Sep 19, 2017

any alternative to wget command when executing the shell on windows OS machine

@mangena-dave

This comment has been minimized.

Copy link

mangena-dave commented Sep 28, 2017

@sarikabagga7
For unknown reasons it seems like your script is complaining with your current ChromeDriver 2.30
Uninstall the current ChromeDriver and # install latest version of ChromeDriver, in my case is 2.32.

@werdlv

This comment has been minimized.

Copy link

werdlv commented Sep 29, 2017

I tried this using php-webdriver but I get following error:

PHP Fatal error:  Uncaught Facebook\WebDriver\Exception\WebDriverCurlException: Curl error thrown for http POST to /session with params: {"desiredCapabilities":{"browserName":"chrome","platform":"ANY","chromeOptions":{"binary":""}}}

Operation timed out after 30000 milliseconds with 0 bytes received in .../vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:286
Stack trace:
#0 /.../vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php(126): Facebook\WebDriver\Remote\HttpCommandExecutor->execute(Object(Facebook\WebDriver\Remote\WebDriverCommand))
#1 /.../test.php(16): Facebook\WebDriver\Remote\RemoteWebDriver::create('http://localhos...', Object(Facebook\WebDriver\Remote\DesiredCapabilities))
#2 {main}
  thrown in /.../vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php on line 286

It seems that for some reason Chrome is unreachable. What could be the issue?

@MorrisLaw

This comment has been minimized.

Copy link

MorrisLaw commented Oct 2, 2017

This works beautifully, thank you!

@abhijity

This comment has been minimized.

Copy link

abhijity commented Oct 6, 2017

It works fine for me with chrome 61.0.3163.100, chromedriver 2.33 and selenium server standalone 2.53.1. Thank you..

@ttruong15

This comment has been minimized.

Copy link

ttruong15 commented Oct 10, 2017

thank you, works beautifully. I been trying getting this things to work for the last couple days now. Pulling my hair out. You are a life saver. Thankyou, Thankyou and Thankyou.

@mamunrashid001

This comment has been minimized.

Copy link

mamunrashid001 commented Oct 13, 2017

worked like a charm!

@dmitryck

This comment has been minimized.

Copy link

dmitryck commented Oct 16, 2017

thanx!

@mamontovdmitriy

This comment has been minimized.

Copy link

mamontovdmitriy commented Oct 21, 2017

Nice job, thank you!

I would added for start-chrome.sh
-Dwebdriver.chrome.whitelistedIps="localhost,127.0.0.1,192.168.*.*"

@robywan27

This comment has been minimized.

Copy link

robywan27 commented Oct 25, 2017

Thanks a lot!

@rifaterdemsahin

This comment has been minimized.

Copy link

rifaterdemsahin commented Nov 1, 2017

Testing with Chrome Driver and getting these errors

Message: Test method SeleniumGridSmokeTest.UnitTest1.TestMethod1 threw exception: System.InvalidOperationException: Unable to create new service: ChromeDriverService Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:40.131Z' System info: host: 'seleniumnode', ip: '10.0.0.9', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-97-generic', java.version: '1.8.0_151' Driver info: driver.version: unknown (InsecureCertificate)

@nobodyme

This comment has been minimized.

Copy link

nobodyme commented Nov 6, 2017

wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ~/
sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
sudo apt-get -f install -y
sudo dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb

Why is that line, 'dpkg -i --force-depends' repeated twice? Can someone explain? Thanks.

@markusguenther

This comment has been minimized.

Copy link

markusguenther commented Nov 6, 2017

@nobodyme Also ask me that ;)

@Alqama

This comment has been minimized.

Copy link

Alqama commented Nov 11, 2017

Thanks, Man!! That was really helpful. (y)

@andreasneuber

This comment has been minimized.

Copy link

andreasneuber commented Nov 20, 2017

Thanks a lot ziadoz!
Actually this gist solves two challenges for me: 1) Install Chrome driver fast, and 2) How to run Codeception acceptance tests headless via xvfb :-)

Just some minor things: I noticed:
xvfb-run java -Dwebdriver.chrome.driver=/usr/local/bin/chromedriver -jar /usr/local/bin/selenium-server-standalone.jar runs better for me with additional flag -a (after xvfb-run)

And Google Chrome install works better for me if line 27 looks like this:
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'

@matthewmuscat

This comment has been minimized.

Copy link

matthewmuscat commented Nov 21, 2017

Where exactly do past the code for the start_chrome.sh file?

@trungtnm

This comment has been minimized.

Copy link

trungtnm commented Nov 29, 2017

@matthewmuscat : you can call it by typing
source start-chrome.sh; start-chrome

@alsilva86

This comment has been minimized.

Copy link

alsilva86 commented Dec 12, 2017

Dude you are the best! Still working on ubuntu 16.04

@Divyapabba16

This comment has been minimized.

Copy link

Divyapabba16 commented Dec 13, 2017

Hi i followed your code and getting the below error, how can we fix it. please help me in resolving the issue

Starting ChromeDriver 2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5) on port 11458
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 4.10.0-28-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.10 seconds

@Divyapabba16

This comment has been minimized.

Copy link

Divyapabba16 commented Dec 13, 2017

@mangena-dave I tried chromedriver with 2.32, 2.33, 2.29, 2.34 and many more still getting the same error, as below

Starting ChromeDriver 2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5) on port 11458
Only local connections are allowed.
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 4.10.0-28-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 60.10 seconds

@shakhal

This comment has been minimized.

Copy link

shakhal commented Dec 26, 2017

Thanks for sharing! saved me!

@hadeelsharaf

This comment has been minimized.

Copy link

hadeelsharaf commented Jan 16, 2018

Thanks,that is very helpful.

@larry852

This comment has been minimized.

Copy link

larry852 commented Jan 31, 2018

Thanks

@andreasneuber

This comment has been minimized.

Copy link

andreasneuber commented Feb 3, 2018

If unknown error: Chrome failed to start: exited abnormally check if adding arg --no-sandbox to chrome options makes a difference.
Helped me at least.

capabilities:
 chromeOptions:
   args: [ "disable-infobars", "--no-sandbox" ]
@ORESoftware

This comment has been minimized.

Copy link

ORESoftware commented Feb 10, 2018

this worked for me (need to update the download link):

set -e;

wget -N https://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip -P ~/
unzip ~/chromedriver_linux64.zip -d ~/
rm ~/chromedriver_linux64.zip

sudo mv -f ~/chromedriver /usr/local/bin/chromedriver
sudo chown root:root /usr/local/bin/chromedriver
sudo chmod 0755 /usr/local/bin/chromedriver
@ksair

This comment has been minimized.

@aderusha

This comment has been minimized.

Copy link

aderusha commented Mar 17, 2018

You're calling sudo in the wrong place on a couple of lines:

sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
should read:
curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add

sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
should read:
echo "deb http://dl.google.com/linux/chrome/deb/ stable main" | sudo tee -a /etc/apt/sources.list.d/google-chrome.list

@jtajtajta

This comment has been minimized.

Copy link

jtajtajta commented Mar 17, 2018

Thank you! Didn't work out as easy as for most others, but extremely helpful in succeeding with installation.

  1. I have a fresh Ubuntu 16.04 installation and curl was not installed. So, at first,initiating the CHROME_DRIVER_VERSION variable would not work. Installed curl and that part was cured.
  2. Everything went smoothly until installation of Chrome driver and Selenium. It was necessary to download them manually, as the script was not able to do that. Opening the http connection was successful, but finding the Chrome and Selenium versions resulted in 404 error.
  3. However, manually downloading, unpacking , installing, moving around and granting rights was easy to do with these instructions. Great work, much appreciated!

EDIT:
After some testing things are not looking good. Firstly, you should mention that in addition to the installation script, Selenium needs to be installed within the python versions that you intend to use, i.e.
pip install selenium
pip3 install selenium

And while having a brand new Ubuntu 16.04 installation, its necessary to install pip, before you can run the two lines above
sudo apt-get install python-pip
sudo apt-get install python3-pip

And yet, after days of trial and error, there is some prognosis, but its still not working. Running following commands in python3:
from selenium import webdriver
driver = webdriver.Chrome('/usr/local/bin/chromedriver')
The second row reports an error after some 30
"......
File "/home/me/.local/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
(Driver info: chromedriver=2.37.543619 (a237acd3116cac3b3f0da42a000502ce3fafcb23),platform=Linux 4.13.0-37-generic x86_64)"

Clearly, Chrome cannot be found. I installed it using the above script, so obviously I'm missing something that I sholud fix in my environment.

EDIT --- SOLVED!!!
Well, as it so often happens you make some changes and suddenly everything works. I actually tried to open Chrome from the X and failed. It would not open...!!! So I thought that if I have Chromium open, then maybe Chrome doesn't start. I closed Chromium and started Chrome, with success.

Then, running the script worked like charm! Funnily, after that I tried and found out that I CAN have Chromium open, and still get the script working and Chrome opening, just like it should. So, I am not sure why it started working, but am really glad that it did.

@pwfcurry

This comment has been minimized.

Copy link

pwfcurry commented Mar 27, 2018

You can also use yarn/npm to install (and update) the driver -
sudo yarn global add chromedriver --prefix /usr/bin
Run to install, and re-run to update when necessary.

@nshores

This comment has been minimized.

Copy link

nshores commented Apr 10, 2018

EDIT 2 - I have created a docker image to automate the creation of an environment for this, and I've already started to build some notifications around it. Find it here if you want a easier way to get an environment up and running for this great library --

https://github.com/nshores/my_usps_notifications

EDIT I have resolved this. By default, getting a session with myusps.get_session will use PhantomJS. This behavior works in Windows, but not linux. You must manually specificfy Chrome as the webdriver to use in linux - IE myusps.get_session(username, password, driver='chrome') See issue here - happyleavesaoc/python-myusps#14

Major issues getting this running on Ubuntu LTS 16.04. Getting a timeout when trying to login.

raceback (most recent call last):
  File "/home/nshores/.local/lib/python3.5/site-packages/myusps/__init__.py", line 180, in _login
    WebDriverWait(driver, LOGIN_TIMEOUT).until(EC.title_is(WELCOME_TITLE))
  File "/home/nshores/.local/lib/python3.5/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nshores/.local/lib/python3.5/site-packages/myusps/__init__.py", line 300, in get_session
    _login(session)
  File "/home/nshores/.local/lib/python3.5/site-packages/myusps/__init__.py", line 182, in _login
    raise USPSError('login failed')
myusps.USPSError: login failed

Tried a bunch of combinations of ChromeDrive, Google Chrome, and Selenium. My environment is --

Chrome - 65.0.333325.181
ChromeDriver -- 2.37
Latest Myusps Script
Python 3.5.2

I'm able to run this fine in windows. Just can't seem to narrow down the issue in Linux. Any ideas? This is very frustrating as I'm trying to work on getting a Docker image together for this project, as well as working on some notification modules to extend it out to Slack, etc.

@Manoj-nathwani

This comment has been minimized.

Copy link

Manoj-nathwani commented Apr 16, 2018

This is great! Thank you 🙏

@xbacon29x

This comment has been minimized.

Copy link

xbacon29x commented Apr 17, 2018

I already update my webdriver but when I run my script it always prompt this error.

WebDriverException: Message: disconnected: unable to connect to renderer
(Session info: chrome=65.0.3325.181)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Linux 4.13.0-37-generic x86_64)

@lijun003

This comment has been minimized.

Copy link

lijun003 commented May 7, 2018

Wonderful job. Thanks!

@pranaybiswas2611

This comment has been minimized.

Copy link

pranaybiswas2611 commented May 23, 2018

Lifesaver :)

@Gravellent

This comment has been minimized.

Copy link

Gravellent commented Jun 21, 2018

Thanks so much! Lifesaver

@pawel-wasiliuk

This comment has been minimized.

Copy link

pawel-wasiliuk commented Jun 29, 2018

Very helpful, thank you!

@justinas-kazanavicius

This comment has been minimized.

Copy link

justinas-kazanavicius commented Jul 16, 2018

Thanks!

@DerekChia

This comment has been minimized.

Copy link

DerekChia commented Jul 22, 2018

To those who got the following error:

Package google-chrome-stable is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'google-chrome-stable' has no installation candidate

try this:

apt install chromium-browser

@KamalQ

This comment has been minimized.

Copy link

KamalQ commented Aug 2, 2018

Life Saver !!

@merlinxcy

This comment has been minimized.

Copy link

merlinxcy commented Aug 8, 2018

Thanks!

@Luc45

This comment has been minimized.

Copy link

Luc45 commented Oct 29, 2018

To those who got the following error:

Package google-chrome-stable is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'google-chrome-stable' has no installation candidate

try this:

apt install chromium-browser

That helped! Thanks!

@mbaev

This comment has been minimized.

Copy link

mbaev commented Dec 5, 2018

$ curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
gpg: can't open `': No such file or directory

Should be

$ curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
OK
@arfurs

This comment has been minimized.

Copy link

arfurs commented Jan 7, 2019

Oh, my God!!
Thank you so much!!!

@Skillvendor

This comment has been minimized.

Copy link

Skillvendor commented Jan 21, 2019

Line 26 could be improved to:

sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add .
Sudo apt-key add.
@Skillvendor

This comment has been minimized.

Copy link

Skillvendor commented Jan 21, 2019

Line 27 might also not work:
sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list

What happens with the command is that echo is run as root, but not >>.

You could do it in two steps:

sudo -i

sudo echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list

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