Django is the most popular Python web framework designed to build feature-filled web applications. Using it you can develop secure, scalable and maintainable dynamic web applications. Django can be installed in a Python virtual environment using pip. The classic thing to use Python Virtual Environment is that you can create multiple different Django Environments on a single machine without affecting other Django projects.
If you install Django into the global environment then you can install only one Django version on your computer. In this tutorial, we will show you how to install django on Ubuntu 18.04 machine.
Before you start installing Django on Ubuntu 18.04, you should logged in using non-root user account on your system with sudo privileges.
Installing Django on Ubuntu 18.04
Django packages are available in the official Ubuntu repositories. It’s easiest way to install using the
apt package manager but not flexible for installation in a virtual environment.
Installing Python 3 and venv
Python 3.6 comes along with Ubuntu 18.04. So you can verify using below command that Python 3 is installed:
The output should as given below:
Creating virtual environment using
venv module is the best and recommended way. The
venv module is included in the
python3-venv package. To install it type the following command:
sudo apt install python3-venv
You can create virtual environment for Django once the installation finished.
Create Virtual Environment
First of all create a new directory and navigate in to it. It can be your home directory or any other directory where your user has read and write permissions.
Run the below command to create a new directory for your Django application and navigate into it:
mkdir django_app && cd django_app
Now, execute the following command to create a new virtual environment:
python3 -m venv venv
The above command will create a directory named
venv, which includes Python binary, the standard Python library, Pip package manager and other supporting files.
To start using the virtual environment, you should activate it by running the
Now your path will change and it will show the name of your virtual environment (
Now your virtual environment is activated and you can install Django using the Python package manager
pip install django
You can confirm the installation and check the installed version typing using below command:
python -m django --version
Your Django version may be different from the version shown here.
Creating a Django Project
Create a Django project named
django-admin command-line utility.
django-admin startproject djangoapp
The above command will create a
djangoapp directory in your current directory.
You can check the directory structure using the following command:
mydjangoapp/ |-- manage.py |-- mydjangoapp |-- __init__.py |-- settings.py |-- urls.py |-- wsgi.py
This directory contain
manage.py file which manage the project and other Django specific files about database configuration settings, routes, and settings.
Next, navigate to the
After that you should migrate the database using below command:
python manage.py migrate
Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations: Applying contenttypes.0001_initial… OK Applying auth.0001_initial… OK Applying admin.0001_initial… OK Applying admin.0002_logentry_remove_auto_add… OK Applying admin.0003_logentry_add_action_flag_choices… OK Applying contenttypes.0002_remove_content_type_name… OK Applying auth.0002_alter_permission_name_max_length… OK Applying auth.0003_alter_user_email_max_length… OK Applying auth.0004_alter_user_username_opts… OK Applying auth.0005_alter_user_last_login_null… OK Applying auth.0006_require_contenttypes_0002… OK Applying auth.0007_alter_validators_add_error_messages… OK Applying auth.0008_alter_user_username_max_length… OK Applying auth.0009_alter_user_last_name_max_length… OK Applying sessions.0001_initial… OK
Once the migrate process is complete you should create a administrative user account to access the Django admin interface:
python manage.py createsuperuser
This command will prompt you for username, email and a password for your user.
Username (leave blank to use 'tecnstuff'): admin Email address: firstname.lastname@example.org Password: Password (again): Superuser created successfully.
Testing the development server
Launch the development server using the
manage.py script as given below:
python manage.py runserver
It will show output as below:
Performing system checks… System check identified no issues (0 silenced). July 18, 2019 - 18:49:23 Django version 2.1.4, using settings 'djangoapp.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
http://127.0.0.1:8000 in your web browser and it will show you the default Django landing page:
You can access admin interface direct by visiting
Enter your username and password which you set at the administrative user create time and you will be redirect to Django admin page:
You can stop the development server by pressing
Ctrl+C in terminal.
Deactivate the Virtual Environment
When you want to stop work you can
deactivate the virtual environment by typing deactivate:
You have learned how to create a Python virtual environment and install Django on your Ubuntu 18.04 system. To create additional Django development environments repeat the steps given in this tutorial. If you want to learn more about Django, visit the Django documentation page.
If you have any questions or suggestion, please leave a comment below.