Fix Port Already In Use In Termux
Find and stop the old Termux process using a local server port before restarting Flask, SocketIO, http.server, or dashboards.
Quick Answer
Find and stop the old Termux process using a local server port before restarting Flask, SocketIO, http.server, or dashboards.
Use this guide only when your symptom or goal matches the indicators below. If a diagnostic command reveals a different problem, follow that evidence instead of forcing every remaining step.
Last reviewed: .
Use This Guide When
Before You Run Anything
- Start the server in the foreground and keep its terminal output visible.
- Confirm the exact host, port, serving folder, and URL before opening the browser.
- Expose only a dedicated public folder; never expose the Termux home folder, keys, backups, or private repositories.
Diagnostic Or Setup Commands
Replace `8080` with the affected port and `PID` with the verified process ID. Send `TERM` first; do not stop an unknown process or use `KILL` unless graceful shutdown fails.
pkg install -y lsof
lsof -i :8080
# Replace PID only after checking the process shown above
ps -p PID -o pid,cmd
kill -TERM PID
sleep 2
ps -p PID -o pid,cmdWhat The Commands Tell You
- `pkg install -y lsof` — Installs the listed Termux packages and dependencies.
- `lsof -i :8080` — Lists files, names, and permissions.
What Success Looks Like
- The local URL opens on the same phone and the terminal logs the request.
- Refreshing the page serves the current files rather than stale cached content.
- Stopping the server releases the port and removes any temporary public tunnel.
Safe Next Steps
- Verify the serving process and folder locally before changing browser or tunnel settings.
- Check whether another process already owns the port.
- Use `127.0.0.1` for same-phone testing and an intentional bind address for LAN testing.
- Create a public tunnel only after the local URL works and remove secrets from the served folder.
Common Mistakes
- Opening the wrong port or assuming `localhost` on another device points to the phone.
- Starting several background servers without tracking their process IDs.
- Publishing credentials, source repositories, backups, or the complete Termux home through a tunnel.
Still Stuck?
Copy the support report, fill in the missing values, and include the complete terminal output. Remove passwords, tokens, private keys, exact home paths, and personal information.
Fixed This Error But Want A Repeatable Way To Diagnose The Next One?
The Complete Termux Troubleshooting Handbook gives you a structured diagnostic process for Termux, Python, Git, storage, packages, networking and recovery instead of relying on disconnected fixes.