In this article, we’re talking about the “Error: That port is already in use” issue when launching a webserver with Django, its causes and what you can do to fix the issue.
What causes this error?
As you can probably guess from the error message itself, the issue is caused by Django running multiple webservers on the same port. Each webserver requires its own unique port to run on, and Django’s default port number is 8000.
If you try and run multiple projects without first specifying a port number, Django will try to run all of them on port 8000, which can cause this error.
How to fix this?
The error can be fixed by launching the webserver on a different port. To do so, you’d use the following command:
python manage.py runserver 0.0.0.0:x
Where ‘x’ is the four-digit port number you want to use for the server. Note that it can be any number other than 8000, as then you’d run into the same error again.
It’s also recommended that you close any processes using the 8000 port before launching your Django project. If you’re on Linux, use this command:
sudo fuser -k 8000/tcp
macOS users can use the following command.
sudo lsof -t -i tcp:8000 | xargs kill -9
Remember that if no process uses the 8000 port, your Django server will run fine without you specifying a port number. This means you can run the aforementioned commands to kill any processes associated with the 8000 port and run your webserver as usual without any problems whatsoever.
However, if you want to run multiple web servers simultaneously, it becomes important to mention the port number in the run command.