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.
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.
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.
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.
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:
- Pressing CTRL + Z, which suspends the server.
- 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:
sudo apt update sudo apt install htop
After a successful install, htop should be ready to use.
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.
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.
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.
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.
See you next time, Dev!