Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Building a react native app in WSL2

Building a react native app in WSL2

Install, build and debug a react native app in WSL2 (Windows Subsystem for Linux) and Ubuntu.

Install tools in Windows

  • Install WSL2 and Ubuntu, see here
  • Install Android Studion, see here
  • Install Viusal Studio Code, see here

Install tools in WSL2

  • Install java-8-openjdk in WSL2 (sudo apt-get install openjdk-8-jre)
  • Install Android SDK cmdline tools in WSL2, see here and adjust directory structure, see here
  • Install nodejs in WSL2, see here

Set environment variables in .profile or .bash_profile

export ANDROID_HOME=/home/xxx/Android/cmdline-tools/latest
export ANDROID_SDK_ROOT=/home/xxx/Android


export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

Connect to android hardware device from WSL2

To debug on a hardware device connect to android device via usbip from WSL2 (thanks to @cjshearer):

Connect to android virtual device in Windows

To debug on a virtual device create a virtual device (e.g. Nexus_5X_API_29) in windows with Android Virtual Device Manager from Android Studio.

Start android virtual device in Windows

Start Android virtual device (e.g. Nexus_5X_API_29) in windows

"C:\Program Files (x86)\Android\android-sdk\emulator\emulator.exe" -avd Nexus_5X_API_29

Start adb server in Windows

adb kill-server
adb -a nodaemon server start

Change firewall rule for adb.exe on first usage in Defender Popup or with Windows Defender Firewall allowing access for the public profile, because the vEthernet (Wsl) adapter belongs to the public profile

Enable access to adb server from WSL2

Set environment variable to access adb server, WSL_HOST is ip of vEthernet (WSL) interface in windows

export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)

Somtimes adb crashes using the environment variable config. One solution is to use socat (thanks to @tuanna1601).

Unset the environment variable if necessary.


Install socat (eg. sudo apt-get install socat). Socat relays the requests from wsl2 to windows using the following command:

socat -d -d TCP-LISTEN:5037,reuseaddr,fork TCP:$(cat /etc/resolv.conf | tail -n1 | cut -d " " -f 2):5037

Enable access to metro bundler from Windows

The metro bundler is running in WSL2, listening on port 8081. Windows 10 version 2004 brings network forwarding from WSL2 to Windows. So the app can connect to the metro bundler from the emulator via Windows localhost.

Sometimes there are problems with the network forwarding. A work around is to use the following script.

WSL_CLIENT is ip of WSL2.

iex "netsh interface portproxy delete v4tov4 listenport=8081 listenaddress=" | out-null;
$WSL_CLIENT = bash.exe -c "ifconfig eth0 | grep 'inet '";
$WSL_CLIENT -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
$WSL_CLIENT = $matches[0];
iex "netsh interface portproxy add v4tov4 listenport=8081 listenaddress= connectport=8081 connectaddress=$WSL_CLIENT"

Connect to Windows Subsystem for Android

To debug the app running on Windows Subsystem for Android (WSA).

Install Windows Subsystem for Android.

Start adb server in Windows.

Enable access to adb server from WSL2.

Enable access to metro bundler from Windows Subsystem for Android

The metro bundler is running in WSL2, listening on port 8081.

To enable access from WSA to WSL2, configure adb reverse port forwarding on WSA.

Connect to the WSA via and execute adb reverse.

adb connect
adb reverse tcp:8081 tcp:8081 

Create react native app in WSL2

npx react-native init AwesomeProject

Build app in WSL2

Add paraameter in file to ignore okhttp3 warnings

-dontwarn okhttp3.internal.platform.*

Start metro JavaScript bundler and bind to an ipv4 address to enable port forwarding to windows

npx react-native start --host

Build app, set device as parameter deviceId from result of adb devices

  • deviceId emulator: emulator-5554
  • deviceId WSA:
npx react-native run-android --variant=debug --deviceId <deviceId>

Debug app in Visual Studio Code from WSL2

Start vs code in WSL2

code .

and install extensions for VS Code

  • Remote - WSL
  • React Native Tools

VS Code UI runs in windows and the VS Code Server runs in WSL2, see here

Add a launch configuration in file launch.json with specified type and request

"name": "Attach to packager",
"cwd": "${workspaceFolder}",
"type": "reactnative",
"request": "attach"

Build app, attach to packager and start debugging, see here.

Copy link

For the proxy to work properly with socat. still need to run npx react-native start --host without

Copy link


Copy link

dkornilove commented Jul 26, 2022

I had the experience of successfully setting up and running react native applications on WSL + Windows AVD emulator with using socat command. But recently I moved to a new computer and did everything as it was on the last machine, only with a newer SDK and tools versions and after running the adb devices command on the WSL, I get this error
adb: failed to check server version: protocol fault (couldn't read status): Success
while windows adb server and socat command run successfully.
And when I kill the socat process, adb devices command works well, but without seeing the windows emulator for sure.
All SDK and tool versions are the same on Windows and WSL.
Can someone help me figure out what it is?

socat log for the adb devices query

2022/07/26 17:28:34 socat[2129] N opening connection to AF=2
2022/07/26 17:29:07 socat[2129] E connect(5, AF=2, 16): Connection timed out
2022/07/26 17:29:07 socat[2129] N exit(1)

upd: solved by adding the firewall rule for adb.exe

Copy link

kicksent commented Aug 30, 2022

and adjust directory structure, see here

Wish this section was more clear.

Copy link

I had problems using expo and the solution @luizwhite gave worked for me.

So, I successfully overrode that with sudo ln -s /mnt/c/Android/Sdk/platform-tools/adb.exe ~/Android/sdk/platform-tools/adb after backing up the original ubuntu adb executable (just fix that path with your windows adb path and your possible custom ubuntu adb path)

However, I'm concerned there could be any kind of performance issues due to WSL2 accessing Windows memory. It would be great if anyone could point out if it could become an issue going forward.

Copy link

@luizwhite I was able to connect the emulator. However when I run npx expo start --android I get this error. The Expo-*.apk exists in the directory. Any idea?

Copy link

NVM, I was able to install the apk with: adb install Exponent-2.28.6.apk.

Copy link

eynopv commented May 24, 2023

Having issue with npx expo start
Getting error: could not connect to TCP port 5554: Connection refused.

Set environment variables

export WSL_HOST=$(tail -1 /etc/resolv.conf | cut -d' ' -f2)
export ANDROID_SERIAL=emulator-5554
export REACT_NATIVE_PACKAGER_HOSTNAME=$(ip addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}')

Starting adb in Windows, emulator was started before

> adb -a nodaemon -P 5037 server start
adb.exe I 05-24 10:56:45 20016 16472 auth.cpp:416] adb_auth_init...
adb.exe I 05-24 10:56:45 20016 16472 auth.cpp:152] loaded new key from 'C:\Users\***\.android\adbkey' with fingerprint A7A3F84B061B063E11EFD5034A7C67ED92AA52C7AA569BE5BEE9BD597E01DDB2
adb.exe I 05-24 10:56:45 20016  5140 transport.cpp:335] emulator-5554: read thread spawning
adb.exe I 05-24 10:56:45 20016 17240 transport.cpp:307] emulator-5554: write thread spawning
adb.exe I 05-24 10:56:45 20016 16472 adb.cpp:173] emulator-5554: already offline

adb in WSL

> adb devices
List of devices attached
emulator-5554   device
> adb shell
emu64xa:/ $

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