Linux error that port is already in use

How to solve “Error: That port is already in use” in Django

It must be frustrating when you run the Django local server, and you get the “Error: That port is already in use.” You cannot serve your new Django project on a local development setup.

The main cause of the error is serving more than one Django project using the same default port, 8000.

Fortunately, solving the error is a very straightforward process.

To solve the “error: that port is already in use” in Django, you need to stop the currently running server on port 8000 and free the port for the new Django project.

Here are three ways to solve “Error: That port is already in use” in Django:

1. Locate the Terminal that you have other Django runserver running and press CTRL + C to quit the server.

That way, you allow another Django project to use the port to serve your new Django project.

2. Specify the port you want to serve your Django project when running the local server, Django runserver.

To do that, run python manage.py runserver 0.0.0.0:9000. The new Django project should be served on port 9000, and the old Django project should be served using the default port, 8000. You may use any port number you may want. Using this method, you can run more than two Django projects on the same local machine.

3. By opening the Terminal and typing htop, press function key F3, type ‘runserver’ and press function key F9.

The process kills the Django server running in the background. If you want to stop the old Django runserver process, but you do not have access to the Terminal that it is running, you may use htop to kill the runserver background process.

Let’s see how we can implement the three methods to solve “Error: That port is already in use” in detail.

Method 1: How to quit Django local development server

Locate the port that you have your Django local server running.

How to run Django local server on Linux

The Terminal window should look like this:

Press CTRL + C to quit the Django local development server.

It is as simple as that – nothing else that you need to do.

Open another Terminal for the new Django project and try running python manage.py runserver.

Django’s local development server should run successfully because you only have one Django project on port 8000.

There should be no more than one runserver instances running concurrently. Thus, the “Error: That port is already in use” should not show again.

Method 2: How to serve a Django project on a different port

Another way to solve “Error: That port is already in use” is to serve your new Django project on another port different from the default port 8000.

To do that, open a new Terminal window and activate the virtual environment you have installed the new Django project.

Читайте также:  Linux adding python to path

Run python manage.py runserver 0.0.0.0:9000 and the error should not appear.

The new Django project should be served using port 9000.

You may use any other port to serve your new Django project.

However, do not serve your Django project on ports already used by other services such as databases. For example, if you use port 5432, which is used by PostgreSQL databases, you will still get the “Error: That port is already in use.”

Serving a new Django project on a different port has its advantage because you can serve different Django projects on the same local machine. Provided you do not have two projects sharing the same port – that is impossible.

As you can see in the screenshot below, I am serving two Django projects, the Django project in the left pane on default port 8000 and the right pane running another Django project on port 9000.

How to serve two Django projects on a local development server (Linux)

When you access each Django project on the browser, you must specify the specific port number used to serve the Django project.

So, if you are serving a Django project on port 9000, you should open 127.0.0.1:9000 in your browser.

Similarly, if you have another project on port 4000, you should type 127.0.0.1:4000 in the address bar.

How to serve two Django websites on different ports

The screenshot below shows how to access two Django projects running on ports 8000 and 4000 in the same local machine on a browser.

Method 3: How to kill Django runserver process running in the background

To kill the background Django runserver process, you need to use htop to browse the process in the background and send a signal to kill it. That way, no Django server instances are running on default port 8000.

The final method involves killing the runserver process running in the background. The reasons that could have led to the Django runserver running in the background would be:

  1. Pressing CTRL + Z, which suspends the server.
  2. Closing the Terminal running the Django server.

By suspending or closing the Terminal window with the Django project, you can only quit the Django development server using htop. htop is a utility you can use to manage background processes running on your Linux machine.

Let’s see how you use htop to quit the Django server running in the background.

But first, you must install the utility before using it.

Here’s how to install htop on your Linux machine:

Open a new Terminal window and type the following:

After a successful install, htop should be ready to use.

How to run htop on Linux

Using the same Terminal window or a new one, type htop and press Enter.

Press function key F3 to search the Django server running in the background.

Type ‘runserver’ and you should see the active Django background process highlighted in a bright color.

How to search a background process using htop on Linux

Press function key F9 to kill the Django development server process.

Use the up-down keys and use signal 9 SIGKILL to quit the Django development server.

Press Enter to kill the process.

How to kill a Django background process using htop on Linux

Managing Django “Error: That port is already in use is as easy as that.

You may choose to serve only one Django project using the default server.

Alternatively, you may choose to server more than one Django project on different port numbers.

Читайте также:  Batch processing in linux

When serving Django projects on different ports, you should not have two Django projects sharing the same port.

Besides, avoid using port numbers used by other services such as databases.

If you have a PostgreSQL database, then port 5432 should not be used to serve Django websites on a local machine.

And that’s it basically for this article.

Источник

Django Server Error: port is already in use

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

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.

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

17 Answers 17

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

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

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.

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.

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 

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.

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 

@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.

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

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

Читайте также:  Grub при загрузке linux mint

Just type this on your terminal

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.

Those who are the most likely to try this without properly understanding it will probably also not understand that point without further elaboration.

>> 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 

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!

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)

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

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 

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

  1. In terminal, Type ps aux | grep runserver
  2. Hit Enter
  3. Use PID among the result execute kill -9

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.

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.

Источник

Оцените статью
Adblock
detector