You try to start your server and get hit with:
Or the Python equivalent:
Something else is already using that port. Here’s how to find it and fix it.
Quick Fix: Find and Kill
On Linux/macOS
Find what’s using the port:
| |
Output:
Kill it:
| |
Or if it won’t die:
| |
One-Liner to Kill Whatever’s on a Port
| |
The -t flag outputs just the PID, making it pipeable to kill.
Alternative: Using fuser
Some systems have fuser which is even more direct:
| |
This finds and kills whatever’s using TCP port 3000 in one command.
Using netstat or ss
If lsof isn’t available:
| |
Output:
Common Causes
1. Previous Process Didn’t Exit Cleanly
Your server crashed or you Ctrl+C’d it, but it didn’t release the port. The process might be a zombie or stuck in cleanup.
Fix: Kill it with the methods above.
2. Running Multiple Instances
You accidentally started your server twice, maybe in different terminals.
Fix: Kill the old one, or use a different port for development.
3. Docker Container Still Running
A container might be binding to the host port:
| |
4. Process Running in Background
You ran something with & or nohup and forgot about it:
| |
5. systemd Service
A service might be configured to use that port:
| |
Preventing the Problem
Use Dynamic Ports in Development
Instead of hardcoding a port, let the OS assign one:
| |
| |
Graceful Shutdown Handling
Make sure your app releases the port on exit:
| |
SO_REUSEADDR Option
Allow reusing a port that’s in TIME_WAIT state:
| |
| |
TIME_WAIT State
Sometimes the port shows as in use even after killing the process. This is the TCP TIME_WAIT state — the kernel keeps the port reserved briefly to handle any straggling packets.
Check for it:
| |
If you see TIME-WAIT, you can either:
- Wait — it clears in 30-60 seconds
- Use SO_REUSEADDR — lets you bind anyway (see above)
- Change the port — quickest workaround
Quick Reference
| Task | Command |
|---|---|
| Find process on port | lsof -i :3000 |
| Kill process on port | lsof -ti :3000 | xargs kill -9 |
| Find and kill (one command) | fuser -k 3000/tcp |
| Check port state | ss -tan | grep :3000 |
| List all listening ports | ss -tlnp |
Summary
- Find what’s using the port:
lsof -i :PORT - Kill it:
kill PIDorkill -9 PID - If it keeps happening, add graceful shutdown handling to your app
- For quick development, consider using port 0 to auto-assign
The error is almost always “something else is already running there.” Find it, kill it, move on.