How to Install Django on CentOS: Step-by-Step
In this article
This guide walks you through installing Django, the popular Python web framework, on CentOS. You'll set up a virtual environment, install Django, and verify the installation—all from the command line. We'll cover both CentOS 7 and CentOS 8/9 (Stream) where commands differ.
What You'll Need
- A CentOS server or local machine with root or sudo access.
- An active internet connection.
- Basic familiarity with the Linux command line.
Step-by-Step
- Update the system
Log in and run:sudo yum update -y(CentOS 7) orsudo dnf update -y(CentOS 8/9). This ensures all packages are current. - Install Python and pip
CentOS 7 comes with Python 2.7 by default, but Django 3.x and later require Python 3. Install Python 3 and pip:sudo yum install -y python3 python3-pip(CentOS 7) orsudo dnf install -y python3 python3-pip(CentOS 8/9). Verify withpython3 --versionandpip3 --version. - Install virtualenv (optional but recommended)
To keep your Django project isolated, install virtualenv:sudo pip3 install virtualenvorsudo python3 -m pip install virtualenv. - Create a project directory
Navigate to where you want your project and create a folder:mkdir ~/myproject && cd ~/myproject - Create a virtual environment
Run:virtualenv venv
If virtualenv isn't installed, usepython3 -m venv venvinstead. This creates a folder namedvenv. - Activate the virtual environment
Activate it with:source venv/bin/activate
Your prompt should now show(venv). - Install Django
Inside the virtual environment, install Django using pip:pip install django
This installs the latest stable version. To install a specific version, usepip install django==4.2(for example). - Verify the installation
Check the Django version:django-admin --version
You should see a version number like5.1. - Create a test project (optional)
To ensure everything works, create a sample project:django-admin startproject mysite
Then run the development server:cd mysite && python manage.py runserver
Visithttp://your_server_ip:8000in a browser—you should see the Django welcome page.
Common Mistakes
- Using Python 2 – Django 2.0+ does not support Python 2. Always use
python3andpip3. - Installing Django globally – This can cause conflicts with system packages. Always use a virtual environment.
- Forgetting to activate the virtual environment – If you see
ModuleNotFoundError: No module named 'django', you're likely not inside the venv. - Using the wrong package manager – CentOS 7 uses
yum, CentOS 8/9 usednf. Using the wrong one gives a command not found error.
Tips
- Use a dedicated virtual environment for each project to keep dependencies separate.
- After installing Django, create a
requirements.txtfile withpip freeze > requirements.txtto track dependencies. - For production, use a WSGI server like Gunicorn and a web server like Nginx—don't use the development server.
- If you're using CentOS 7, consider upgrading to Python 3.6 or higher manually if the default is too old for newer Django versions.
Troubleshooting
- pip3 not found – On CentOS 7, the package may be named
python3-pip; if not, install EPEL first:sudo yum install epel-release, then retry. - Permission denied when installing with pip – If you get permission errors, you're likely outside the virtual environment. Activate it first, or use
sudoonly for global installs (not recommended). - django-admin not found – This means the virtual environment's bin directory isn't in PATH. Ensure you've activated the venv with
source venv/bin/activate. - Port 8000 already in use – Run
python manage.py runserver 8080to use a different port, or find and kill the process withlsof -i :8000. - SSL errors when using pip – Update pip first:
pip install --upgrade pip. If that fails, trypip install --trusted-host pypi.org --trusted-host files.pythonhosted.org django.
Frequently Asked Questions
Do I need to use a virtual environment?
It's strongly recommended. Virtual environments prevent package conflicts and make your project portable. You can skip it, but you risk breaking system Python packages.
How do I install a specific version of Django?
Use pip install django==4.2 (replace with the version you need). Check available versions with pip index versions django.
Can I use Django with Python 2 on CentOS 7?
No, Django 2.0 and later require Python 3. If you must use Python 2, you'd need Django 1.11, which is no longer supported.
How do I exit the virtual environment?
Type deactivate and press Enter. This returns you to the global Python environment.
Why does my Django project not show up when I visit the server IP?
Make sure you run python manage.py runserver 0.0.0.0:8000 to listen on all interfaces. Also, check firewall rules: sudo firewall-cmd --add-port=8000/tcp --permanent and reload.
How do I update Django later?
Activate your virtual environment and run pip install --upgrade django.