How to run Node-RED on Windows 10 WSL (Linux Subsystem)

Windows 10 has a feature that allows you to run Linux in a lightweight virtual container.

WSL allows easy access to and from Windows/Linux filing systems.

It runs a special version of the Linux kernel to achieve tight integration with Windows. You may install a number of different Linux distributions from the Windows Store, Ubuntu being the original and probably best developed. You cannot (yet) easily run a Linux desktop but this should be integrated in 2021H2. In addition, WSL does not use systemd as most distributions now do.

How to install Node.js and Node-RED

  1. Install node.js from the official installation page. If you are using a Debian-based distribution on WSL, I strongly recommend using the NodeSource binary distributions.

  2. Install Node-RED using npm -g install node-red or my alternate-node-red-installer.

  3. Start node-red

  4. Panic because you now don't know how to access the started web server!

It is OK, no need for panic. Microsoft have crafted some magic so that localhost (AKA on WSL is mapped over to the same on the Windows host.

But now something really weird happens. You can start Node-RED on the host and in WSL - with the same PORT! When you do, the WSL version takes preference.

Incidentally, because WSL is a virtual environment, it has its own virtual network interface (It actually uses Hyper-V) with its own IP address, something in the range 172.20.*.*. Unfortunately, this may not be the same address every time WSL is started. :frowning: I add the following to my ~/.bashrc script in WSL so that I can see the IP address every time I start it:

export WSL_IP=`ip route get 1 | awk '{gsub("^.*src ",""); print $1; exit}'`
echo "WSL Local IP Address: $WSL_IP"

This has an impact if you want to make your WSL-based service available from outside the host (e.g. from a different device). In that case, you will need to look up the various work-arounds that have been published that grab the IP address and create firewall forwarding rules.

PS: If you are going to the trouble of using WSL on Windows, you will almost certainly want a couple of other things as well:

  • Microsoft Terminal - this is the new windows terminal and beats the pants of the one that comes with Windows. Install via the Windows Store.

  • VSCode - This free IDE/Code Editor is open source but backed by Microsoft. Microsoft have produced a number of extensions including some remote editing extensions for WSL, Docker and SSH.

And of course, as soon as you publish something like this, you discover more info.

You can find out the IP address of WSL from the host with the command wsl hostname -I. The address is stable for a specific instance of WSL and it will change on a system reboot or a fully shutdown/termination of WSL. Different Linux distributions will have different WSL instances and therefore different IP addresses.

windows 10 - Make IP address of WSL2 static - Super User

And another quick tip. This bash script will copy your WSL IP address with Node-RED's default port into the Windows clipboard! You could even run this at WSL startup by adding to ~/.bashrc

hostname -I | awk '{print $1}' | awk '{printf "%s:1880", $0}' | clip.exe
1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.