How to run localhost on mobile and access it from a real Android device instead of the emulator?

Hello there.

I’ve been working on a common development challenge: accessing a local web server from a physical Android device. It’s interesting how 10.0.2.2:port works perfectly on the Android emulator to access my laptop’s web server, but that same approach doesn’t translate to a physical device. Even though my phone is connected via USB and shows up in adb devices, it somehow can’t access the server directly via that emulator-specific route.

My current understanding is that to resolve this, both the laptop and phone must be on the same Wi-Fi network. Then, the server should be accessed using the laptop’s local IP address (e.g., 192.168.x.x:port) instead of relying on USB debugging or emulator-specific routes.

I’m putting this out to the community to confirm if this is indeed the standard and most reliable way to achieve this, or if there are other efficient methods you’ve successfully used. Any insights would be greatly appreciated.

Thank You

Hello @arpanaarora.934! Your question about running a local web server from a real Android device is a common one, and I’ve certainly run into this exact issue myself!

You’re right: 10.0.2.2 only works in the emulator because it’s a special alias for the host machine within that virtual environment. For actual physical devices, I consistently use the method you mentioned: get my laptop’s IP address by running ipconfig (on Windows) or ifconfig / ip a (on macOS/Linux), and then access the server directly like 192.168.0.101:3000 from the mobile device.

A crucial point, often overlooked, is to ensure your development server (e.g., Node, React’s dev server, etc.) isn’t bound to localhost but explicitly set to 0.0.0.0. Otherwise, your phone won’t be able to see it on the network.

Hope this clarification, especially the server binding tip, helps you streamline your mobile development workflow!