How to Fix Python 'Address Already in Use' Errors
'Address already in use' (OSError: [Errno 98]) means another socket already holds the port your Python app wants. On a real Ubuntu machine we find who holds it with sudo ss -ltnp and lsof, show why Ctrl+Z leaves a paused server holding the port, pick another port when it isn't yours to stop, and fix the error that comes right after a restart (TIME-WAIT) with SO_REUSEADDR.
Port 5000 is taken
Your Python app won't start: address already in use. At a small greenhouse, Ana runs a dashboard that shows the temperature on each bench. She starts it, and Flask says port five thousand is in use by another program. Flask is polite enough not to say which one. Nothing of hers is running. So what is holding port five thousand? There are four usual reasons, and if you're on a Mac, one of them is the Mac itself. By the end you'll know which one is yours, and how to fix it.
python3 dashboard.py* Serving Flask app 'dashboard' * Debug mode: off Address already in use Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.
One program per port
A port is a numbered way into a computer. A server claims one, and listens on it for a browser to knock. Think of a hotel room: one guest per room. When a second program asks Linux for the same number, Linux refuses, with error ninety-eight: address already in use. It says address because a server claims two things at once: the computer's network address, and the port number. So first, we find who's in the room and clear it. Then a stranger case: the same error, with nobody in the room at all.

Find who holds the port
The greenhouse computer is shared: Ben, who works there too, logs in to it as well. To find out who holds the port, Ana uses ss, a tool that lists network connections. She asks who's listening on port five thousand. The L asks for listeners, and the P asks for the program's name. It shows a listener, but no name. That's a clue: ss only names your own programs, so this one belongs to someone else. So she runs it again with sudo, as the administrator, who can see everyone's programs. Now ss names it: python three, process number 14. And ps, the tool that lists running programs, says whose it is: Ben's. He started his own copy earlier, to try a change, and left it running.
ss -ltnp 'sport = :5000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 127.0.0.1:5000 0.0.0.0:*
sudo ss -ltnp 'sport = :5000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 127.0.0.1:5000 0.0.0.0:* users:(("python3",pid=14,fd=3))
ps -o user,pid,etime,cmd -p 14USER PID ELAPSED CMD ben 14 00:07 python3 dashboard.py
Ana checks with Ben, then stops his copy with kill and that process number. It needs sudo too, because the program is Ben's, not hers. kill asks the program to stop, and a Flask server does. ss shows the port free again.
sudo kill 14
sudo ss -ltnp 'sport = :5000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
The two steps are the same everywhere: find the process number, then stop it. This table has the commands for Linux, Mac, and Windows. On a Mac, the finding tool is called lsof. Pause here if you need yours. If a program ignores a plain kill, use kill dash nine. That ends it at once, but skips its clean-up, so always try the plain kill first.

Paused is not stopped
Ana starts her dashboard, and it runs. To stop it, she presses Ctrl+Z. The terminal says: stopped, and gives her the prompt back. Guess first: is port five thousand free now? No. Starting it again gives the same error as before.
python3 dashboard.py* Serving Flask app 'dashboard' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit ^Z [1]+ Stopped python3 dashboard.py
python3 dashboard.py* Serving Flask app 'dashboard' * Debug mode: off Address already in use Port 5000 is in use by another program. Either identify and stop that program, or start the server with a different port.
The terminal said stopped, but it really means paused. Ctrl+Z doesn't end a program. The jobs command lists it: stopped, but still there. Like a guest asleep in the room: the room is taken, and nobody answers the door. curl, which fetches a page from the terminal, knocks on the port. Nobody answers, so after three seconds it gives up. fg brings the paused program back to the front. It wakes up, answers the knock that was still waiting, and logs it, and then Ctrl+C really stops it.
jobs[1]+ Stopped python3 dashboard.py
curl -m 3 localhost:5000curl: (28) Operation timed out after 3007 milliseconds with 0 bytes received
fgpython3 dashboard.py 127.0.0.1 - - [26/Nov/2026 23:56:29] "GET / HTTP/1.1" 200 - ^C
When it's not yours to stop
Sometimes the program on the port isn't yours to stop. Flask's own docs say what to do: stop the other program, or pick a different port. On a Mac, it's often the Mac itself: since macOS Monterey, a built-in service called AirPlay Receiver uses port five thousand. Turn it off in System Settings, or move your app to another port.

