How can I kill whatever process is using port 8080 so that I can vagrant up?

On MacOSX, I'm using Packer to build a Vagrant box so I need to continually bring it up and tear it down. I'm attempting to 'vagrant up', and receive the standard error because the port is in use:

"Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 8080 is already in use on the host machine."

The solution seems simple enough: I just need to identify the process that is holding port 8080 open and kill that process, right?. It's not that easy.


If I run the command:

nmap localhost -p 8080

I receive the following output:

PORT     STATE SERVICE
8080/tcp open  http-proxy

If I run the following command:

top -o prt

The highest port in use in 1360


If I run the following command:

 netstat -tulpn | grep :8080

I receive:

netstat: n: unknown or uninstrumented protocol

If I run the following command:

lsof -i :8080

I receive no output


If I restart my computer, the port is now available and I can now 'vagrant up'.

How can I kill whatever process is using port 8080 so that I can vagrant up without restarting my computer?


Solution 1:

This might help

lsof -n -i4TCP:8080 

The PID is the second field in the output.

Or try:

lsof -i -P

Solution 2:

Fast and quick solution:

lsof -n -i4TCP:8080

PID is the second field. Then, kill that process:

kill -9 PID

Less fast but permanent solution

  1. Go to /usr/local/bin/ (Can use command+shift+g in finder)

  2. Make a file named stop. Paste the below code in it:

#!/bin/bash
touch temp.text
lsof -n -i4TCP:$1 | awk '{print $2}' > temp.text
pidToStop=`(sed '2q;d' temp.text)`
> temp.text
if [[ -n $pidToStop ]]
then
kill -9 $pidToStop
echo "Congrates!! $1 is stopped."
else
echo "Sorry nothing running on above port"
fi
rm temp.text
  1. Save this file.
  2. Make the file executable chmod 755 stop
  3. Now, go to terminal and write stop 8888 (or any port)

Solution 3:

In case above-accepted answer did not work, try below solution. You can use it for port 8080 or for any other ports.

sudo lsof -i tcp:3000 

Replace 3000 with whichever port you want. Run below command to kill that process.

sudo kill -9 PID

PID is process ID you want to kill.

Below is the output of commands on mac Terminal.

Command output

Solution 4:

sudo lsof -i:8080

By running the above command you can see what are all the jobs running.

kill -9 <PID Number>

Enter the PID (process identification number), so this will terminate/kill the instance.

Solution 5:

Use the following command:

lsof -n -i4TCP:8080 | awk '{print$2}' | tail -1 | xargs kill -9

The process id of port 8080 will be picked and killed forcefully using kill -9.