Django is a robust Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of the hassle of Web development. Using a full-featured framework like Django allows you to get your applications and sites up and to run quickly without worrying about the standard structural code to tie them together.

It is open-source software that lets you focus on the individual portions of the applications while the in-built tools will take care of the heavy lifting. Django’s primary goal is to ease the creation of complex applications and takes care of the internal structures.

This tutorial will go through various methods of installing Django on the Ubuntu 18.04 server. Throughout this guide, we will cover three main ways of installing Django.

Django installation methods

There are multiple ways to install and set up Django on your Ubuntu server. Each method will make a different configuration in your development environment with unique advantages. Here are the three ways that we will discuss in this blog post:

  1. Global installation from packages: You can install Django easily from the official Ubuntu repository. This is very simple but might not be as flexible as other methods. One more thing to note is that the repository might not contain the most recent version of Django.
  2. Installation through PIP in a virtual environment: The Python venv package allows you to create self-contained environments for various projects. Using this technology, you can install Django in a project directory without affecting the whole system. This type of installation will provide you with the most flexibility. This is preferred by most because it is the most practical and recommended approach to working with Django.
  3. Installation from git: If you wish to install the latest development version instead of the stable release, you will have to acquire the code from the git repository. Development versions do not have the same stability, but you can get the latest features and fixes. 

Now let’s get to the installation process.

Prerequisites

Before we get started, you will need the following:

  • A Ubuntu 18.04 server (VPS)
  • A basic understanding of Linux commands 
  • A root user or a user with Sudo privileges

1.   Global installation from packages

Installing Django from the Ubuntu local repository is straightforward. You can use the apt command to download and install the relevant packages. However, it’s best to update the local package list:

sudo apt update

Now we need to check which Python version we have installed on the system. By default, Ubuntu comes with Python 3.6.

python3 -V

If you see an output as “Python 3.6.5”, move on to the next step.

Now we will go ahead and install Django:

sudo apt install python3-django

Once the installation is completed, you can test if everything was set successfully by entering the following command:

django-admin --version

You will see an output with the Django version that was installed.

2.   Install using pip in a virtual environment

We believe the most flexible way to install Django is in a virtual environment. We will be using the venv module, which will be used to create virtual Python environments where you can install Python packages without affecting the rest of the system. This will allow you to select different Python packages depending on the project, which will not conflict with other projects running on the system.

First, let’s update the system:

sudo apt update

Now we will check the Python version we have on the system. We will need to note this down for further steps.

python3 –V

If you see an output as “Python 3.6.5”, move on to the next step.

Let's go ahead and install pip from the Ubuntu repository.

sudo apt install python3-pip

Next, we will install the venv package:

sudo apt install python3-venv

Now, whenever you start a new project, you can create a virtual environment for it. Let’s start this by creating a new directory and entering it (Replace a newproject with your project's name).  

mkdir ~/newproject

cd ~/newproject

The next step is to create a virtual environment in the newproject directory using the python command compatible with your system’s version. We will use testenv as the virtual environment name, but be sure to change this to your one:

python3.6 -m venv testenv

This will install a standalone version of Python and pip into an isolated directory structure within your project directory. To install packages into the isolated environment, we must activate it first. To do that, enter the following:

source testenv/bin/activate

Once you enter this command, your shell prompt will change to something like this: (testenv)username@hostname:~/newproject$

Now we can go ahead and install Django. Here you won’t need to use Sudo since you will be installing it locally:

pip install django

To verify the installation, type in the following:

django-admin --version

You will see an output with the Django version that was installed.

If you want to exit your virtual environment, enter the command anywhere in the system.

deactivate

If you exited successfully, your prompt should revert to the conventional display. Reactivate the virtual environment by moving into the project directory when you wish to work on your project.

cd ~/newproject

source testenv/bin/activate

3.   Installation through the git repository

If you want to install the development version of Django, you can download it directly from its git repository. We will do this through a virtual environment.

First, as a best practice, we will update the local package list:

sudo apt update

Check the python version installed on the system:

python3 –V

If you see an output as “Python 3.6.5”, move on to the next step.

Let’s go ahead and install pip from the Ubuntu repository.

sudo apt install python3-pip

Next, we will install the venv package:

sudo apt install python3-venv

We have everything ready to start cloning the Django repository. This repository will have more up-to-date software with a multitude of features but probably with the expense of stability. You can clone the repository to a directory called django-dev within your home directory by typing:

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

Enter the newly created directory:

cd ~/django-dev

Create a virtual environment using the python command that’s compatible with your installed version of Python:

python3.6 -m venv testenv

To install packages into the isolated environment, we must activate it first. To do that, enter the following:

source testenv/bin/activate

Once the repository is cloned, let’s go ahead and install it using the pip command. We will use the –e option to install it in editable mode.

pip install -e ~/django-dev

You can verify that the installation was successful by typing:

django-admin --version

You will see an output with the Django dev version that was installed.

Create a sample Django Project

Now that we have Django installed on the system let’s go and try to create a sample Django project on your Ubuntu server. You can create a new Django application through the command line. Navigate to the directory where you wish to create the new application and enter the following commands. We will create a directory called tst1 in the working directory and store all the necessary files.

django-admin startproject tst1

cd tst1

 To migrate the database, type in:

python3 manage.py migrate

Now you will need to create an administrative user for Django applications. This will be done manually using the following command:

python3 manage.py createsuperuser

You will be prompted to enter a username, an email address and a password for the user. Once the user is set in place, we can start a Django development server to see what a new Django project looks like in the browser. However, Django does not allow external hosts to access the web interface. We will edit the settings.py file and add the server IP address under ALLOWED_HOSTS to fix this.

Use the nano text editor to access the settings file:

nano django_app/settings.py

Add your server IP as follows:

ALLOWED_HOSTS = ['your_server_IP']

Save and exit the text editor. Now run the following command to run the Django application server:

python3 manage.py runserver 0.0.0.0:8000

The Django application is running successfully now. Now go to your browser and enter your server IP address as shown below:

server_ip_address:8000

If you see something like this, congratulations! You have successfully created a Django project.

Now we can go ahead and view the admin side of things by making a small change to the URL as shown below:

server_ip_address:8000/admin

If you enter the admin username and password you just created, you should be taken to the site’s admin section. Once you finish working on the site, go to your terminal and press CTRL + C to quit the application.

The Django project you’ve created provides the structural basis for designing a complete site. Check out the Django documentation for more information about how to build your applications and customize your site.

Conclusion

Congratulations on making it to the end of this article. We have successfully covered the installation process of Django on Ubuntu 18.04 server. Django provides the essential tools needed to create and manage powerful web applications, streamlining the development process. Additionally, we walked through creating a sample project and launching the developer server from the web browser.

If you're looking to further enhance your development environment, consider using an Ubuntu VPS and RDP server for Ubuntu. These services can provide you with a robust and secure platform to host your Django applications, offering greater control and scalability as your projects grow. Proper integration of Django’s complete web framework, combined with the power of an Ubuntu VPS, makes development faster and allows you to focus more on your applications' unique aspects.

People also read: