Skip to content

Instantly share code, notes, and snippets.

@pjobson
Last active March 8, 2024 08:08
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save pjobson/6b9fb926c59f58aa73d4efa10fe13654 to your computer and use it in GitHub Desktop.
Save pjobson/6b9fb926c59f58aa73d4efa10fe13654 to your computer and use it in GitHub Desktop.
Setting Up Cypress on Ubuntu WSL2

Setting Up Cypress on Ubuntu WSL2

Deprecated

Per @samali0996 's comment below: https://gist.github.com/pjobson/6b9fb926c59f58aa73d4efa10fe13654?permalink_comment_id=4487157#gistcomment-4487157

The new WSL Win10/11 should support this natevly. Thanks for posting the update!

Support

Edit: I no longer have access to a Windows dev environment and don't really have much interest in setting one up, you can reply, but I can't offer you any support on any of this. I recommend reading the Deprecatoin notice and going from there.

License

As this is becoming more popular, I am officially licensing it as Public Domain, please do whatever you want with it. Sell it, modify it, steal it... I'm not concerned.

https://en.wikipedia.org/wiki/Public_domain

Notes

No clue why the directions for this are buried deep in WLS issues. This is how I setup my Cypress on Ubuntu WSL2 working for both on and off VPN with some corp firewall.

Before executing any command, be sure to read and understand it, I ask you to run two shell scripts which I wrote. You should review any shell script provided by anyone before blindly running it. I'm not doing anyting bad, but I advise you to check for yourself.

Setup WSL2 & Install Linux

This is outside of the scope of this document, read this doc for more info: https://pureinfotech.com/install-windows-subsystem-linux-2-windows-10/

Dependencies

This is for Ubuntu, it should be similar for other distros.

sudo apt install libgtk2.0-0 libgtk-3-0 libgbm-dev libnotify-dev \
     libgconf-2-4 libnss3 libxss1 libasound2 libxtst6 xauth xvfb \
     x11-apps build-essential ca-certificates libcurl3-gnutls    \
     libcurl4 libcurl4-openssl-dev
mkdir ~/bin
echo "export PATH=~/bin:\$PATH" >> ~/.bashrc

Install Certs

Inside a firewall sometimes it is a PITA to get it to recognize certifications. I made a script to add the certs from github domains. If you're not behind a firewall and you can route to github hosts, then you do not need this.

cd ~/bin
wget --no-check-certificate https://gist.githubusercontent.com/pjobson/c7983da02ddb81c2c7457775111a4266/raw/90d23250c3466b7a39a76c2082591bd4880b7f5d/install_certs.sh
chmod +x install_certs.sh
sudo ./install_certs.sh

Install Node

This uses tj/n which is what I like. You can use nvm or install from binary or source, those are outside of the scope of this document.

curl -k -L https://git.io/n-install | bash
source ~/.bashrc
n lts
node --version
npm --version

Install X-server

Download and install vcxsrv: https://sourceforge.net/projects/vcxsrv/

Setup DISPLAY Environmental Variable

If you're unfamiliar with what this is, I recommend reading this article: https://askubuntu.com/questions/432255/what-is-the-display-environment-variable

WSL uses its own virtual network and has a different IP than Windows, so you need to tell WSL to use your Windows IP address and display.

In WSL, run:

netsh.exe interface show interface

Mine returns this at home while on the VPN:

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled        Connected      Dedicated        Ethernet 3
Disabled       Disconnected   Dedicated        Ethernet 2
Enabled        Connected      Dedicated        Wi-Fi
Enabled        Connected      Dedicated        vEthernet (WSL)

Whereas at work off the VPN it returns:

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Disabled       Disconnected   Dedicated        Ethernet 3
Disabled       Disconnected   Dedicated        Ethernet 2
Enabled        Connected      Dedicated        Wi-Fi
Enabled        Disconnected   Dedicated        Ethernet
Enabled        Connected      Dedicated        vEthernet (WSL)

In this example, my VPN connection is Ethernet 3 where as my non-VPN connection is Wi-Fi. In my case I need to use the IP for the VPN while at home and the IP from the Wi-Fi at work. I'm not 100% sure why this is, but I dislike Windows, so whatever.

I wrote a script which will output the display based on VPN or Default.

cd ~/bin
wget --no-check-certificate https://gist.githubusercontent.com/pjobson/3c5c23edd8e49cef7816372f4931f564/raw/8121f630571c8391f031d378f9381590ecbda003/getdisplay.sh
chmod +x getdisplay.sh
~/bin/getdisplay.sh 'VPN Interface' 'Default Interface'

Note: If you're not on a VPN and just to use a single interface, you can just pass it twice. The script is written for my specific use case, so it will always require two.

~/bin/getdisplay.sh 'Default Interface' 'Default Interface'

Of course change the VPN Interface and Default Interface to whatever yours are.

Mine for example is:

~/bin/getdisplay.sh 'Ethernet 3' 'Wi-Fi'

This should output something like this. If it returns nothing, you probably are using the wrong interfaces.

10.10.10.10:0.0

Edit .bashrc, add the following, again making the interfaces your own.

# set DISPLAY variable to the IP automatically assigned to WSL2
export DISPLAY="$(~/bin/getdisplay.sh 'VPN Interface' 'Default Interface')"
sudo /etc/init.d/dbus start &> /dev/null

Edit dbus file

sudo vi /etc/sudoers.d/dbus

Add:

<your username> ALL = (root) NOPASSWD: /etc/init.d/dbus

Source your bashrc

source ~/.bashrc
echo $DISPLAY

This should again display something like:

10.10.10.10:0.0

Start X Server

Open XLaunch from start menu.

Config:

  • Select: Multiple Windows (default)
  • Select: Start no client (default)
  • Check: Disable access control
  • Uncheck: Native opengl
  • Save configuration on startup folder, location: %AppData%\Microsoft\Windows\Start Menu\Programs\Startup

Windows Firewall

Start -> Run

 	%windir%\system32\WF.msc

Right-Click Inbound Rules -> New Rule...

  • Rule Type: Program
  • Program -> This Program Path: %ProgramFiles%\VcXsrv\vcxsrv.exe
  • Action: Allow the Connection
  • Profile: Check all
  • Name: VcXsrv

Test It

Just run one of the x11-apps installed at the top.

xeyes

Should open the xeyes application.

Install Cypress

General:

cd code_path
npm init

If you're NOT behind a firewall:

npm install cypress --save-dev

If you are behind a firewall, you need to get the zip file and point NPM to use it.

# Version -------------------------------------------------VVVVV
wget --no-check-certificate https://cdn.cypress.io/desktop/9.5.3/linux-x64/cypress.zip -O ~/cypress.zip
CYPRESS_INSTALL_BINARY=~/cypress.zip npm install cypress --save-dev

If you get a certificate error, disable strict-ssl in npm.

npm config set strict-ssl false

Then start it.

./node_modules/.bin/cypress open

Frequently I'll temporarily add the path to it to my PATH.

export PATH=./node_modules/.bin/:$PATH

cypress open

AWS EC2 Notes

I wanted to add a note that you can forward traffic from your AWS EC2 instance through WSL to the X Server in Windows.

In your WSL create a ~/.ssh/config file and add:

Host <AWS_HOST_IP_ADDRESS>
        HostName     <AWS_HOST_IP_ADDRESS>
        User         <AWS_HOST_USERNAME>
        IdentityFile ~/.ssh/<AWS_HOST_PEM_FILE>
        Port         22
        ForwardX11   yes

Host *
        IdentitiesOnly=yes
        PreferredAuthentications=publickey
        Compression yes
        ServerAliveInterval 30
        ServerAliveCountMax 1200
        LogLevel INFO

Then you can ssh to your EC2 host inside WSL. From your EC2 install xorg-x11-apps and run xeyes, this should open that app within your Windows x-server. You should be able to run cypress headed from your EC2 and have it open in Windows.

@isomorpheric
Copy link

This should be added to the cypress docs - shouldn't be this hard to add cypress to WSL and its a pretty big to entry for anyone programming on windows.

@pjobson
Copy link
Author

pjobson commented Jun 30, 2022

Thanks for the complement @ecam900 my doc is still kind of a mess.

@codemonkeynumber24
Copy link

Thank you very much, this worked for me! Although I had some issues as my WSL is in German - I took the liberty of translating your script, it wouldn't run without these changes in a German terminal.

https://gist.githubusercontent.com/codemonkeynumber24/b6ef32c8435bac0f803e260ec15a3c1c/raw/0c89542ded112a1258ef25a94f4b837c15e30f00/getdisplay.sh

@isomorpheric
Copy link

@pjobson well that mess came very much in handy 👍 Thanks, again.

I forked the gist and the script 🍴 !

@pjobson
Copy link
Author

pjobson commented Jul 11, 2022

@codemonkeynumber24 / @ecam900 Thanks for the positive feedback. I'm glad others are benefiting from this.

@pjobson
Copy link
Author

pjobson commented Jul 13, 2022

Added note about AWS EC2.

@jportella93
Copy link

This worked wonders!! thank you 😄

@royyanbach
Copy link

Worked perfectly! Thank you 🚀

@Hackder
Copy link

Hackder commented Oct 2, 2022

Works like a charm 🚀
Thank you very much!

@J-Pster
Copy link

J-Pster commented Oct 4, 2022

🇧🇷 VERSÃO EM PORTUGUÊS: 🇧🇷
https://gist.github.com/J-Pster/de514040cf83c1ec5677d36f78919bb8

@pjobson
Copy link
Author

pjobson commented Oct 4, 2022

@J-Pster

NICE! I'm elated to see how much the community enjoys my doc.

@pjobson
Copy link
Author

pjobson commented Oct 4, 2022

Minor updates and clarification.

@codemonkeynumber24
Copy link

codemonkeynumber24 commented Oct 19, 2022

Upgrading to a newer laptop this no longer works for me - only a blank screen is displayed. Has anyone experienced this before and knows a workaround?

The window opens and displays the 'Cypress'-Logo and header and the top bar is also loaded but nothing else happens. Attempting to open the App Data from the top bar results in the following error:

Exited with code 3
Error: Exited with code 3
at ChildProcess. (/home//.cache/Cypress/10.5.0/Cypress/resources/app/node_modules/opn/index.js:87:13)
at Object.onceWrapper (node:events:646:26)
at ChildProcess.emit (node:events:526:28)
at ChildProcess.emit (node:domain:475:12)
at maybeClose (node:internal/child_process:1092:16)
at Socket. (node:internal/child_process:451:11)
at Socket.emit (node:events:526:28)
at Socket.emit (node:domain:475:12)
at Pipe. (node:net:687:12)

UPDATE: I have corrected the issue, I was unknowingly still on WSL1. This guide helped me upgrade and after a restart everything ran fine.

@fabianlugones
Copy link

fabianlugones commented Oct 24, 2022

Thank you so much for this! 🙌

@shrinidhi6547
Copy link

shrinidhi6547 commented Nov 1, 2022

Getting error after saving dbus file "sudo vi /etc/sudoers.d/dbus".
source ~/.bashrc
Error - Please see nvm --help or https://github.com/nvm-sh/nvm#nvmrc for more information.
Please Help

@pjobson
Copy link
Author

pjobson commented Nov 18, 2022

@shrinidhi6547 I missed your message before, sorry. Did you figure it out? If not, do this and post your results: cat ~/.bashrc

@shrinidhi6547
Copy link

@shrinidhi6547 I missed your message before, sorry. Did you figure it out? If not, do this and post your results: cat ~/.bashrc

it's resolved.

@jadir-junior
Copy link

jadir-junior commented Dec 21, 2022

I tried this but I'm receving a error:

 npx cypress open                         jadirjsjunior@NBQFC-3ZS5Z63
It looks like this is your first time using Cypress: 12.2.0


Cypress failed to start.

This may be due to a missing library or dependency. https://on.cypress.io/required-dependencies

Please refer to the error below for more details.

----------

Command was killed with SIGTRAP (Debugger breakpoint): /home/jadirjsjunior/.cache/Cypress/12.2.0/Cypress/Cypress --no-sandbox --smoke-test --ping=628

----------

Platform: linux-x64 (Ubuntu - 22.04)
Cypress Version: 12.2.0

@Danziger
Copy link

Danziger commented Jan 11, 2023

This worked initially but now when running cypress open I just get the following error after a while:

The Test Runner unexpectedly exited via a close event with signal SIGSEGV

Anyone experiencing the same issue?

Update:

I was setting the DISPLAY variable like so:

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

Which seems to cause issues with the firewall I'm using.

@shrinidhi6547
Copy link

shrinidhi6547 commented Jan 11, 2023

Ubuntu system

Mac OS & windows system

For more detailed info, check out this official guide on troubleshooting.

Running specs from the command line:

# run the entire suite
npm run cy:run

# run headless chrome
npm run e2e -- --headless --browser chrome

# run an individual spec file
npm run e2e -- --spec "cypress/e2e/specs/smoke.cy.js"

# run all specs within the folder matching the glob (Note: Using double quotes is strongly recommended.
npm run e2e -- --spec "cypress/e2e/**/*"

Running specs from the GUI:

  1. Open the Cypress Test Runner and click on any types of testing E2E Testing & Component Testing.
npm run cy:open
  1. The Cypress Test Runner will open a new window with brower option, select respective browser.

  2. The Cypress Test Runner will open a new window with specs, clicking on any spec will execute the test in browser.

TRY the above method, worked for me - @jadir-junior

@dominic-ks
Copy link

dominic-ks commented Jan 20, 2023

Connecting to the display on Windows is definitely a challenge. Using this method didn't work for me:

  • export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

This is because the IP that WSL2 was putting in there for me by default was preventing internet access for WSL for some reason, possibly the reason I come on to later, so I had updated this to use 1.1.1.1 as my name server.

I was then using the getdisplay.sh for a while with no issues, when one day it suddenly stopped working.

For some reason, even though the getdisplay.sh script was returning my local IP correctly as far as I could tell from my router, running ipconfig on my Windows machine was returning something else. I then started using this instead (based on examples I found here) to run ipconfig on Windows from WSL2 and fish out the correct IP address to use:

  • export DISPLAY=$(ipconfig.exe | grep 'vEthernet (WSL)' -A4 | cut -d":" -f 2 | tail -n1 | sed -e 's/\s*//g'):0.0

And I am now up and running again...

Wondering if this will help you @Danziger, your error was the same one I started getting.

@mapucat
Copy link

mapucat commented Feb 13, 2023

Worked perfectly! Thank you, man! You are awesome! 🚀

@pjobson
Copy link
Author

pjobson commented Feb 13, 2023

I'm glad folks are getting use of this guide. I never really thought there'd be this much positive response. Thanks for all the contributions!!

@samali0996
Copy link

Hey just wanted to drop this link here. Looks like after a wsl update, Linux GUI capability was added.

https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Updating my WSL made the cypress ui run.

@isomorpheric
Copy link

Hey just wanted to drop this link here. Looks like after a wsl update, Linux GUI capability was added.

https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Updating my WSL made the cypress ui run.

can confirm, GUI on Win10 and Win11 by default now 🚀 🚀 🚀

@stoikerty
Copy link

I kept getting this error in WSL2 for Ubuntu 22.04 and 22.04.2

Cypress verification timed out.

This command failed with the following output:

/home/user/.cache/Cypress/8.4.1/Cypress/Cypress --no-sandbox --smoke-test --ping=113

----------

Command timed out after 30000 milliseconds: /home/user/.cache/Cypress/8.4.1/Cypress/Cypress --no-sandbox --smoke-test --ping=113 Timed out

----------

Platform: linux (Ubuntu - 22.04)
Cypress Version: 8.4.1

I fixed it by running export DISPLAY=:0 before npx cypress open.
see: cypress-io/cypress#21075 (comment)

@benjamin-jun-li
Copy link

Hey just wanted to drop this link here. Looks like after a wsl update, Linux GUI capability was added.

https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Updating my WSL made the cypress ui run.

I tried to update my wsl2, it seems that the linux GUI capability is still not added for me....

@sslkb
Copy link

sslkb commented Oct 6, 2023

I am trying to run the cypress in headless mode, but says "not able to connect to Browser" and fails
image

@sslkb
Copy link

sslkb commented Oct 6, 2023

And in UI, ie cypress open will open new window but no browsers are listed

@pjobson
Copy link
Author

pjobson commented Oct 9, 2023

THIS IS DEPRECATED

I put a note in the instructions, folks are still requesting support now and then, MS has instructions on their site.

https://learn.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

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