Address already in use errors – Find and kill process

Sometimes we get the error Failed to listen to address when starting a new application, since the address is already in use. As an example, when working with my Laravel apps and I need to stop and restart the server, I get the following error:

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

For cleaning up and work withthe desired address, follow the next steps:

lsof -i tcp:8000

This lists the «pids» for applications using port 8000. So once we have the pid we can kill the process with the kill command:

kill -9 <pid>