February 7, 2025

Fixing 'Port Already in Use' Error When Starting a Web Server

Imagine you have a toy car, but when you try to play with it, someone else is already using it. You either have to ask them to stop or find another toy to play with. Similarly, when you start your web server, sometimes another program is already using the port (like a parking spot for your server). Here's how to fix that!

1. Find Out Who is Using the Port

Before you can fix the issue, you need to see which program is using the port (like checking who is playing with your toy).

  • Windows (Command Prompt):

    netstat -ano | findstr :8082
    

    The last number in the output is the process ID (PID), which tells you which program is using the port.

  • Mac/Linux (Terminal):

    lsof -i :8082
    

    This will list the program using port 8082.

  • Another way for Mac (using netstat):

    netstat -anv | grep 8082
    

    This also shows details about which app is using the port.

2. List All Running Processes and Ports

If you want to see all the busy ports and running processes (like checking which toys are being used in the whole room):

  • Windows:

    netstat -aon
    
  • Linux/macOS:

    netstat -tulnp
    

    or

    ss -tulnp
    
  • Mac Users (Using Homebrew lsof): First, install lsof (if not installed):

    brew install lsof
    

    Then run:

    lsof -nP -iTCP -sTCP:LISTEN
    

    This will list all processes listening on different ports.

3. Stop the Process Using the Port

Once you know which process is using the port, you can ask it to stop.

  • Windows:

    taskkill /PID <PID> /F
    

    Replace <PID> with the actual process ID.

  • Mac/Linux:

    kill -9 <PID>
    

    This forcefully stops the process.

4. Force Stop the Process (If It Won’t Quit)

If the process doesn’t stop normally, you can forcefully end it.

  • Windows:

    taskkill /IM <process-name> /F
    

    Replace <process-name> with the actual application name.

  • Mac/Linux:

    sudo pkill -9 -f <process-name>
    

    or

    sudo killall -9 <process-name>
    
  • Mac Users (Using Homebrew htop): If you like a visual way to see and stop processes, install htop:

    brew install htop
    

    Then run:

    htop
    

    Use the arrow keys to select the process and press F9 to stop it.

5. Change the Port Instead

If you don’t want to stop the process, you can change your web server to use a different port (like finding another parking space).

  • Spring Boot: Change application.properties or application.yml:

    server.port=8083
    
    server:
      port: 8083
    
  • Node.js/Express:

    const port = process.env.PORT || 8083;
    
  • Tomcat: Edit conf/server.xml:

    <Connector port="8083" ... />
    

6. Restart Your Computer

If nothing works, restarting your computer can free up the port (like clearing the whole room so you can play with your toy again).

7. Common Port Issues and Solutions

Issue Solution
Port is in use by another process Find and kill the process using lsof, netstat, or taskkill.
Port is not being released after stopping the app Use kill -9 or taskkill /F to forcefully terminate it.
Firewall or antivirus blocking the port Temporarily disable the firewall and check connectivity.
Process restarts automatically after being killed Find and stop the service using systemctl, sc stop, or launchctl.
Need to free up all used ports Use netstat -aon or lsof -i to list all used ports and terminate unnecessary processes.

By following these steps, you can easily fix the "Port Already in Use" error and get your web server running smoothly!