chintu
chintu

Reputation: 51

Unable to install chromium using snap on WSL

I ran the command /usr/bin/chromium-browser it mentioned installing chromium using snap
When I did that, it threw a new error. The following is the output of the command snap version.

snap 2.51.1+20.04ubuntu2
snapd unavailable
series -

chromium-browser

Command '/usr/bin/chromium-browser' requires the chromium snap to be installed. Please install it with:

snap install chromium

snap install chromium error: cannot communicate with server: Post http://localhost/v2/snaps/chromium: dial unix /run/snapd.socket: connect: no such file or directory

whereis chromedriver
chromedriver: /usr/bin/chromedriver /usr/local/bin/chromedriver

I ran into all these because I was trying to test out a simple selenium program:

import requests
import unittest

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get("https://www.python.org")
print(driver.title)

Which threw the following error:

Traceback (most recent call last):
  File "test_ui.py", line 7, in <module>
    driver = webdriver.Chrome('/usr/bin/chromedriver')
  File "/home/chintu/anaconda3/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "/home/chintu/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/chintu/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/chintu/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/chintu/anaconda3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
  (Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.10.16.3-microsoft-standard-WSL2 x86_64)

Not sure if these problems are coming because I am using WSL on windows-10

Upvotes: 1

Views: 16254

Answers (1)

NotTheDr01ds
NotTheDr01ds

Reputation: 20887

Not sure if these problems are coming because I am using WSL on Windows 10

Well, yes. While WSL is quite capable, it still has a few limitations that need to be understood in order to make full use of it. The primary one that you are running into at the moment is that WSL does not support Systemd out-of-the-box.

The second one that you haven't run into yet, but will, is that WSL on Windows 10 does not support GUI apps out of the box.

The second part is easiest to overcome for your particular use. Just run the browser in headless mode so that it doesn't try to output to a display. The code example for doing this is in this answer, and I have tested it out on a Windows 10 WSL/Ubuntu system.

Note that this isn't an issue under Windows 11, since the WSLg feature allows you to run GUI applications. It's also possible to overcome with other methods as I cover in this Super User answer. I've also run Selenium through xrdp without issues.

The first problem is a bit trickier. On Ubuntu, Chromium is installed via Snap, which pretty much requires Systemd. Systemd, unfortunately, insists on running as the main process (PID 1), or it just won't work. WSL needs its own PID1 ('/init') in order to provide its interoperability with Windows.

You still do have multiple alternatives, however. I'm going to list these in my order of preference:

Option 1: Use Google Chrome, rather than Chromium

I was able to run your sample code (with the modifications for headless) under Ubuntu using the official Google Chrome Debian package and the corresponding webdriver.

This is, by far, the easiest solution.

Option 2: Use a distribution that doesn't require Snap in order to install Chromium

I was also successful doing this using my custom Artix distribution, but I believe Arch Linux would work as well since, AFAIK, they use the same Chromium package.

As an added bonus, the chromium package also included chromedriver with no need to find a matching version to install separately.

On the downside, you'll need to manually wsl --import an Artix or Arch rootfs based on this Microsoft guide and learn how to configure it.

Option 3: Get Systemd running on WSL/Ubuntu

While I won't go through all the steps here, there are a number of Systemd helpers out there for WSL, including Genie, distrod, and others.

These all work the same way at their core, by using creating a new Linux PID namespace where Systemd is PID1, then putting you inside that namespace. I don't recommend it for new users, really. There are quite a few things that are changed on your WSL system, and it's best that you have a deep understanding of Systemd before using it with WSL, IMHO.

But, it's there as an option. I believe you can install Snaps with this in place, but I haven't tried it personally. On Windows 10, however, you might still have issues installing the Chromium snap due to a lack of display, and you'll need to resolve this separately if so.

Upvotes: 9

Related Questions