295

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?

3
  • 4
    Dont use ctrl+z to stop server. As mentioned here askubuntu.com/a/510816 Control+Z shunts it into the background, suspended. That is why your port is already in use. Commented May 21, 2021 at 8:07
  • Also occurring on Fedora 35 Commented Jun 12, 2022 at 15:10
  • 1
    Use Control+C instead. From the askubuntu link above posted by @Muzaffer : Control+C aborts the application almost immediately Control+Z shunts it into the background, suspended Commented Jan 23, 2023 at 21:00

18 Answers 18

819

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

Sign up to request clarification or add additional context in comments.

8 Comments

On mac you need to use sudo lsof -i tcp:8000 then kill the process ids that show up.
So whatever the port is just replace 8000 with the port the block occurs.
I'm getting this error, but I have killed everything on the port.
Works! On mac: sudo lsof -i tcp:8000
I come like everyday to copy and past in my terminal. THANKS
|
82
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.

2 Comments

for this, we need to install net-tools.
If anyone struggles with this in the future, I've checked the processes using the specified port using the command $ sudo lsof -i tcp:8000 from gordonc's comment in Mounir's answer, and then ussed the command in this answer to kill it. Can runserver in python without issue after that.
18
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

3 Comments

So you try to start the server again, while one is already running? That won't work as the first one will be using the HTTP port. You must kill or terminate the first one, before trying to run again (at least run it on the same port).
It's working because you killed the currently running instance, which is what I said you have to do. You can't have two programs listening on the same network port, it's as easy as that. So this is a solution to something that isn't really a problem, just a fact.
I'm sorry if I might have been coming on to strong, I'm just feeling a little grumpy today. :/
16

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

2 Comments

@StephenRauch, the question does not ask WHO is using the port. The question is stating an error being thrown. This is a solution on how to fix that error.
The OP is asking how to restart the server on port 8000, not asking for running it on another port
13
lsof -t -i tcp:8000 | xargs kill -9

1 Comment

Don't routinely recommend kill -9; it should only be used as a last resort in desperate situations.
6
>> 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

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
5

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)

Comments

5

Sorry for comment in an old post but It may help people

Just type this on your terminal

killall -9 python3

It will kill all python3 running on your machine and it will free your all port. Greatly help me when to work in Django project.

4 Comments

This is extremely hamfisted for anyone who might be running Python for any other reason.
I already mention that it will kill all python3 running on your machine
Those who are the most likely to try this without properly understanding it will probably also not understand that point without further elaboration.
All sorts of programs use python3, so you may be killing more processes than you intended by running this.
5

Type 'fg' as command after that Ctrl-C.
Command:
Fg will show which is running on background. After that Ctrl-C will stop it.

fg
ctl-c

2 Comments

The simplest and most helpful answer.
This seems to be the most simple way to adress the OP's issue. It works, but it would be interesting to know why. Isn't the process running in the foreground already?
5
  1. In terminal, Type ps aux | grep runserver
  2. Hit Enter
  3. Use PID among the result execute kill -9 <PID>

For an instance, If result of step 1 is as follow

root      1041  0.0  0.1 266912 34580 pts/3    S+   11:31   0:01 python3 manage.py runserver 0.0.0.0:3030
root      1696  4.5  0.1 126128 40708 ?        S    Feb14 925:43 /usr/local/bin/python manage.py runserver 0.0.0.0:8000

1041 and 1696 are PIDs. We need to choose whichever process we want to kill among them.

Comments

3

Just past your terminal below the command

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

Comments

2

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

Comments

2

Click the arrow in the screenshot and find the bash with already running Django server. You were getting the message because your server was already running and you tried to start the server again.

enter image description here

Comments

1

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.

Comments

1

Dont use CTRL + Z to stop server, use CTRL + C to stop the server, I had also had the same problem in my linux (fedora) , I used to stop the server using CTRL + Z and again I used to kill the server using sudo fuser -k 8000/tcp command, which worked fine. But later when I started using CTRL + C , I didnot get that port running issue anymore.

Comments

0

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

Comments

0

In case You are using the VSC's screen terminal, The error might be due to the fact that you already runserver in some other shell.

Just click on the dropbox on the left of the + sign in the header of the terminal of VSC and select some other shell and check if the server is already running there. Quit that server and you are ready to launch a another server.

Comments

0

I was trying all the solutions but they were not working i suggest you to keep press the power button or if your battery is removeable then remove it all the process will be killed and your local host will be reset

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.