Before we can install the latest python version, we need to install some python dependencies:<\/p>\n
apt install software-properties-common -y\r\n\r\nsudo add-apt-repository ppa:deadsnakes\/ppa -y\r\n\r\napt update\r\n<\/pre>\nOnce the python dependencies are installed, we can install the latest python3.10 version with the command below:<\/p>\n
sudo apt install python3.10 -y<\/pre>\nOnce installed, we can check the full python version:<\/p>\n
python3 --version<\/pre>\nYou should receive the following output:<\/p>\n
root@host:~# python3 --version\r\nPython 3.10.4<\/pre>\nNow, when the latest version of Python is installed, we can install the Python Virtual Environment required for Django Web Framework to run smoothly and isolated from other libraries on the server.<\/p>\n
To install Python Virtual Environment execute the command below:<\/p>\n
sudo apt install python3-pip python3.10-venv -y<\/pre>\n<\/span>Step 3. Install Django Web Framework<\/span><\/h2>\nNow, we can create a python virtual environment and install the Django framework. To create and activate a virtual environment in some directory on your server, follow the steps below:<\/p>\n
cd \/opt\r\n\r\npython3 -m venv django-venv\r\n\r\nsource django-venv\/bin\/activate\r\n<\/pre>\nOnce the virtual environment is created and activated, you should see the following output:<\/p>\n
root@vps:\/opt# python3 -m venv django-venv\r\nroot@vps:\/opt# source django-venv\/bin\/activate\r\n(django-venv) root@vps:\/opt#\r\n<\/pre>\nTo install Django Admin in the virtual environment, execute the command below:<\/p>\n
pip3 install django<\/pre>\nTo verify that the installation is successful and the Django admin version executes the following command:<\/p>\n
django-admin --version<\/pre>\nYou should receive the following output:<\/p>\n
(django-venv) root@vps:\/opt# django-admin --version\r\n4.0.5<\/pre>\n<\/span>Step 4. Create Django Project<\/span><\/h2>\nNow, when the virtual environment is created, we can create a new project using the Django-admin with the start project command as described below:<\/p>\n
django-admin startproject djangoproject .<\/pre>\nOnce the Django project is created, list the directory with the ls -al<\/b> command, and you should see the following output:<\/p>\n(django-venv) root@vps:\/opt# ls -al\r\ntotal 20\r\ndrwxr-xr-x 4 root root 4096 Jun 18 13:02 .\r\ndrwxr-xr-x 19 root root 4096 Jun 5 11:14 ..\r\ndrwxr-xr-x 2 root root 4096 Jun 18 13:02 djangoproject\r\ndrwxr-xr-x 5 root root 4096 Jun 18 09:25 django-venv\r\n-rwxr-xr-x 1 root root 669 Jun 18 13:02 manage.py\r\n<\/pre>\n<\/span>Step 5. Migrate Django Database<\/span><\/h2>\nThe next step is to migrate the Django database using the command below:<\/p>\n
python3 manage.py migrate<\/pre>\nAfter successful migration you should receive the following output:<\/p>\n
(django-venv) root@vps:\/opt# python3 manage.py migrate\r\nOperations to perform:\r\n Apply all migrations: admin, auth, contenttypes, sessions\r\nRunning migrations:\r\n Applying contenttypes.0001_initial... OK\r\n Applying auth.0001_initial... OK\r\n Applying admin.0001_initial... OK\r\n Applying admin.0002_logentry_remove_auto_add... OK\r\n Applying admin.0003_logentry_add_action_flag_choices... OK\r\n Applying contenttypes.0002_remove_content_type_name... OK\r\n Applying auth.0002_alter_permission_name_max_length... OK\r\n Applying auth.0003_alter_user_email_max_length... OK\r\n Applying auth.0004_alter_user_username_opts... OK\r\n Applying auth.0005_alter_user_last_login_null... OK\r\n Applying auth.0006_require_contenttypes_0002... OK\r\n Applying auth.0007_alter_validators_add_error_messages... OK\r\n Applying auth.0008_alter_user_username_max_length... OK\r\n Applying auth.0009_alter_user_last_name_max_length... OK\r\n Applying auth.0010_alter_group_name_max_length... OK\r\n Applying auth.0011_update_proxy_permissions... OK\r\n Applying auth.0012_alter_user_first_name_max_length... OK\r\n Applying sessions.0001_initial... OK<\/pre>\n