How to Install Django on CentOS 8
Choose a different version or distribution
Introduction
Before we begin talking about how to install Django on CentOS 8, let’s briefly understand - What is a Django?
Django is a powerful framework useful for writing Python web applications. It is full of features that allow getting your applications as well as sites up. It's a framework that lets you focus on the unique portions of the application, even letting the tools do the heavy lifting.
In this tutorial, you will install Django on CentOS 8. We will also address a few FAQs related to Django installation on CentOS 8.
Advantages
- Rapid development: Django provides a high-level abstraction layer, built-in tools, and ready-to-use components, allowing developers to build web applications quickly.
- Security features: Django includes various security measures, such as protection against common web vulnerabilities, secure password handling, and user authentication.
- Scalability: Django's scalability features, such as caching, database optimization, and support for distributed systems, allow applications to handle high loads efficiently.
- Community and ecosystem: Django has a large, active community that contributes to its continuous development. It also has a rich ecosystem of reusable packages and libraries.
- Versatile and Flexible: Django is a versatile framework that can be used to build a wide range of web applications. Whether it's a content management system (CMS), e-commerce platform, social media application, or a complex web application, Django provides a flexible foundation to handle diverse requirements.
Install the EPEL Repository
1) All the methods here will depend on the EPEL repository for CentOS and RedHat-like distributions. The EPEL repository has extra packages which are not maintained as part of the core distribution, which is fairly sparse.
Configuring access to the EPEL repository is fairly simple. On the server, you need to configure yum
to use the EPEL repository by typing the below command:
sudo yum install epel-release
Now, you can access all applications within the EPEL repository.
Having Different Methods
There are multiple ways in which you can install Django based on your needs and configure the development environment.
- Global Install from the Packages: The conventional
yum
package manager can be used to install Django packages. This method is simple to follow, but not as flexible as the other methods. There might also be some version differences when you take it directly from the repository. - Global Install via pip: The
pip
tool is a package manager used for Python packages. Ifpip
is installed, it will become very easy to install Django on the system level. It should always contain the latest stable release. - Install through pip in a virtualenv: The Python
virtualenv
package creates self-contained environments for various projects. It enables you to install Django in a project directory, without even affecting the main system. It will allow you to provide per-project customizations and packages easily. They add a slight mental and process overhead in comparison with globally accessible installations, but they provide the most flexibility. - Development Version Install via git: Get the code from the
git
repo if you want to install the latest development version of the stable release. It is very important to get the latest features/fixes. Moreover, it can be done either globally or locally.
Now, you can select the installation method that best suits your needs. Next, continue with the below steps.
Global Installation from Packages
1) Here, you can simply use the yum
package manager. It will download and install the necessary packages:
sudo yum install python3-django -y
2) You will now test that the installation was successful. Do it by typing:
django-admin --version
2.2.24
It means that the software installation is successful. Even the Django version is not the latest stable.
Global Installation via pip
1) If you want to install the latest version of Django globally. It is a better option to use pip
. First, you will need to install the pip
package manager.
2) You can install the pip
package manager from the EPEL repositories using the following command:
sudo yum install python3-pip
3) After having pip
, you can easily install Django globally by typing:
pip3 install django
4) Verify if the installation is successful by typing:
django-admin --version
2.2.24
You will see, the version available via pip. It is more up-to-date than the one from the EPEL repository.
Installing from pip in a virtualenv
1) Installing Django using virtualenv
tool is the most flexible way. It allows you to install any Python package without affecting the rest of the system. This allows the selection of the Python packages on a per-project basis. Further, it is regardless of conflicts with other project's needs. Now, begin by installing pip
from the EPEL repository using the following command:
sudo yum install python3-pip
2) After installation of pip
, you will use it to install virtualenv
package by typing:
sudo pip3 install virtualenv
3) Now, whenever you will start a new project, it will enable you to create a virtual environment for it. Start by creating and moving into a new project directory:
mkdir ~/newproject
cd ~/newproject
4) Now, create a virtual environment within the project directory by typing:
virtualenv newenv
5) It will install a standalone version of Python, as well as pip
into an isolated directory structure within the project directory. This virtual environment is called as newenv
. You should name it something descriptive. Next, a directory will be created with the name you will select. So, it will hold a file hierarchy where the installation of your package is.
To install packages into the isolated environment. You can activate it by typing:
source newenv/bin/activate
6) Your prompt will change to reflect, that you are now in your virtual environment. It will look something like (newenv)username@hostname:~/newproject$
.
Now use pip
to install Django. Remember not to use sudo
as you are installing it locally:
pip install django
7) You will now verify the installation by:
django-admin --version
3.2.20
8) If you want to leave your virtual environment, you need to issue the deactivate
command from anywhere on the system:
deactivate
9) Your prompt will now revert to the conventional display. So, to work on the project again, re-activate the virtual environment. Do it by moving back into the project directory and activating:
cd ~/newproject
source newenv/bin/activate
Development Version Installation via git
1) If you want a development version of Django then download and install it from its git
repository, But make sure you have python-3.10
or a greater version installed.
You need to install git
on your system with yum
. Further, install pip
Python package manager. It will also handle the installation of Django after it is downloaded:
sudo yum install git python3-pip
2) Once git
is installed, then clone the Django repository. This repository will have more up-to-date features and bug fixes at the possible expense of stability. You can clone the repository to a directory django-dev
within the home directory using the below command:
git clone https://github.com/django/django ~/django-dev
3) After cloning the repository, install it using pip
. Use the -e
option to install in "editable" mode. It is necessary when installing from version control:
sudo python3 -m pip install -e .
4) You can verify if the installation is successful by:
django-admin --version
4.1.dev20150305200340
Please note that you can use virtualenv
if you want to install a development version of Django in a single environment.
Creating a Sample Project
1) Now, after the installation of Django, you can get started with the project.
You can use django-admin
command to create a project:
django-admin startproject projectname
cd projectname
2) It will create a directory called projectname
within the current directory. Within this, it will also create a management script. Further, another directory called projectname
is created with the actual code.
django-admin startproject projectname .
3) To bootstrap the database on more recent versions of Django, type the following command:
python3 manage.py migrate
4) You are using an older version of Django and if the migrate
command is not working, then use the below command.
python3 manage.py syncdb
5) You will be prompted to create an administrative user as part of the process. Select a username, an email address, and also a password for the user.
6) If you are using the migrate
command, you will need to create the administrative user manually. Create the user by typing:
python3 manage.py createsuperuser
7) You will get a prompt for a username, an email address, and a password for the user. After you have a user, you will start up the Django development server and see how a fresh Django project looks like. Make sure to use this only for development purposes.
Now, before running the server, we need to add our server IP to ALLOWED_HOSTS in projectname/projectname/setting.py
in single quotes.
...
ALLOWED_HOSTS = ['<IP>']
...
Then run the server.
python3 manage.py runserver 0.0.0.0:8000
8) After that, visit your server’s IP address followed by :8000
in your web browser:
server_ip_address:8000
You will see something like below:
9) Next, append /admin
to the end of your URL to get to the admin login page:
server_ip_address:8000/admin
10) You will now enter the admin username as well as the password created. It will then direct you to the admin section of the site:
11) After finishing looking via the default site, you can stop the development server now. Do it by typing CTRL-C
in your terminal. After the creation of the Django project, it will provide the structural basis for designing a more customized site.
FAQs to Install Django on CentOS 8
What Python version to use with Django?
Django 1.11 is the last version to support Python 2.7. Support for both Python 2.7, and Django 1.11, ends in 2020. Therefore, the latest versions of Python are often faster and have more features. They give better support.
Should the user use the stable version or the development version?
Mainly, if using code in production, you will be using a stable release. Django project publishes a full stable release every 9 months or so. It is with many bug fix updates now in between.
What are the prerequisites for installing Django on CentOS 8?
To install Django on CentOS 8, you need a CentOS 8 server or machine with Python already installed. It is recommended to use Python 3.x as Django works best with the latest Python versions.
What is the recommended method to install Django on CentOS 8?
The recommended method to install Django on CentOS 8 is by using the pip
package manager, which is the standard tool for installing Python packages.
Why should I install Django on CentOS 8?
Installing Django on CentOS 8 allows you to utilize the features and benefits of the framework when developing web applications.
Can I install Django using the CentOS package manager?
CentOS's package manager (dnf) may have Django available, but it might not provide the latest version.It is recommended to use pip
to install Django directly from the Python Package Index (PyPI) to obtain the most up-to-date version.
Can Django be used with different databases?
Yes, Django provides support for various databases, including PostgreSQL, MySQL, SQLite, Oracle, and others.
Conclusion
We hope this detailed tutorial helped you to install Django on CentOS 8.
If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.