Django is a free web framework open source code written in high level language – Python, designed to help developers to rapidly establish secure, scalable, and maintainable web applications.
Installing Django Ubuntu can be accomplished in two ways. It can be installed directly in the operating system or virtual Python environment using pip. The Django package is included in official repository of Ubuntu and can be installed using the package Manager apt. This is the easiest way to install Django in Ubuntu 18.04, but it is not as flexible as installing using pip. In addition, the version included in the repository always lags the latest version of Django.
The main purpose of virtual Python environments — creating multiple environments for different projects. Thus, you can get several different environments of Django on a single computer and install the appropriate version of module for each project without worrying that it will affect other installations of Django. If you are installing Django in the global environment, you can install only one version of a web framework on your computer.
Installing Django on Ubuntu 18.04
Next, the article presents step-by-step instructions for installing Django in a virtual environment Python on Ubuntu 18.04. First, consider the installation of Python 3 and the virtual environment venv. Ubuntu 18.04 3.6 comes with Python by default. You can verify that Python 3 installed on your system by typing:
The output should look like this:
Starting with Python 3.6, the recommended method of creating a virtual environment is to use the venv module. To install the package python3-venv, run the following command:
sudo apt install python3-venv
After installing the module you need to create a virtual environment for our Django application. Navigate to the directory where you want to store your virtual environments Python 3. It can be your home directory or any other directory where your user has permissions to read and write.
Create a new directory for your Django application and go into it:
Being in the created directory, run the following command to create a new virtual environment:
python3 -m venv venv
The above command creates a directory named venv, which contains a copy of the binary Python package Manager Pip, a standard Python libraries, and other supporting files. You can use any name for the virtual environment. To start using this virtual environment, you need to activate it by running the activation script:
After activating the bin directory of the virtual environment is added to the beginning of the $PATH variable. In addition, the prompt will change to show the name of the virtual environment that you are using at the moment. In our case venv.
Now that the virtual environment is activated, you can use the package Manager pip to install Django:
pip install django
In a virtual environment you can use pip instead of pip3 and python3 instead of python. To make sure that the installation went successfully, use the following command which displays the version of Django:
python-m django --version
At the time of this writing, the latest official version of Django was 2.1.2. Your version of Django may differ from the version shown here.
Creation of Django project
To create a new Django project with the name mydjangoapp, use the utility command-line django-admin:
django-admin startproject mydjangoapp
This command creates a directory mydjangoapp in your current directory.
Inside that directory is the main script for project management with the name manage.py and the directory that includes the database configuration, and configure Django apps. Let’s move the database and create administrator. Start by navigating to the directory mydjangoapp:
By default Django uses SQLite database. You can use other databases, e.g. PostgreSQL, MariaDB, Oracle, or MySQL Database. Run the following command to migrate the database:
python manage.py migrate
After you move the database, create a user with administrator rights, so you can use the Django admin interface:
python manage.py createsuperuser
The command will prompt you for a user name, email address and password.
Launch the web development server using a script manage.py followed runserver option:
python manage.py runserver
You will see the following output of the command:
If you installed Django on the virtual machine and want to access Django development server, you need to edit the file settings.py and add the IP address of the server to the list of ALLOWED_HOSTS.
Open a web browser and type in the address bar http://127.0.0.1:8000 and you should see the Django default:
To access the admin interface of Django, you need to add / admin / to the end of the URL (http://127.0.0.1:8000/admin/). This will open the login screen of the administrator:
To get to the administration page of Django, enter the user name and password:
To stop the server, type CTRL-C in your terminal.
Disable the virtual environment
At the end of work you want to deactivate your virtual Python environment for this type the command deactivate, and you will return to your normal shell.
You learned how to create a virtual Python environment and install Django in Ubuntu 18.04. To create additional development environment Django, repeat the steps in this guide. If you’re new to Django, go to Django documentation and learn how to build your first Django application.