Skip to content
← All posts

Port 3000 already in use? Find and free it on macOS

You run npm run dev and get Error: listen EADDRINUSE: address already in use :::3000. Or Rails says the address is in use, or Flask fails on port 5000. Something on your Mac is already listening on that port, and you need to find out what it is before you can free it.

Usually it is a dev server you started earlier in another terminal tab, or one that kept running after its window closed. Sometimes it is a database, a Docker container or macOS itself. Here is how to find out which, and how to stop it safely.

Find what is using the port

macOS ships with lsof, which lists open files, and on macOS network sockets count as files. To see what is listening on port 3000:

lsof -nP -iTCP:3000 -sTCP:LISTEN

The output looks like this:

COMMAND   PID  USER   FD   TYPE  DEVICE  SIZE/OFF NODE NAME
node    48213  you    23u  IPv6  0x...   0t0      TCP  *:3000 (LISTEN)

What the parts mean:

node alone doesn’t say much when you have five projects open. To see the full command line and the folder it was started from:

ps -o command= -p 48213
lsof -a -p 48213 -d cwd -Fn

The second command prints the process’s working folder after an n, which is usually the project folder.

To list every port in use at once, drop the port number:

lsof -nP -iTCP -sTCP:LISTEN

Stop the process

Once you know the PID, ask the process to quit:

kill 48213

kill without options sends SIGTERM, a polite request. Dev servers, databases and most other programs catch it, close their connections, write what they need to disk and exit. Give it a few seconds, then run the lsof command again to check that the port is free.

If the process ignores SIGTERM, force it:

kill -9 48213

SIGKILL ends the process immediately, without any cleanup. That is fine for a hung dev server, but for a database it is like pulling the plug: it will usually recover, but unsaved work can be lost. Use it only when a normal kill didn’t work.

You can also find and stop in one line. This sends SIGTERM to every process listening on port 3000, so look at the lsof output first if you are not sure what is there:

kill $(lsof -ti tcp:3000 -sTCP:LISTEN)

When the port doesn’t free up

It comes back after you stop it

If a new process grabs the port a second later, something is restarting it. Common causes:

lsof shows nothing, but the port is still busy

lsof only shows your own processes. A port held by a process that runs as root or as another user doesn’t appear unless you use sudo:

sudo lsof -nP -iTCP:5432 -sTCP:LISTEN

Stopping that process needs sudo kill too. Be sure what it is first: a root process on a port is often a system service or something installed with an installer package.

It’s Docker

If the command is com.docke (lsof cuts names to 9 characters; the full name is com.docker.backend), the port is published by a container. Stopping the Docker process would stop all your containers. Stop just that container instead:

docker ps --format '{{.Names}}\t{{.Ports}}'
docker stop <container-name>

It’s macOS: port 5000 and 7000

On recent macOS versions, port 5000 and 7000 are often taken by ControlCenter. That is AirPlay Receiver, which lets other devices stream to your Mac, and it collides with Flask’s default port. You can’t kill it; turn it off in System Settings › General › AirDrop & Handoff › AirPlay Receiver, or run your app on another port.

Caveats

The faster way: CCToolify

The Ports tool in CCToolify keeps a live list of every port in use, with the app, the project folder or the Docker container behind it, and who can connect.

The Ports guide walks through the whole screen. Read localhost vs 0.0.0.0 to learn what the “Who can connect” column tells you.