WSL for Developers!: Connect USB devices
Tags: usb, wsl2, android, mobile
Published: 2022-06-06 01:23:55

---
title: WSL for Developers!: Connect USB devices
published: true
date: 2022-06-06 01:22:20 UTC
tags: usb, wsl2, android, mobile
canonical_url: https://halimsamy.com/wsl-for-developers-connect-usb-devices
series: WSL for Developers!
cover_image: https://halimsamy.com/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1654460741462%2Fa_nehlYtc.jpg%3Fw%3D1600%26h%3D840%26fit%3Dcrop%26crop%3Dentropy%26auto%3Dcompress%2Cformat%26format%3Dwebp&w=1920&q=75
---

Have you ever wanted to connect a USB to WSL for development or debugging? Good news! this is possible thanks to the [usbipd-win](https://github.com/dorssel/usbipd-win) project!

## What is the usbipd-win project?

Umm! it's simple. Quoted from their [GitHub repo](https://github.com/dorssel/usbipd-win):

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

```bash
uname -a
```

The output should be something like that:

```bash
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](https://github.com/dorssel/usbipd-win).

- Running the (.msi) installer. Get it from their [releases page](https://github.com/dorssel/usbipd-win/releases).
- 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:

```bash
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:

```bash
usbipd wsl list
```

You can expect the output to be like:

```text
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:

```bash
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:

``` bahs
lsusb
```

You should see an output similar to:

```text
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_):

```bash
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.

```bash
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:

```text
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.

```bash
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](https://docs.microsoft.com/en-us/windows/wsl/connect-usb) and [a blog post on the Windows Command Line Blog](https://devblogs.microsoft.com/commandline/connecting-usb-devices-to-wsl) beside the [usbipd-win repo wiki](https://github.com/dorssel/usbipd-win/wiki/WSL-support) and the fix of the ADB issue came from a [Stack Exchange answer](https://android.stackexchange.com/questions/144966/how-do-i-get-my-device-detected-by-adb-on-linux). I'm just documenting all of those in a single place to save time for people searching for all of this!