Moving is one command. Instead of python three, Ana uses Flask's own command, flask run, because it can take a port number. Dash dash app names the file, and dash dash port picks five thousand and one. It starts at once. Just remember to open it at port five thousand and one in your browser now.
flask --app dashboard run --port 5001* Serving Flask app 'dashboard' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5001 Press CTRL+C to quit ^C
It fails right after a restart
Now the strange one. The greenhouse has a second program, the collector. It's written with Python's socket module. That's the basic building block for network programs, without any of Flask's extras. It takes a reading from each bench's sensor on port nine thousand. Ana stops it properly, with Ctrl+C, and starts it again straight away. This time it's plain Python's error: error ninety-eight. And with sudo, ss finds nobody listening on port nine thousand at all.
python3 collector.pycollector listening on port 9000 bench 2: 22.5 C bench 3: 20.9 C bench 1: 22.4 C bench 2: 21.5 C bench 3: 21.4 C ^C collector stopped
python3 collector.pyTraceback (most recent call last): File "/home/ana/greenhouse/collector.py", line 5, in <module> server.bind(("0.0.0.0", 9000)) OSError: [Errno 98] Address already in use
sudo ss -ltnp 'sport = :9000'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
Earlier, Ana asked ss only for listeners. This time she swaps the L for an A, for all connections, and adds an O, for timers. There they are: connections from the sensors, each in a state called time wait, with a timer. Guess first: how long until port nine thousand frees itself? About a minute. Ana waits, and 61 seconds after she stopped it, the port is free.
ss -tano 'sport = :9000' | head -4State Recv-Q Send-Q Local Address:Port Peer Address:Port Process TIME-WAIT 0 0 192.168.40.10:9000 192.168.40.21:54072 timer:(timewait,56sec,0) TIME-WAIT 0 0 192.168.40.10:9000 192.168.40.21:54104 timer:(timewait,59sec,0) TIME-WAIT 0 0 192.168.40.10:9000 192.168.40.21:54074 timer:(timewait,57sec,0)
Why wait? Each sensor sends a reading, and the collector says ok and hangs up first. The side that hangs up first keeps a note of that connection for a minute, so a delayed message from it can't land in a new conversation by mistake. It's the room being cleaned after checkout: the guest is gone, but nobody gets the room for a while.

The fix for your own server
Waiting a minute after every restart gets old fast, especially while you're testing changes. Python's socket docs name this exact error, and the fix: a socket option called SO_REUSEADDR, which tells Linux it's fine to reuse a port that's only in time wait.

In the collector, it's one line. It goes just before bind, which is the call that claims the port. The call is setsockopt, short for set socket option, and the number one at the end turns this option on. It never lets two programs listen on one port. It only stops old connections in time wait from blocking a restart. Flask and Python's own http server already set it for you. Your own socket servers don't.

One catch: right after the change, it fails once more. The connections still waiting were made by the old version, without the option, so they block this one last start. So just this once, Ana waits out the minute, starts the new version, and stops it with Ctrl+C. From then on, she can stop the collector and restart it straight away. The time wait lines are still there, and it starts anyway.
python3 collector.pyTraceback (most recent call last): File "/home/ana/greenhouse/collector.py", line 5, in <module> server.bind(("0.0.0.0", 9000)) OSError: [Errno 98] Address already in use
python3 collector.pycollector listening on port 9000 bench 2: 21.8 C bench 3: 22.5 C bench 1: 21.6 C bench 2: 22.0 C bench 3: 22.4 C ^C bench 1: 22.2 C collector stopped
ss -tan 'sport = :9000' | head -3State Recv-Q Send-Q Local Address:Port Peer Address:Port Process TIME-WAIT 0 0 192.168.40.10:9000 192.168.40.21:36662 TIME-WAIT 0 0 192.168.40.10:9000 192.168.40.21:36704
python3 collector.pycollector listening on port 9000 bench 2: 21.5 C bench 3: 21.6 C bench 1: 22.4 C ^C bench 2: 20.8 C collector stopped
Recap
So, which one is yours? Run ss with sudo. If it names someone else's program, stop it, or move to another port. On a Mac, it may be the Mac itself: turn off AirPlay Receiver, or move ports. If it's your own paused one, use fg, then Ctrl+C. And if nobody is listening, it's time wait: wait a minute, or give your server SO_REUSEADDR before bind.

If your server starts on the first try now, a like helps YouTube show this to more beginners, and subscribing gets you the next fix, tested on a real machine.
Every error and timer in this video came from a real Ubuntu machine, and the commands and lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- bind(2): EADDRINUSE, the given address is already in use
- socket(7): SO_REUSEADDR
- ss(8): -l listening sockets, -p the process using each socket
- Python socket docs: Errno 98 after a quick rerun is TIME_WAIT; SO_REUSEADDR
- Python socketserver: allow_reuse_address (False on TCPServer, set by http.server)
- Flask, Development Server: Address already in use, --port 5001, macOS AirPlay Receiver
- Bash manual, job control: Ctrl+Z suspends a job, jobs, fg
Demos: two containers on our own Mac standing in for a greenhouse's small computer and its bench sensors; the greenhouse and its people are made up. Every error and timer was real. Watch the full video on YouTube.