Tutorials > How to install the Django framework on CentOS 8

How to install the Django framework on CentOS 8

Published on: 27 January 2021

CentOS Development Python

Introduction

Django is a powerful framework for developing web applications in Python. Using such a feature-rich framework will allow you to build and run your applications faster, ensuring good organization and code structuring.

The framework also allows you to focus only on the unique aspects of your application, leaving the most complex development parts to Django's tools.

In this guide, various methods to install Django on a Linux CentOS 8 server will be shown. After the installation, you will see how to create a new project to be used as the basis for a website.

First, connect to your server via an SSH connection. If you haven't done so yet, following our guide is recommended to connect securely with the SSH protocol. In case of a local server, go to the next step and open the terminal of your server.

Installing the EPEL library

All the installation methods below are based on the EPEL library for CentOS and RedHat distributions. The EPEL repository contains extra packages that can be added to the main distribution, which turns out to be quite minimal.

To configure your server to use the EPEL library, just enter the command:

$ sudo yum install epel-release

The applications linked to the EPEL release can now be used.

Different ways of installing Django

There are different installation methods for Django, depending on your different needs and how you would like to configure your development environment. Choosing the most suitable way to install Django will facilitate operations in specific situations of your interest.

Listed below, are some of the main Django installation methods:

  • Global installation from packages: the EPEL repository contains Django packages that can be installed with the conventional yum package manager. It is the simplest but less flexible installation, and it also causes some lags with the official version of the project.
  • Global installation via pip: The pip tool is a package manager for Python. By installing it, Django can be easily installed and made available for all users of the system. The version that will be installed will probably be the last one verified as stable. Even in this way, global installations are still less flexible than others.
  • Installation with pip in Virtualenv (virtual environment) : The Phyton virtualenv package allows you to create self-contained environments for various projects. Using this technology, it is possible to install Django in a project directory without affecting the larger system. This will allow you to independently customize each project and its packages. Virtual environments require some more efforts compared to the normal global installation, but provide more flexibility.
  • Installing the development version via git: To install the latest development version instead of the most stable one, acquire the source code from git repository. This is necessary to get the latest features and fixes, and can be done both globally and locally. However, note that the development versions do not guarantee the same stability as the "stable" versions.

At this point, choose the type of installation that best meets your needs. In the following paragraphs you will be explained in detail all the methods presented.

Global installation from packages

To install Django via the EPEL repository, the procedure is very simple. Use the yum package manager to download and install the packages of your interest, using the command:

$ sudo yum install python3-django

To verify that the installation was successful ask the system for the Django version that is currently present in the system, using the command:

django-admin --version

1.6.10

If you get a version number as an output, it means that the installation has been successfully completed. It is also possible to check if the installed version is the latest or not.

Global installation via pip

To install the latest version of Django globally, the best option is to use pip, the Python package manager.

Start by installing pip via the EPEL repository:

$ sudo yum install python3-pip

After the installation pip , switch to Django:

$ sudo pip3 install django

To verify that the installation was successful, use the command:

django-admin --version
1.7.5

The number shown as output will indicate the installed release.

As you can see, the version available via pip will be more up-to-date than the one installed via the EPEL repository.

Installing via pip in a Virtualenv (virtual environment)

Probably the most flexible way to install Django on your system is to use the virtualenv tool. This tool will allow you to create virtual Python environments where any package can be installed without affecting the rest of the system.

This flexibility will allow you to choose, for each project, the Python packages to install, with the advantage of being able to customize the different installations without conflicts.

Then, start with the installation of pip from the EPEL repository:

$ sudo yum install python3-pip

Once installed use the new package manager to install virtualenv :

$ sudo pip3 install virtualenv

From now on, every time you want to create a new project, you will need to define a virtual machine or environment. Start by creating and moving into a new project directory:

$ mkdir ~/newproject
$ cd ~/newproject

Now, create a new virtual environment in the new defined directory:

