Django is a widely used and powerful framework for developing Python web applications. It streamlines the development process, allowing you to rapidly build applications without getting bogged down in repetitive structural code. This framework empowers you to focus on the core logic of your application, while it handles the more cumbersome tasks behind the scenes. If you need to install Django on a CentOS 7 machine, several methods are available.
This guide will demonstrate how to install Django using pip within a virtual environment (virtualenv), a highly recommended approach.
The first step involves enabling the EPEL repository, which is necessary for RedHat-based distributions like CentOS. This repository contains extra packages that are not part of the core distributions.
Installing the EPEL Repository
Installing the EPEL repository is straightforward. You simply need to configure yum to access it by running the following command:
sudo yum install epel-release
This command grants you access to a variety of packages maintained within the EPEL repository.
Check out our Best Django Hosting Plans!
Now, let’s walk through installing the Django framework using pip3 within a virtual environment.
Django Installation Through pip in Virtualenv
Using the virtualenv tool is a convenient way to install Django. This tool enables you to create isolated Python environments for your projects. This means you can install specific packages required for a particular project without causing conflicts with other projects or your system’s base Python installation. It provides a clean and organized way to manage dependencies.
We’ll start by installing pip from the EPEL repository:
yum install epel-release yum-utils -y
Next, install Python 3 and pip using:
yum install python3-pip -y
Once pip is installed, you can use it to install the virtualenv package:
pip3 install Virtualenv
When you start a new project, create a dedicated virtual environment for it. Begin by making a new project directory and navigating into it:
mkdir ~/newproject cd ~/newproject
Creating New Project in Virtual Environment
Here’s how to create a new virtual environment for your project:
virtualenv newproject
To install packages within this isolate environment, you need to activate the virtual environment with the following command:
source newproject/bin/activate
Your command prompt should now show that you’re in the virtual environment. You can now use pip3 to install Django within this environment. Make sure you don’t use sudo since the installation should be local to the environment.
pip3 install Django
You can verify the installation and Django version by typing:
django-admin –version 3.2.14
To exit the virtual environment, use the `deactivate` command:
Deactivate
To work on your reactivate your virtual project again, navigate back to your project directory and activate with the following command:
cd ~/newproject source newenv/bin/activate
Now, let us explore how to create a basic Django project on your Desktop. First navigate to a code directory and create a simple helloworld directory by running the specific commands for your Operating System:
# Windows > cd onedrive\desktop\code > mkdir helloworld > cd helloworld # macOS % cd ~/desktop/code % mkdir helloworld % cd helloworld
Create a new Python virtual environment called .venv, then install Django using pip3 inside.
# Windows > python -m venv .venv > .venv\Scripts\Activate.ps1 (.venv) > python -m pip3 install django~=3.2.14 # macOS % python3 -m venv .venv % source .venv/bin/activate (.venv) % python3 -m pip install django~=3.2.14
Conclusion
This guide has demonstrated the process of installing Django with pip3 within a virtual environment. By leveraging powerful frameworks such as Django, you can accelerate your web development workflow and focus on the unique aspects of your applications while maintaining a clean and organized environment for each project.