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:

@fxia-sonatus
Copy link

I see any error when doing make modules and need to install this package: sudo apt install dwarves

@ansmaniac
Copy link

Thanks for your good explaination. It worked fine for me except two problems I experienced.

  1. git checkout 'uname -r' was not working for me. The kernel name was not matching to the tags on the repo. Please find it manually. Mine was for example 5.15.90.1-microsoft-standard-WSL2
  2. The line : Create the file .wslconfig in your Windows user folder C:/Users// with the following content: needs to be fixed with C:/Users//

@ansmaniac
Copy link

I see any error when doing make modules and need to install this package: sudo apt install dwarves

worked for me as well

@rsarrazin2
Copy link

rsarrazin2 commented Feb 3, 2025

Confirmation from my side, with some additional notes:

  • Using Ubuntu-24.04 image (with standard 5.15.167.4-microsoft-standard-WSL2 kernel - uname -r output) -> used a fresh distro for this, but probably not required
  • The branch names and the uname -r don't strictly match (anymore?), but finding which of git branch -a branches and uname -r output match is straightforward.
  • I had to install libncurses-dev for menuconfig to run.
  • I recommend running make modules with e.g. -j8.
  • I confirm the need for sudo apt install dwarves, thanks @fxia-sonatus.
  • Timing: Only make modules takes some time (even with parallel jobs). The remaining steps are quick.
  • .wslconfig needs to be created in C:/Users/<yourwindowsloginname>/.
  • Beware: The kernel change applies to all distributions, not only the one you've built the new kernel in. If this causes an issue (it doesn't on my side, CAN is then enabled for all distributions), you can simply revert by removing the .wslconfig file.

@rsarrazin2
Copy link

Following up on my own comment

The kernel change applies to all distributions, not only the one you've built the new kernel in.

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+.

Could anyone narrow down which of the original instructions need to be performed on the other distributions for them to be able to load the CAN modules as well? I can't imagine the full steps are required since the kernel has already been built.

@manavortex
Copy link

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 :)

@fulmicotone98
Copy link

I followed all the steps and now I can create a running vcan interface, but still I have an error trying to use the interface (with candump for example):
"socket: Address family not supported by protocol"

Has someone ever had this error?
Thanks!

@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