WARN Failed to listen on 127.0.0.1:8000 (reason: Address already in use).

Posted on

Resolving Port Conflict Issues in Laravel on macOS and Windows

When developing with Laravel, you might encounter an issue where the PHP built-in server fails to start due to a port conflict. The error message you may see is something like this:

WARN  Failed to listen on 127.0.0.1:8000 (reason: Address already in use).

This means that another application is already using the port you want to use (in this case, port 8000). This problem can occur on both macOS and Windows. Below, we’ll walk through the steps to identify and resolve this issue on both operating systems.

Identify Which Application Is Using the Port

On macOS

To find out which process is using the port 8000, you can use the lsof (List Open Files) command. Open your terminal and run:

lsof -i :8000

This will return a list of processes using port 8000. You will see output similar to this:

COMMAND   PID             USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
php     22748 milhamsyahhafizs    7u  IPv4 0x7f8d7e69038a467d      0t0  TCP localhost:irdmi (LISTEN)

The PID (Process ID) column shows which processes are using the port.

To terminate the process, use the kill command with the Process ID (PID) you found. For example, if the PID is 22748, run:

kill 22748

If the process does not stop with the normal kill command, you can forcefully terminate it by using the kill -9 command:

kill -9 22748

You can kill multiple processes at once by separating their PIDs with a space:

kill 22748 22749 22750 22751

On Windows

To find the process using port 8000, open Command Prompt as Administrator and run:

netstat -ano | findstr :8000

This command will show a list of processes using port 8000 along with their PID.

On Windows, use the taskkill command to terminate a process by its PID. For example:

taskkill /PID 22748 /F

Where 22748 is the PID of the process occupying the port.

Restart Your Laravel Application

Once the port is free, you can restart your Laravel application using the following command:

php artisan serve

This will start the Laravel development server on port 8000 (or another port if you specify a different one).

Alternative Solution: Change the Port Number

If you don’t want to stop the conflicting process, you can choose to run your Laravel server on a different port. You can specify a different port using the --port option:

php artisan serve --port=8001

This will start the Laravel server on port 8001, which might not be in use by another process.

You can also change the port in the Laravel .env file by setting the APP_URL to a different port (though this won’t change the server’s port, it’s useful for ensuring consistency).