Skip to content

Instantly share code, notes, and snippets.

@yonatanh20
Last active April 23, 2025 09:41
Show Gist options
  • Save yonatanh20/664f07d62eb028db18aa98b00afae5a6 to your computer and use it in GitHub Desktop.
Save yonatanh20/664f07d62eb028db18aa98b00afae5a6 to your computer and use it in GitHub Desktop.
Enabling SocketCAN on WSL2 walkthrough

#Enabling SocketCAN on WSL2 Preface: this walkthrough is a hand-holdy step by step tutorial to enable SocketCan on your WSL2 instance (Ubuntu 20.04).

To enable SocketCAN's can-utils on WSL we need to enable the CAN interface module in the WSL, to do so requires a re-building of the WSL kernel.

Requirements:

From cmd / powershell

wsl --shutdown
wsl --update

From wsl

sudo apt update
sudo apt install can-utils

If you try now to use candump for example you'd get this error message: socket: Address family not supported by protocol

This just means that WSL2 doesn't come with the CAN interface support enabled, so we need to build the wsl kernel and enable it.

Get the latest WSL2 kernel and configure it for can and vcan support.

sudo apt install build-essential flex bison libssl-dev libelf-dev
cd ~
git clone https://github.com/microsoft/WSL2-Linux-Kernel
cd WSL2-Linux-Kernel
git checkout 'uname -r'
cat /proc/config.gz | gunzip > .config
make prepare modules_prepare
make menuconfig 

The config cli app will pop up and we need to enable the following settings. Enter Networking support Change CAN bus subsystem support to M and enter Change Raw CAN Protocol to M Enter CAN Device Drivers Change Virtual Local CAN Interface to M Make sure CAN bit-timing calculation is set to * Optionally change CAN devices debugging messages to * Save and exit

And now we will build the kernel and export it to your Windows user lcoation.

make modules
sudo make modules_install
make -j $(nproc)
sudo make install
cp vmlinux /mnt/c/Users/<yourwindowsloginname>/

The file vmlinux was created and now we will load it. We need to create config file for wsl to load the custom kernel. Create the file .wslconfig in your Windows user folder C:/Users// with the following content:

[wsl2]
kernel=C:\\Users\\<yourwindowsloginname>\\vmlinux

Now you can reset WSL with the new kenrel. We need to enable the modules every time we restart the WSL kernel:

sudo modprobe can
sudo modprobe can-raw
sudo modprobe vcan

And now you are able to create virtual can devices to run. For example:

sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
sudo ip link add dev vcan1 type vcan
sudo ip link set up vcan1

Sources:

@hansfn
Copy link

hansfn commented Mar 17, 2025

While that seems to be correct, I can't e.g. "sudo modprobe can" on my other distributions, as it produces modprobe: FATAL: Module can not found in directory /lib/modules/5.15.167.4-microsoft-standard-WSL2+.

That is probably because you didn't run sudo make modules_install inside your other distributions. (I think no other changes are needed since the kernel is shared.)

Added: Maybe also sudo make install - haven't have time to test.

@Afx31
Copy link

Afx31 commented Apr 23, 2025

Thank you for the detailed explanation. This helped me a lot while doing this!

However, the tutorial was slightly outdated, so I've taken the liberty of updating it to 2025. Would be great if you could merge it :)

This updated method still works as of today, using Ubuntu. Thankyou!

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