How To Install PIP3 and Django On Ubuntu 20.04 / 19.10

We can install PIP3 and configure Django on the ubuntu machine. Let’s see, how to install PIP on ubuntu. In a similar way, you can also install and configure Django on ubuntu 18.04 as well as 16.04.

How To Install PIP3 and Django On Ubuntu 20.04 / 19.10

Before installing, we need to check the python version installed on the ubuntu machine.

ubuntu@ubuntu:~$ sudo python
Python 2.7.18rc1 (default, Apr 7 2020, 12:05:55) 
[GCC 9.3.0] on linux2

Check Out: How To Install Jenkins Server On Ubuntu 20.04 / 19.10

1. Right now, I have python 2.7 version on the machine and I need to install the python 3. You can run the following commands to install it. 

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
wget https://www.python.org/ftp/python/3.7.3/Python-3.7.3.tar.xz
tar -zxf Python-3.7.3.tar.xz
cd Python-3.7.3/
sudo ./configure --enable-optimization
sudo make 
sudo make altinstall
python3 --version

2. Install pip3 using the below command and check the version. 

sudo apt install python3-pip
sudo pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)

We’ve installed python3 and pip3 using the above commands.

Install Django On Ubuntu:

We can use global installation using the below command. if you want to install for a particular user, use this sudo pip3 install –user django command.

ubuntu@ubuntu:~$ sudo pip3 install Django
Collecting Django
Downloading Django-3.1-py3-none-any.whl (7.8 MB)
|████████████████████████████████| 7.8 MB 105 kB/s 
Collecting asgiref~=3.2.10
Downloading asgiref-3.2.10-py3-none-any.whl (19 kB)
Collecting sqlparse>=0.2.2
Downloading sqlparse-0.3.1-py2.py3-none-any.whl (40 kB)
|████████████████████████████████| 40 kB 745 kB/s 
Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from Django) (2019.3)
Installing collected packages: asgiref, sqlparse, Django
Successfully installed Django-3.1 asgiref-3.2.10 sqlparse-0.3.1

Once the installation is finished, it will give you the django-admin command to manage the project.

ubuntu@ubuntu:~$ sudo which django-admin
/usr/local/bin/django-admin

Set the Django path using the below command. Open the .bashrc file.

sudo vim ~/.bashrc

Check Out: How To Enable Root Login On Ubuntu Focal 20.04 / 18.04

Add the below line and save the file.

export PATH=$PATH:/usr/local/bin/django-admin

Run the below command.

source ~/.bashrc

You can use the below command to check the Django version. 

ubuntu@ubuntu:~$ django-admin --version
3.1

Create a Django Project and Test:

Once you have successfully installed the Django framework on the ubuntu machine. Install the Virtualenv using the below command. 

sudo pip install virtualenv

Or

sudo pip3 install virtualenv

Check Out: How To Enable Automatic Grammar And Spelling Check In LibreOffice

Create a directory and go to the project directory and now activate the virtual environment.

mkdir djangoproject
cd djangoproject

Create a  virtualenv for your project using the below command.

ubuntu@ubuntu:~/djangoproject$ virtualenv newenv
created virtual environment CPython3.8.2.final.0-64 in 1560ms
  creator CPython3Posix(dest=/home/ubuntu/djangoproject/newenv, clear=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/ubuntu/.local/share/virtualenv)
    added seed packages: pip==20.2.1, setuptools==49.2.1, wheel==0.34.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

To install the packages in the isolated environment, you must need to active it.

ubuntu@ubuntu:~/djangoproject$ source newenv/bin/activate

Now we will install the Django 3 using the below commands. 

ubuntu@ubuntu:~/djangoproject$ sudo pip3 install django==3.1
Collecting django==3.1
  Using cached Django-3.1-py3-none-any.whl (7.8 MB)
Requirement already satisfied: pytz in /usr/lib/python3/dist-packages (from django==3.1) (2019.3)
Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.8/dist-packages (from django==3.1) (0.3.1)
Requirement already satisfied: asgiref~=3.2.10 in /usr/local/lib/python3.8/dist-packages (from django==3.1) (3.2.10)
Installing collected packages: django
  Attempting uninstall: django
    Found existing installation: Django 3.0
    Uninstalling Django-3.0:
      Successfully uninstalled Django-3.0
Successfully installed django-3.1

Check the version using

ubuntu@ubuntu:~/djangoproject$django-admin --version
3.1

To leave your environment, we need to deactivate it. you can run this command anywhere on the system.

ubuntu@ubuntu:~/djangoproject$ deactivate

We need to create a project “test_djan” using the django-admin command.

ubuntu@ubuntu:~/djangoproject$ django-admin startproject test_djan

We have started a Django project as you can see above. we will allow the server IP in the settings.py file. Look for ALLOWED_HOSTS and put your server IP address then save the file. 

ubuntu@ubuntu:~/djangoproject/test_djan$ vim test_djan/settings.py
ALLOWED_HOSTS = ["192.168.185.147"]

Check Out: How To Install and configure Haproxy Load Balancer On Linux

The next step is to migrate the database for our project. By default, Django uses the SQLite database. Use the below command to migrate the database. 

ubuntu@ubuntu:~/djangoproject/test_djan$ python3 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 auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK

The default SQLite database has been migrated.

ubuntu@ubuntu:~/djangoproject/test_djan$ ls
db.sqlite3 manage.py test_djan

We need to create an admin user for your project using the below command. 

 ubuntu@ubuntu:~/djangoproject/test_djan$ python3 manage.py createsuperuser
Username (leave blank to use 'ubuntu'): admin
Email address: it@gmail.com
Password: 
Password (again): 
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

Once the admin user is created, you need to start the server using the below commands.

ubuntu@ubuntu:~/djangoproject/test_djan$ python3 manage.py runserver 0.0.0.0:8000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 10, 2020 - 20:58:53
Django version 3.1, using settings 'test_djan.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Open the browser and hit the URL  http://IP:8000 and you will see the home page. 

If you want to open the admin, use the URL http://IP:8000/admin 

django install ubuntu

Now you can see the default admin dashboard

install and configure django

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment