The “Port is already in use” error typically occurs when a program or service is attempting to use a network port that is already being used by another process. This could happen if multiple applications are trying to use the same port simultaneously or if a previous instance of an application did not release the port properly.
Solution
The solution for the “Port is already in use” error involves identifying the process currently using the port and then either terminating that process or configuring the new application to use a different port.
If you want to terminate a process using a specific port on Ubuntu, you can follow these steps:
1. Identify the process
Identify the process that is using the port. Use the lsof
command along with grep
to find the process ID (PID) associated with the port.
Show all ports
Open the terminal and type:
sudo lsof -i -P -n
List open files (lsof
) that are currently being used by active processes, network connections, associated with internet connections (-i
), displaying ports (-P
) and avoiding hostname resolution (-n
). The -n
parameter speeds up the command by avoiding DNS lookups.
or
sudo lsof -i -P -n | grep LISTEN
grep LISTEN
searches for lines containing the word “LISTEN
” in the input it receives from the previous command. It filters the output to only show lines indicating processes that are listening for incoming connections.
Show a specific port
Syntax
sudo lsof -i:<port_number>
This will show you information about the process using the specified port.
Example
sudo lsof -i:9000
2. Terminate the process
Once you have identified the PID (Process IDentifier), you can use the kill command to terminate the process.
sudo kill -9 <PID>
Replace <PID> with the actual process ID obtained from the previous step.