Google collab is using Ubuntu 20.04 now and you can't install chromium-browser without snap. But you can install it by using .deb files for ubuntu 18.04 at security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/.
I made a python script for this purpose. It finds latest version of chromium-browser and chromedriver for 18.04 and installs it for your google colab which has Ubuntu 20.04.
Site's links have been updated regularly. You don't need debian repository and apt keys.
import os
import re
import subprocess
import requests
# The deb files we need to install
deb_files_startstwith = [
"chromium-codecs-ffmpeg-extra_",
"chromium-codecs-ffmpeg_",
"chromium-browser_",
"chromium-chromedriver_"
]
def get_latest_version() -> str:
# A request to security.ubuntu.com for getting latest version of chromium-browser
# e.g. "112.0.5615.49-0ubuntu0.18.04.1_amd64.deb"
url = "http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/"
r = requests.get(url)
if r.status_code != 200:
raise Exception("status_code code not 200!")
text = r.text
# Find latest version
pattern = '<a\shref="chromium\-browser_([^"]+.ubuntu0\.18\.04\.1_amd64\.deb)'
latest_version_search = re.search(pattern, text)
if latest_version_search:
latest_version = latest_version_search.group(1)
else:
raise Exception("Can not find latest version!")
return latest_version
def download(latest_version: str, quiet: bool):
deb_files = []
for deb_file in deb_files_startstwith:
deb_files.append(deb_file + latest_version)
for deb_file in deb_files:
url = f"http://security.ubuntu.com/ubuntu/pool/universe/c/chromium-browser/{deb_file}"
# Download deb file
if quiet:
command = f"wget -q -O /content/{deb_file} {url}"
else:
command = f"wget -O /content/{deb_file} {url}"
print(f"Downloading: {deb_file}")
# os.system(command)
!$command
# Install deb file
if quiet:
command = f"apt-get install /content/{deb_file} >> apt.log"
else:
command = f"apt-get install /content/{deb_file}"
print(f"Installing: {deb_file}\n")
# os.system(command)
!$command
# Delete deb file from disk
os.remove(f"/content/{deb_file}")
def check_chromium_installation():
try:
subprocess.call(["chromium-browser"])
print("Chromium installation successfull.")
except FileNotFoundError:
print("Chromium Installation Failed!")
def install_selenium_package(quiet: bool):
if quiet:
!pip install selenium -qq >> pip.log
else:
!pip install selenium
def main(quiet: bool):
# Get the latest version of chromium-browser for ubuntu 18.04
latest_version = get_latest_version()
# Download and install chromium-browser for ubuntu 20.04
download(latest_version, quiet)
# Check if installation succesfull
check_chromium_installation()
# Finally install selenium package
install_selenium_package(quiet)
if __name__ == '__main__':
quiet = True # verboseness of wget and apt
main(quiet)
And try selenium
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
wd = webdriver.Chrome('chromedriver', options=chrome_options)
wd.get("https://www.google.com")
print(f"Page title: {wd.title}")
Update 14.02.2024
Ubuntu 18.04 LTS has reached end of life(no more updates) and google colab updated to Ubuntu 22.04 LTS.
While searching for an alternative I found that linux mint has it's own chromium repo instead of snap. Linux Mint 21.3(Virginia) is based on Ubuntu 22.04 LTS and will get updates until 2027.
Since both of them ubuntu 22.04 LTS we can use linux mint's chromium deb file to install chromium on google colab. I tested and it is working.
I changed script to scrape chromium browser from Linux Mint packages site and also chromedriver from Chrome for Testing page.
import os
import shutil
import re
import subprocess
import urllib
import zipfile
import requests
"""
Scrapes and installs chromium from linux mint 21.3(virginia) packages site.
Link: http://packages.linuxmint.com/pool/upstream/c/chromium/
Scrapes and installs chromedriver from Chrome for Testing page.
Link: https://googlechromelabs.github.io/chrome-for-testing/
"""
class CantGetLatestChromiumVersionError(Exception):
"""Happens when regex failed"""
class ChromiumInstallationFailedException(Exception):
"""
Happens when deb package not installed
Check the downloaded chroumium deb file
"""
class CantGetChromeDriverError(Exception):
"""Happens when regex failed"""
main_url = "http://packages.linuxmint.com/pool/upstream/c/chromium/"
work_dir = "/content"
def get_chromium_latest_version() -> str:
# A request to packages.linuxmint.com for getting latest version of chromium
# e.g. "chromium_121.0.6167.160~linuxmint1+virginia_amd64.deb"
r = requests.get(main_url)
if r.status_code != 200:
raise Exception("status_code code not 200!")
text = r.text
# Find latest version
pattern = '<a\shref="(chromium_[^"]+linuxmint1%2Bvirginia_amd64.deb)'
latest_version_search = re.search(pattern, text)
if latest_version_search:
latest_version = latest_version_search.group(1)
else:
raise CantGetLatestChromiumVersionError("Failed to get latest chromium version!")
return latest_version
def install_chromium(latest_version: str, deb_file: str, quiet: bool):
# Full url of deb file
url = f"{main_url}{latest_version}"
# Download deb file
if quiet:
command = f"wget -q -O {work_dir}/{deb_file} {url}"
else:
command = f"wget -O {work_dir}/{deb_file} {url}"
print(f"Downloading: {deb_file}")
# os.system(command)
!$command
# Install deb file
if quiet:
command = f"apt-get install {work_dir}/{deb_file} >> apt.log"
else:
command = f"apt-get install {work_dir}/{deb_file}"
print(f"Installing: {deb_file}")
# os.system(command)
!$command
def check_chromium_installation(deb_file: str):
try:
subprocess.call(["chromium"])
print("Chromium installation successfull.\n")
# If installation successfull we can remove deb file
# Delete deb file from disk
os.remove(f"{work_dir}/{deb_file}")
except FileNotFoundError:
raise ChromiumInstallationFailedException("Chromium Installation Failed!")
def get_chromedriver_url(deb_file: str) -> str:
# Get content of crhomedriver page
url = "https://googlechromelabs.github.io/chrome-for-testing/"
r = requests.get(url)
if r.status_code != 200:
raise Exception("status_code code not 200!")
text = r.text
# Get chromium version from deb file's name
version_number = deb_file.split("chromium_")[-1].split(".")[0]
# Example: https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/121.0.6167.85/linux64/chromedriver-linux64.zip
pattern = f'https://[^<]+/{version_number}[^<]+/linux64/chromedriver-linux64.zip'
# Find latest version
chromedriver_url_search = re.search(pattern, text)
if chromedriver_url_search:
chromedriver_url = chromedriver_url_search.group()
return chromedriver_url
else:
raise CantGetChromeDriverError("Failed to get chromedriver!")
def install_chromedriver(deb_file: str, quiet: bool):
url = get_chromedriver_url(deb_file)
file_name = url.split("/")[-1]
# Download chromedriver
chromedriver_zip = f"{work_dir}/{file_name}"
if quiet:
command = f"wget -q -O {chromedriver_zip} {url}"
else:
command = f"wget -O {chromedriver_zip} {url}"
print(f"Downloading: {file_name}")
# os.system(command)
!$command
# Extract chromedriver from zip
with zipfile.ZipFile(chromedriver_zip) as zpf:
zpf.extract(member="chromedriver-linux64/chromedriver", path=work_dir)
# Remove chromedriver-linux64.zip file
os.remove(chromedriver_zip)
# Move extracted chromedriver binary file to /usr/bin directory
source = f"{work_dir}/chromedriver-linux64/chromedriver"
destination = "/usr/bin/chromedriver"
os.rename(source, destination)
# Make chromedriver binary executable
os.system(f"chmod +x {destination}")
# Remove empty chromedriver-linux64 folder
shutil.rmtree(f"{work_dir}/chromedriver-linux64")
print("Chromedriver installed")
def install_selenium_package(quiet: bool):
if quiet:
!pip install selenium -qq >> pip.log
else:
!pip install selenium
def main(quiet: bool):
# Get the latest version of chromium from linux mint packages site
latest_version = get_chromium_latest_version()
# Name of the deb file
deb_file = urllib.parse.unquote(latest_version, "utf-8")
# Download and install chromium for ubuntu 22.04
install_chromium(latest_version, deb_file, quiet)
# Check if installation succesfull
check_chromium_installation(deb_file)
# Install chromedriver
install_chromedriver(deb_file, quiet)
# Finally install selenium package
install_selenium_package(quiet)
if __name__ == '__main__':
quiet = True # verboseness of wget and apt
main(quiet)
To test it:
from selenium import webdriver
# Chromium browser options
options = webdriver.ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
# Start webdriver
driver = webdriver.Chrome(options=options)
# Go to https://www.google.com and print page title
driver.get("https://www.google.com")
print(f"Page title: {driver.title}")
driver.quit()