Flask — port 5000 already in use when starting the dev server
Resolve Flask address-in-use errors by finding the process bound to the port and choosing a free port or fixing the stale server.
Symptoms
- OSError: [Errno 98] Address already in use on startup
- On macOS, port 5000 fails because AirPlay Receiver listens there
- Starting two dev servers for the same app produces the error
- A previously killed server is still holding the port
Possible causes
- Another process is listening on the same host and port
- A previous dev server did not shut down and still holds the socket
- The operating system reserves the port for another service such as AirPlay on macOS
- Two instances of the app are launched, for example by a reloader plus a manual start
Diagnosis steps
Find the process bound to the port
lsof -i :5000Interpretation
The listing shows the PID and command holding the port. On Windows use netstat -ano | findstr :5000 to get the PID.
Stop the stale process
kill <PID>Interpretation
Terminating the previous dev server releases the socket. If it refuses to die, confirm it is not a service you still need.
Start on a different port to confirm the cause
flask run --port 5001Interpretation
If the app starts cleanly on another port, the conflict is the port rather than the application code.
Root cause
The socket is already bound by another process, so the new server cannot bind. The app code is usually fine; the problem is a duplicate or stale process or a reserved port.
Fix
Stop the process holding the port or run the server on a free port, and on macOS disable the AirPlay Receiver or choose another port. Ensure only one server instance runs per port.
Verify the fix
Start the server and confirm it logs a running-on URL without the OSError, then load the app in a browser to confirm it responds.
Symptom walkthrough
Flask fails immediately at startup with OSError: [Errno 98] Address already in use, or the equivalent message on Windows. The traceback points at the socket bind call, not at application logic. On macOS the default port 5000 is often taken by the AirPlay Receiver, so the error appears even when you believe nothing else is running.
Fix in practice
Identify the process bound to the port with lsof or netstat, stop it, and restart. If the port is reserved by the operating system, run the app on another port or free the reserved one. The safest habit is to keep one dev server per port and let the reloader manage restarts.
Verification
After the fix, the server prints a running-on URL and the app responds in a browser. If the error persists, verify no second instance was launched and that the chosen port is not reserved.
Diagnostic commands
Check the behavior with commands like these:
lsof -i :5000
kill <PID>
flask run --port 5001