Error message

bind: Address already in use

Something is already listening on the port your program wants.

Quick diagnosis

Start here. This one command usually tells you which of the causes below you are dealing with:

$ sudo ss -tlnp | grep :8080

Names the process holding the port, with its PID. Replace 8080 with your port.

Causes, ordered by how often they are the culprit

1 A previous instance is still running (most common)

You stopped the terminal but not the process, or it forked into the background, or the crash left it alive.

Check
$ sudo ss -tlnp | grep :3000
Fix
$ kill $(sudo lsof -t -i:3000)

Try plain kill first so the process can shut down cleanly. Only escalate to kill -9 if it ignores you.

2 Another service legitimately uses that port

Common collisions: 80 and 443 with an already-installed nginx or Apache, 3306 with MySQL, 5432 with PostgreSQL, 53 with systemd-resolved.

Check
$ sudo ss -tlnp | grep -E ":(80|443) "
Fix
$ sudo systemctl stop nginx

Or change your program to a different port — often the better answer, since the other service is there for a reason.

3 TIME_WAIT after a restart

A socket that has just closed stays in TIME_WAIT for up to a couple of minutes. Restarting immediately can hit this, especially in test suites.

Check
$ ss -tan | grep TIME-WAIT | head
Fix
$ # set SO_REUSEADDR in your server code, or wait ~60s

Most frameworks set SO_REUSEADDR by default. If yours does not, that is the correct fix rather than waiting.

4 You are binding to a privileged port without permission

Ports below 1024 need root. Some systems report this as an "in use" style failure rather than a permission error.

Check
$ id && sudo ss -tlnp | grep :80
Fix
$ sudo setcap CAP_NET_BIND_SERVICE=+eip /path/to/binary

Cleaner than running the whole application as root: it grants only the ability to bind low ports.

5 A container is publishing the port

A forgotten Docker container holds the port from outside your usual process list.

Check
$ docker ps --format "table {{.Names}}\t{{.Ports}}"
Fix
$ docker stop thecontainer

How to stop it happening again

Frequently asked questions

How do I find what is using a port?

sudo ss -tlnp | grep :PORT gives the process and PID. lsof -i :PORT is the older equivalent and works just as well.

Is it safe to kill whatever is holding the port?

If it is your own leftover process, yes. If it is a system service like nginx or postgresql, stop it properly with systemctl instead — and consider whether your program should use another port.

Why does it fail right after I stopped the program?

TIME_WAIT: the socket is closing gracefully and holds the port for up to a couple of minutes. SO_REUSEADDR in the server avoids the wait.

Why can I not bind to port 80 as a normal user?

Ports below 1024 are privileged. Use sudo, grant CAP_NET_BIND_SERVICE to the binary, or put a reverse proxy in front.

Commands involved

ss

See listening ports, active connections and which process owns each socket.

kill

Terminate processes politely or forcibly — and understand what -9 really does.

ps

See what is running, find a process ID, and combine with grep to hunt down a program.

systemctl

Start, stop, enable and inspect services — the control panel of modern Linux.

Concepts behind this error: Port · Process · PID (Process ID)

How to do it properly

Free a port that is already in use

Find which process holds a port and stop it cleanly.

Find the process using a port

Identify the PID and program listening on a Linux TCP port.

Other errors

Permission denied (publickey)

SSH rejected your login because no key you offered was accepted by the server.

bash: command: command not found

The shell searched every directory in your PATH and found no executable with that name.

No such file or directory

The path you gave does not resolve — but often the missing part is not the one you think.

Permission denied

The path resolves fine — the system simply will not let you do that with it.