Django Server Error: port is already in use

Django Server Error: port is already in use

Restarting the Django server displays the following error:
this port is already running....

This problem occurs specifically on Ubuntu and not other operating systems. How can I free up the port to restart the server?

Solutions/Answers:

Answer 1:

A more simple solution just type sudo fuser -k 8000/tcp.
This should kill all the processes associated with port 8000.

EDIT:

For osx users you can use sudo lsof -t -i tcp:8000 | xargs kill -9

Answer 2:

netstat -ntlp

It will show something like this.

   Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State           PID/Program name    
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN      6599/python         
tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      -                   
tcp        0      0 192.168.124.1:53        0.0.0.0:*               LISTEN      -                   
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN     

So now just close the port in which Django/python running already by killing the process associated with it.

kill -9 PID

in my case

kill -9 6599

Now run your Django app.

Answer 3:

ps aux | grep -i manage

after that you will see all process 


ubuntu@ip-10-154-22-113:~/django-apps/projectname$ ps aux | grep -i manage
ubuntu    3439  0.0  2.3  40228 14064 pts/0    T    06:47   0:00 python manage.py runserver project name
ubuntu    3440  1.4  9.7 200996 59324 pts/0    Tl   06:47   2:52 /usr/bin/python manage.py runserver project name
ubuntu    4581  0.0  0.1   7988   892 pts/0    S+   10:02   0:00 grep --color=auto -i manage


kill -9 process id


e.d kill -9 3440


`enter code here`after that :

python manage.py runserver project name

Answer 4:

We don’t use this command { sudo lsof -t -i tcp:8000 | xargs kill -9 } Because it’s close all tabs…You should use to

ps -ef | grep python

kill -9 process_id

ps -ef | grep python (show all process with id)

kill -9 11633
(11633 is a process id to :- /bin/python manage.py runserver)

Answer 5:

By default, the runserver command starts the development server on the internal IP at port 8000.

If you want to change the server’s port, pass it as a command-line argument. For instance, this command starts the server on port 8080:

python manage.py runserver 8080

Answer 6:

This is an expansion on Mounir’s answer. I’ve added a bash script that covers this for you. Just run ./scripts/runserver.sh instead of ./manage.py runserver and it’ll work exactly the same way.

#!/bin/bash

pid=$(ps aux | grep "./manage.py runserver" | grep -v grep | head -1 | xargs | cut -f2 -d" ")

if [[ -n "$pid" ]]; then
    kill $pid
fi

fuser -k 8000/tcp
./manage.py runserver

Answer 7:

For me, this happens because my API request in Postman is being intercepted by a debugger breakpoint in my app… leaving the request hanging. If I cancel the request in Postman before killing my app’s server, the error does not happen in the first place.

–> So try cancelling any open requests you are making in other programs.

On macOS, I have been using sudo lsof -t -i tcp:8000 | xargs kill -9 when I forget to cancel the open http request in order to solve error = That port is already in use. This also, complete closes my Postman app, which is why my first solution is better.

Answer 8:

ps aux | grep manage

ubuntu 3438 127.0.0 2.3 40256 14064 pts/0 T 06:47 0:00 python manage.py runserver

kill -9 3438

Answer 9:

It seems that IDEs, VSCode, Puppeteer, nodemon, express, etc. causes this problem, you ran a process in the background or just closed the debugging area [browser, terminal, etc. ] or whatever , anyway, i have answered same question before,
Here you are it’s link

https://stackoverflow.com/a/49797588/2918720

Answer 10:

Type ‘fg’ as command after that ctl-c.
Command:
Fg will show which is running on background. After that ctl-c will stop it.

fg
ctl-c

Answer 11:

if you have face this problem in mac you just need to open activity monitor and force quite python then try again

enter image description here

Answer 12:

lsof -t -i tcp:8000 | xargs kill -9

References