Test localhost on an Android phone: USB, Wi-Fi or a link
Over USB, adb reverse lets the phone open localhost itself. Debug it with chrome://inspect, reach the emulator at 10.0.2.2, or skip the cable with a link.
Updated
Over USB, adb reverse is the best way to test localhost on an Android phone. It makes localhost:5173 on the phone reach the same port on your computer, so the site loads as it does on your desk: as a secure context, with your dev server's host checks passing. Without a cable, use your Wi-Fi or a link, and inspect the page from Chrome on your computer either way.
For any phone, see how to open localhost on your phone; when nothing connects, the six checks; for an iPhone, testing localhost on an iPhone.
1. Over USB with adb reverse
- Turn on developer options. Find Build number under Settings > About phone > Build number on a Pixel, and Settings > About phone > Software information > Build number on a Galaxy. “Tap the Build Number option seven times until you see the message You are now a developer!”
- Turn on USB debugging: Settings > System > Advanced > Developer Options > USB debugging, on Android 9 and later.
- Get adb. It comes with Android Studio, or on its own as the SDK Platform Tools: “This download is useful if you want to use adb directly from the command-line and don't have Studio installed.”
- Plug the phone in, allow debugging when it asks, and forward the port:
adb reverse tcp:5173 tcp:5173Then open http://localhost:5173 in Chrome on the phone. The rule adb reverse tcp:5173 tcp:5173 sends the phone's port 5173 to your computer's, over the cable. Why it beats the Wi-Fi:
- The phone opens localhost, which counts as a secure context, so the camera, location and service workers work over plain http.
- Your dev server sees
localhost:5173as the host, so Vite, Next.js and Django need no setting changed. - Hot reload connects to the same port, so it works on most dev servers.
- No network to get wrong: guest Wi-Fi and firewalls don't matter.
Forward each port the page uses, an API on another port included. adb reverse --list shows the ports forwarded, and adb reverse --remove-all ends them.
Or let Chrome forward the port
Chrome on your computer can do the same without the adb command: in chrome://inspect/#devices, click Port forwarding, give the phone's port and the computer's address, like localhost:5000, and check Enable port forwarding. Its docs say why the cable is the reliable way:
“Traffic between the ports travel through the USB connection between your Android device and development machine, so the connection doesn't depend on your network configuration.”
2. Inspect it with chrome://inspect
Turn on USB debugging, open chrome://inspect#devices with Discover USB devices on, and plug the phone in. The first time, the phone shows as Offline until you accept the prompt on its screen. Then the phone's Chrome tabs are listed, each with Inspect, which opens DevTools on your computer for that tab: console, network, elements and the rest.
- Open tab with url opens a page in Chrome on the phone, which saves typing an address on the phone.
- Toggle Screencast shows the phone's page in DevTools, where clicks become taps.
While the phone is plugged in, inspecting works whichever way the page got there: through adb reverse, your Wi-Fi or a link.
3. No cable: wireless debugging
Android 11 and later can pair with your computer over Wi-Fi, with adb pair. Once they are paired, Google's docs say:
“You can now use your device wirelessly similar to how you would with a USB connection.”
So adb reverse works the same over the air. The phone and the computer have to share a network for this, unlike the cable.
4. In the Android emulator: 10.0.2.2
Inside the emulator, localhost doesn't reach your computer: there, 127.0.0.1 is the emulated device's own loopback interface. Your computer gets an address of its own, 10.0.2.2, which the docs describe as:
“Special alias to your host loopback interface (127.0.0.1 on your development machine)”
So http://10.0.2.2:5173 opens your dev server. To the browser it isn't localhost, though: no secure context, and dev servers that only allow local names turn it away. Django in debug, for one, accepts only ['.localhost', '127.0.0.1', '[::1]']. adb reverse helps here too: adb lists emulators as devices, like emulator-5554, so the same command gives the emulator a real localhost.
5. Over Wi-Fi, or with a link
Without adb, start the dev server on the network and open your computer's address, like http://192.168.1.20:5173; the flag for each dev server is in the general guide. Or give it a public link, printed as a QR code:
ouicu share 5173On a Pixel, the Camera app shows a link when it sees a QR code; tap it. Quick Settings has a Scan QR code tile too. The link is https, opens on mobile data and on a client's phone, and your dev server sees each request as coming to localhost. On Free, the phone shows a short notice from ouicu first, once a week per share.
Which to use
| USB with adb reverse | Wi-Fi | A link | |
|---|---|---|---|
| Address on the phone | localhost:5173 | Your computer's address and port | An https link |
| Secure context | Yes | No | Yes |
| Dev server settings | None | Listen on the network, and allow the address on some | None |
| Whose phone | The one on your cable | One on your Wi-Fi | Anyone's |
Questions
How do I open localhost on an Android phone?
Connect it by USB with USB debugging on, run adb reverse tcp:5173 tcp:5173 with your port, and open localhost:5173 in Chrome on the phone.
Why doesn't 10.0.2.2 work on my phone?
It is the emulator's name for your computer, and exists only inside the emulator. On a real phone, use adb reverse, your computer's Wi-Fi address or a link.
Does adb reverse work over Wi-Fi?
Yes, once the phone is paired for wireless debugging: adb treats it as it would a phone on a cable.
Sources
Prices, defaults and quotes about other products, and the day each was last checked at its source.
- Configure on-device developer options, Android Developers. Checked .
- SDK Platform Tools release notes, Android Developers. Checked .
- adb: commandline.cpp (adb reverse), Android source. Checked .
- Access local servers and Chrome instances with port forwarding, Chrome for Developers. Checked .
- Remote debug Android devices, Chrome for Developers. Checked .
- Android Debug Bridge (adb), Android Developers. Checked .
- Network address space, Android Developers. Checked .
- Settings, Django docs. Checked .
- Scan QR codes with your Pixel phone, Pixel Phone Help. Checked .