ChromeDriver in WSL2
Notice: this guide will soon be (or already is) obsolete
Microsoft is rolling out built-in GUI support in WSL. These steps will still work, but will hopefully be unnecessary.
Original guide is as follows.
In WSL1 you could download chromedriver.exe for Windows, put it somewhere on your Path, and then let ChromeDriver and Chrome run in Windows while Selenium (or what have you) ran in Linux. This doesn’t work in WSL2 anymore, since localhost on the WSL side does not point to Windows; the outside Windows world has its own IP address. Chrome doesn’t like this at all.
Fortunately, we can run Chrome entirely from the WSL using an X server.
I’m using Debian for this, Ubuntu should work the same. This requires WSL2; it will not remotely work in the original WSL.
Installing Chrome
You need to do this even if you have Chrome installed in Windows already.
Dependencies:
sudo apt-get update
sudo apt-get install -y curl unzip xvfb libxi6 libgconf-2-4
Chrome itself:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
Ensure it worked:
google-chrome --version
Installing ChromeDriver
Find the URL of the ChromeDriver version that matches your Chrome version on the ChromeDriver website. It should be a zip file; in my case it’s https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_linux64.zip
.
Download, unzip, and put it in your bin directory:
wget https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
Double check it worked:
chromedriver --version
If you had previously installed ChromeDriver in Windows and were using it in WSL from the Path, make sure you aren’t pointing to that one anymore:
which chromedriver # should be /usr/bin/chromedriver
The X Server
Download and install VcXsrv in Windows. Once installed, run xlaunch.exe
(from the VcXsrv folder in Program Files). You can leave most of the settings as default, but make sure to check “Disable access control”. Allow it through the firewall if prompted when you first run it.
In Linux the DISPLAY
environment variable tells GUI applications at which IP address the X Server is that we want to use. Since in WSL2 the IP address of Windows land is not localhost anymore, we need to set DISPLAY
to the correct IP address:
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0
I recommend you put this in your .bashrc
or whatever the equivalent is for your distro.
Now if you run echo $DISPLAY
you should get something like 172.17.35.177:0.0
.
All done
If you run google-chrome
the Linux-side Chrome should open inside an X server window in Windows! This will also “just work” when ChromeDriver tries to open Chrome when you run your automated tests.
Troubleshooting
Unable to open X display
If you see something like the following when running Chrome:
Error: /etc/machine-id contains 0 characters (32 were expected).
Unable to open X display
- Make sure VcXsrv is running in Windows with “Disable access control” checked.
- Make sure VcXsrv is whitelisted in the Windows firewall (search the start menu for “Allow a program through Windows firewall” and enable it there)