Halim Samy
Programming with Halim

Programming with Halim

WSL for Developers!: Connect USB devices

WSL for Developers!: Connect USB devices

Halim Samy's photo
Halim Samy
·Jun 6, 2022·

5 min read

Subscribe to my newsletter and never miss my upcoming articles

Table of contents

  • What is the usbipd-win project?
  • Check if your WSL version is supported
  • Setting up the Windows side
  • Setting up the WSL side
  • Attaching device
  • ADB not detecting your phone/device?
  • Resources

Have you ever wanted to connect a USB to WSL for development or debugging? Good news! this is possible thanks to the usbipd-win project!

What is the usbipd-win project?

Umm! it's simple. Quoted from their GitHub repo:

Windows software for sharing locally connected USB devices to other machines, including Hyper-V guests and WSL 2.

Thus, it requires WSL 2, not WSL 1 (you shouldn't be using WSL 1 anyway!). With this being said let's dive more into this!

Check if your WSL version is supported

What we will be doing here is not supported by every WSL kernel. Open up your WSL terminal and run the following command to check your kernel version:

uname -a

The output should be something like that:

Linux HALIMSAMY 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Do you see that kernel version above? It should be 5.10.60.1 or later (with WSL2), if that's not the case, feel free to close the article or update your WSL!

Setting up the Windows side

There are two ways to install usbipd-win.

  • Running the (.msi) installer. Get it from their releases page.
  • Use the Windows Package Manager (winget): winget install usbipd

It doesn't matter how you install it as long as it gets installed at the end, this is up to your personal preference. However, take this note into consideration from their repo:

If you are using a third-party firewall, you may have to reconfigure it to allow incoming connections on TCP port 3240.

Setting up the WSL side

With the Windows side ready, let's move to WSL. (I'm using Ubuntu 20.04 LTS) Again open up your WSL terminal and run the following command to install some packages that are needed:

sudo apt update
sudo apt upgrade # recommend, but not required
sudo apt install linux-tools-virtual hwdata
sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/*/usbip 20

Attaching device

Plug in the device using USB to the host machine (Windows) and open PowerShell or Command Prompt on your Windows machine as Administrator to continue.

You can list all of the available device by running:

usbipd wsl list

You can expect the output to be like:

BUSID  VID:PID    DEVICE                                                        STATE
1-2    22b8:2e81  < YOUR DEVICE >                                               Not attached
1-3    0b05:1866  USB Input Device                                              Not attached
2-1    0461:4dfb  USB Input Device                                              Not attached
2-4    13d3:3563  MediaTek Bluetooth Adapter                                    Not attached

Once you find the device you want and it's bus ID, you can attach it using the following command:

usbipd wsl attach --busid <BUD ID>

You might face a problem where the device is already being used by your Windows host or some application make sure to close that and try again. Also when attaching a device it is recommended to have a WSL terminal already opened so that we ensure that the WSL lightweight VM is up and running!

Now switching to the WSL side, Verify that the device was attached by running:

lsusb

You should see an output similar to:

Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 005: ID 22b8:2e81 < YOUR DEVICE >
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Congratulations! Now you can access your devices from inside WSL! When you're done with your device from inside WSL you can de-attach and give it back to the Windows machine by running the following command (from the same Administrator shell):

usbipd wsl detach --busid <BUS ID>

ADB not detecting your phone/device?

When you realize that you can access your USB devices from inside WSL, you would want to use that for your Android development! Well, it's possible actually and in the previous part of the series, we managed to install the Android SDK so we can do that. You can simply just run adb devices and it might detect your device out of the box (it worked for me once!) or we might need to do some extra steps!

Remember that output you got from running lsusb? Do you see those magic numbers after the text "ID" (The 22b8:2e81 part), In fact, those are not just magic numbers, those are the VendorID and ProductID (in order) Now keep that in mind.

Now, I would like you to edit the following file: /etc/udev/rules.d/51-android.rules, use vim or nano it doesn't matter, maybe you would need to install one of them if not already installed.

sudo apt install nano
sudo nano /etc/udev/rules.d/51-android.rules

It is expected that the file would be empty, now add the following line:

SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", ATTR{idProduct}=="2e81", MODE="0666", GROUP="plugdev", SYMLINK+="android%n"

Remember to change the VendorID and ProductID to what you have got from lsusb. Now everything should be working fine. adb devices should show your device.

Additionally, you might need to add the VendorID to your ~/.android/adb_usb.ini (one ID per line, in hex notation like: 0x22b8), but this is usually not needed!

One more issue you can face while attaching your Android device is that Chrome DevTools is using your device from your Windows machine, Disable it for now!

You can also find that starting the adb server as root fixes the problem.

adb kill-server
sudo adb start-server
adb devices

Resources

I'm a man who respects copyrights and if I would quote someone or use this work, I would have to mention him. I didn't come up with all of this out of the blue! In fact, Microsoft has documentation about this topic and a blog post on the Windows Command Line Blog beside the usbipd-win repo wiki and the fix of the ADB issue came from a Stack Exchange answer. I'm just documenting all of those in a single place to save time for people searching for all of this!

 
Share this