TechBuzzPro

How to Install Django on CentOS: Step-by-Step

· 4 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Update the system
    Log in and run:
    sudo yum update -y (CentOS 7) or sudo dnf update -y (CentOS 8/9). This ensures all packages are current.
  2. 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) or sudo dnf install -y python3 python3-pip (CentOS 8/9). Verify with python3 --version and pip3 --version.
  3. Install virtualenv (optional but recommended)
    To keep your Django project isolated, install virtualenv:
    sudo pip3 install virtualenv or sudo python3 -m pip install virtualenv.
  4. Create a project directory
    Navigate to where you want your project and create a folder:
    mkdir ~/myproject && cd ~/myproject
  5. Create a virtual environment
    Run:
    virtualenv venv
    If virtualenv isn't installed, use python3 -m venv venv instead. This creates a folder named venv.
  6. Activate the virtual environment
    Activate it with:
    source venv/bin/activate
    Your prompt should now show (venv).
  7. Install Django
    Inside the virtual environment, install Django using pip:
    pip install django
    This installs the latest stable version. To install a specific version, use pip install django==4.2 (for example).
  8. Verify the installation
    Check the Django version:
    django-admin --version
    You should see a version number like 5.1.
  9. 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
    Visit http://your_server_ip:8000 in a browser—you should see the Django welcome page.

Common Mistakes

  • Using Python 2 – Django 2.0+ does not support Python 2. Always use python3 and pip3.
  • 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 use dnf. 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.txt file with pip freeze > requirements.txt to 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 sudo only 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 8080 to use a different port, or find and kill the process with lsof -i :8000.
  • SSL errors when using pip – Update pip first: pip install --upgrade pip. If that fails, try pip 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.