$ virtualenv newenv

This creation will install a new standalone version of Python, as well as pip , into a directory isolated from your project directory.

NB The names new project and new environment have been included for illustrative purposes only. However, different names can be used. Thanks to the chosen names, it is possible to identify a specific directory and its hierarchy.

To proceed with the installation of packages within the isolated environment, first activate it via the command:

$ source newenv/bin/activate

Your command prompt should now change, indicating that you are inside the selected virtual environment. The console should be similar to the following:

(newenv)[email protected]:~/newproject$

In the new environment, then, install the Django framework via pip . There is also no need to use the sudo command, because the changes will be applied locally and not within the system.

$ pip3 install django

Verify that the installation was successful by using the command:

$ django-admin --version
1.7.5

The numeric output displayed will indicate the version of Django installed.

To exit the virtual environment, issue the deactivate command via the prompt.

$ deactivate

The prompt should return to normal information, without specifying the virtual environment. To return to the virtual environment, reactivate it by accessing the project directory.

To do so, simply enter the commands in sequence:

cd ~/newproject
source newenv/bin/activate

Installing the development version via git

If a development version of Django is needed, download and install it from its git repository .

To do so, the first step is to install git on your system via yum . Install also pip , the Python package manager, through which you will manage the Django installation after the download.

To begin, enter the command:

$ sudo yum install git python3-pip

Once git , clone the Django repository. Among the different releases, this repository will have the most updated features, albeit at the expense of stability, being a development version.

Clone the repository into a directory called django-dev , which can be placed in the home directory. Use the command:

$ git clone git://github.com/django/django ~/django-dev

Now that the repository has been cloned, install Django using pip . Use the -e option to install it in custom mode:

$ sudo pip3 install -e ~/django-dev

Verify that the installation was successful by issuing the command:

$ django-admin --version
1.9.dev20150305200340

Again, the string generated as the command output will indicate the version of Django currently installed.

NB By combining this method with that of virtualenv , it will be possible to install the development version of Django on a single virtual environment.

Creating a project with Django

At this point of the guide, Django should already be installed and your first project can now be created.

Use the django-admin command to create a new project:

$ django-admin startproject myproject
cd myproject

This command pair will create a directory with the selected name within the root directory. Later, an administration script and another directory with the same name will also be created.


Note: if you were already inside a project directory, created via the virtualenv command, Django can be told to insert the script and the internal directory inside the one already created, without the extra level, by entering the command:

$ django-admin startproject project name. 

Don't skip the final step!


To bootstrap the database (by default in SQLite) on latest versions of Django, write:

$ python manage.py migrate

If the migrate command doesn't work, it means that an older version of Django is being used. The alternative command will be:

$ python manage.py syncdb

To complete the process, in this case, you will be requested to register an admin user by entering various information in the prompt. It will request your username, password and email.

If the migrate command was used, create the admin user manually instead. To do so, enter:

$ python manage.py createsuperuser

As in the other case, you will be requested some information to register the user. These will be: username, password and email.

Once you've finished creating it, start the Django development server to see what a new Django project looks like. Run:

$ python manage.py runserver 0.0.0.0:8000

Visit your server's IP address now, followed by the string : 8000 in your web browser:

server_ip:8000 

To access the administration section, simply concatenate the string / admin at the end of the URL. Here's an example of what it should look like:

server_ip:8000/admin

On the login page, simply enter the credentials specified above to access the administration section of the site.

At the end of the use of the site, just press the CTRL + C combination in the terminal to stop the development server.

Conclusions

Thanks to this guide you learned how to successfully install the Django framework on your CentOS 8 server and how to create a new project and launch the developer server. 

The Django project you have created will form the structural basis for designing a more complex website. Check the Django documentation for more information on how to build applications and customize your site.

Relying on the Django web framework will allow you to focus on the unique aspects of your application, speeding up the other aspects of development.