How to Fix Flask 'Address already in use' When Starting Dev Server
In this tutorial, you'll learn about How to Fix Flask 'Address already in use' When Starting Dev Server. We cover key concepts, practical examples, and best practices.
The Problem
You run flask run or python app.py and get:
OSError: [Errno 98] Address already in use
or on Windows:
OSError: [WinError 10048] Only one usage of each socket address is normally permitted
The previous Flask server didn't shut down cleanly and still holds port 5000 (the Flask default). The OS refuses to start a new server on the same address. This happens most often when the terminal is closed without stopping the server, or when the reloader creates a child process that survives the parent.
Quick Fix
1. Kill the process holding the port
# Find the PID using port 5000
lsof -i :5000
# Example output: python 12345 user 3u IPv4 ... TCP *:5000 (LISTEN)
# Kill it
kill -9 12345
On macOS, the PID is the second column. On Linux, use sudo lsof -i :5000 if the process runs as a different user.
2. Use fuser to kill directly (Linux)
fuser -k 5000/tcp
This kills any process listening on TCP port 5000 without needing to look up the PID first.
3. Use a different port
flask run --port=5001
Or set the environment variable:
export FLASK_RUN_PORT=5001
flask run
Useful if you want to run multiple Flask apps simultaneously.
4. Allow address reuse in your app
if __name__ == "__main__":
app.run(port=5000, use_reloader=True)
The reloader already handles socket reuse. If you disable the reloader (FLASK_DEBUG=0 or use_reloader=False), the socket stays in TIME_WAIT state and prevents immediate restart.
5. Check if Flask already started in background
# List all python processes
ps aux | grep python
# Kill all Flask processes
pkill -f "flask run"
If you used flask run & or ran the server in a background terminal, the process may still be alive even after closing the terminal.
6. Use a script to kill and restart in one command
fuser -k 5000/tcp 2>/dev/null; flask run
Add this as an npm script or shell alias for a single-command restart during development.
7. Reuse the address by setting SO_REUSEADDR (advanced)
import socket
from flask import Flask
app = Flask(__name__)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)
Flask's built-in development server with debug=True already sets SO_REUSEADDR on the socket, allowing immediate port reuse after the server stops. This is the simplest way to prevent the "address in use" error during development.
Prevention
- Always stop the server with Ctrl+C, not by closing the terminal
- Use
fuser -k 5000/tcpas a one-liner before starting the dev server - Consider using a launcher script that kills the old process first
- Set
FLASK_DEBUG=1so the reloader manages socket lifecycle properly - Use port 5001 for a second project to avoid conflicts
- Check for background processes with
ps aux | grep flaskif the port stays busy
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro