Error in php artisan serve - Failed to listen on 127.0.0.1:8xxx (reason: Address already in use)

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component. 

So, while using Laravel you will often find yourself running commands like 
    
    $ php artisan serve


PHP now comes with a built-in development server and we can use the above serve Artisan command to serve your application. This command will start a development server at http://localhost:8000

You can also set up more robust local development options like installing Homestead and Valet or setting up a server like apache or nginx on your local machine and then creating a virtual host to run your applications like http://dev.awesomeapp.com

But the above requires some setup and sometimes can get time-consuming. Therefore, Artisan remains good to go option for many users.

Now, sometimes you might forcefully or accidentally stop the server which you ran using 
    
    $ php artisan serve

and when you try to rerun the server you might end up with the following error
    
    $ php artisan serve
    Laravel development server started: <http://127.0.0.1:8000>
    [Mon Jul  9 10:45:38 2018] Failed to listen on 127.0.0.1:8000
    (reason: Address already in use)

The reason for this is that the server did not stop properly or some process is already running on the same port. We have two ways to solve this

1) Killing the process running on the desired port -

If the server did not stop properly then probably we will still have artisan running on our port. So, we'll try to list the processes with PHP in it.
    
    $ ps -ef | grep php

This will return the list of all the processes containing the word PHP.
    
    $ ps -ef | grep php
    501   770   723   0 10:35AM ??         0:08.95 php /Users/gurmeetsingh/.vscode/extensions/felixfbecker.php-intellisense-2.3.3/vendor/felixfbecker/language-server/bin/php-language-server.php --tcp=127.0.0.1:49377 --memory-limit=4095M
    501   791   697   0 10:36AM ttys000    0:00.27 php artisan serve
    501   795   791   0 10:36AM ttys000    0:00.35 /usr/local/Cellar/php71/7.1.14_25/bin/php -S 127.0.0.1:8001 /Users/gurmeetsingh/Desktop/carhp-app/server.php
    501  1026   697   0 10:46AM ttys000    0:00.00 grep php
    
If the server is still running artisan on that port you'll see results like above. Here, you can clearly see that that artisan serve is running (line 2). Now, we have to force close this process. To do this we'll need to refer to it. We can refer to a process by the number mentioned in the second column. So, in our case, the process can be referred to by the number 791.

To kill the process simply run
    
    $ kill 791

Now again run 
    
    $ ps -ef | grep php

If you still see the artisan running then we'll have to force close it by using the following command
    
    $ kill -9 791

-9 is a signal number. You can read more about it here.

By now the process would have been killed and you can resume working on the server by running 
    
    $ php artisan serve

If you are still facing any issue try the second method

2) Start server on a different port - 

This one is a comparatively easy way. If the port on which artisan runs by default (8000) is not free just try to run it on a different port by using
    
    $ php artisan serve --port=8001

You can replace 8001 by any port number you wish.

Hope one of these works for you. If you still face any issue feel free to contact via the comment section.







Comments

Popular posts from this blog

Event Loop in Javascript - What, Why and How

Validate single/multiple ckeditor fields in a form