LinuxWizardry
  • Home
  • Tutorials
  • News
  • Ubuntu
  • Centos
  • Tools
No Result
View All Result
LinuxWizardry
  • Home
  • Tutorials
  • News
  • Ubuntu
  • Centos
  • Tools
No Result
View All Result
LinuxWizardry
No Result
View All Result

How to Install and Set up a Django Web Framework on Ubuntu 22.04 LTS

How to Install and Set Up a Django Web Framework on Ubuntu 22.04 LTS

by Rohan
July 8, 2022
in Tutorials
ShareTweetShareShare

Django is a free and open-source Python web framework. With Django, you can build dynamic web applications written in Python easier and faster. It offers a wide range of features for creating better Python-based web applications.

It is a popular full-stack framework known for its security, development speed, and scalability. Some popular websites built with Django are YouTube, Instagram, Spotify, DropBox, Pinterest, Mozilla Firefox, BitBucket, etc.

This tutorial will explain how to install the Django web framework on Ubuntu 22.04 system. We will also discuss how you can create and run a simple Django application.

Prerequisites

For this tutorial, you will need access to the sudo privileges on the Ubuntu machine. We will be using Ubuntu 22.04 for installing Django. If you need to set up Ubuntu 22.04, you can follow our tutorial on how to upgrade to Ubuntu 22.04 LTS.

There are different methods for installing Django in Ubuntu. Some of them include:

  • Install Django from Ubuntu repositories
  • Installing Django from the Git repository
  • Using pip command to install Django

Let’s explore all the installation methods and set up the Django framework on Ubuntu 22.04 LTS.

Install Django from Official Ubuntu Repositories

Django is available on the official package repositories of Ubuntu. One of the simplest methods is to use the apt package manager tool and get Django from the Ubuntu official repositories.

First, you will need to update the package index of your system using the following command.

$ sudo apt update

Check which Python version is installed on your system with this command.

$ python3 -V

Output:

[email protected]:~$ python3 -V
Python 3.10.4

Then run the apt command below to install Django.

$ sudo apt install python3-django

apt command to install django

You can verify whether the installation is successful by checking the Django version. This command prints the Django version on the system.

$ django-admin --version

If it returns the version, you have successfully installed Django on your system.

Output:

[email protected]:~$ django-admin --version
3.2.12

The above output confirms we have installed the Django version 3.2.12. The current LTS release of Django is 3.2, which will be supported until April 2024.

Install Django from the Official Django Repository

You can install the development version of Django using its GitHub repository. Follow the steps below to clone Django’s git repository and get Django on your system.

First, you have to update the package index to the latest version.

$ sudo apt update

Check the python version installed on your system.

$ python3 -V

You will need a pip which is a package manager tool for python. You can install pip tool using the apt command.

$ sudo apt install python3-pip

apt command to install pip on ubuntu

Next, clone the Django git repository to the directory django-setup in the home directory.

$ git clone https://github.com/django/django ~/django-setup

git clone django on ubuntu

Then change the current directory to django-setup and run this command to install Django.

$ pip install -e ~/django-setup

pip install packages from the Python Package Index. Once the installation is completed, verify it by entering the command below.

$ django-admin --version

 

Install Django in the Python Virtual Environment

You can also install Django in the virtual Python environment instead of the system. It allows you to use Django for a specific project without affecting other files or projects on the system.

First, you have to install the virtual environment package.

$ sudo apt install python3-venv

apt command to install python virtual environment on ubuntu

Next, create a new directory where you want to create a virtual environment and change it to the current working directory.

$ mkdir myproject
$ cd myproject

The following command creates a new virtual environment myenv. After creating the environment, you have to activate it before using it.

$ python3 -m venv myenv

This command activates the Python virtual environment.

$ source myenv/bin/activate

See the username in the terminal to confirm whether you are in the virtual environment. It should look like (myenv)[email protected]:~.

create new python virtual environment on ubuntu

You can use pip command to install Django in your virtual environment.

$ pip install django

pip command to install django on ubuntu

After completing the installation, check the Django version with this command.

$ django-admin --version

Output:

(myenv) [email protected]:~/myproject$ django-admin --version
4.0.5

To exit from the virtual environment, you can type deactivate command.

$ deactivate

You must activate your virtual environment before running the Django application.

Create a Sample Django Project

You should have successfully installed Django on the system. Now, let’s discuss how you can build a Django project and test it on the local server.

We will create a new Django project myfirstproject in the previously built virtual environment myenv.

First, activate the virtual environment.

 $ source myenv/bin/activate

Next, run the following command to create a new Django project mydjango.

$ django-admin startproject myproject .

Then you have to migrate the database using the command below.

$ python manage.py migrate

Django uses the SQLite database by default.
create a django project on ubuntu

After the migrations process is completed, create an administrative account for using the Django admin interface. The following command creates a new admin user for the Django project.

$ python manage.py createsuperuser

It will prompt you to enter the new user’s username, email, and password. After submitting all values, the admin user will be created successfully. If you leave blank in the username field, the current user’s login name is taken.

create a admin user on django ubuntu

Now, let’s run our newly created Django project. You can use the following command to start the Python Django development server.

$ python manage.py runserver

run python django server on ubuntu

The above output shows that the Django project is now up and running on port 8000. You can visit http://127.0.0.1:8000/ on your web browser to view the site.

If you get similar output as below, you have successfully created the Django web application. It is the default home page of Django.

django install worked successfully
You can access the admin panel by adding the admin path to the site URL.

http://127.0.0.1:8000/admin

django administration login page

Type the username and password of the admin user you created before and press Log in button.

django site administration admin dashboard

To stop the Django server, enter Ctrl + C on the terminal.

 

Conclusion

In this tutorial, you have learned different methods to install the Django web framework on Ubuntu operating system. We also discussed how to create the Django project and launch it on the local server. It is easier to create web applications more quickly with Django.

We hope this article is helpful for you to understand how to install Django on Ubuntu 22.04 LTS. If you have any questions or feedback, please let us know in the comment section.

ShareTweetShareShare
Previous Post

How to Configuring an SSH Login without Password on Ubuntu 22

Next Post

How to Manage Users and Groups on Ubuntu 22.04

Next Post
How to Upgrade Ubuntu 20.04 LTS to Ubuntu 22.04 LTS Jammy Jellyfish

How to Manage Users and Groups on Ubuntu 22.04

Discussion about this post

Copyright © 2022 LinuxWizardry.com

No Result
View All Result
  • #682 (no title)

Copyright © 2022 LinuxWizardry.com

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In