HomeBlogWeb DevelopmentHow to Install Django on Ubuntu? Step-by-Step Guide

How to Install Django on Ubuntu? Step-by-Step Guide

Published
27th May, 2024
Views
view count loader
Read it in
6 Mins
In this article
    How to Install Django on Ubuntu? Step-by-Step Guide

    Django is a Python web framework that allows you to create interactive websites and applications. You can easily build Python web applications with Django and rely on the framework to do a lot of the heavy lifting for you. And Ubuntu provides an environment that is secure and stable with extensive Python and its dependencies support, making it an ideal platform for Django development.

    If you want to install Django on Ubuntu, it involves using the package manager, pip, to easily acquire and manage Django and its dependencies. This streamlined installation process, combined with Ubuntu's strong compatibility with Python development tools, makes it easier for Django setup in Ubuntu, allowing developers to focus on coding rather than configuring the development environment. 

    Furthermore, Ubuntu's vibrant community and comprehensive documentation contribute to a seamless experience by providing Django developers with resources, tutorials, and troubleshooting assistance. Django installation on Ubuntu allows developers to harness the power of a robust framework in a dependable and well-supported environment, allowing for efficient web application development. Using Django within Ubuntu provides numerous benefits. 

    This tutorial will show you how to install Django on Ubuntu 18.04 server. After installation, you'll create a new project to serve as the foundation for your website.

    What is Django? 

    Django is a high-level, open-source Python web framework that promotes rapid development and clean, pragmatic design. It adheres to the model-template-views (MTV) architectural pattern, emphasizing component reusability, the "Don't Repeat Yourself" (DRY) philosophy, and the promotion of efficient, maintainable online development. Django supports four main databases (PostgreSQL, MariaDB, MySQL, Oracle and SQLite) and community libraries support other popular SQL and NoSQL databases at various levels. 

    Django setup in Ubuntu makes it easier to create online applications by providing a rich collection of tools and functions, such as an ORM (Object-Relational Mapping) system for database interaction, a powerful URL routing system, an easy-to-use template engine, and an administration interface built in. Developers like this framework because of its scalability, security features, and vast ecosystem of reusable components known as "Django apps,", which can be created by Django install in Ubuntu and are readily connected to the existing application and increase its functionality. Get Enrolled in a Django online course.

    Install Django on Ubuntu: Different Methods

    Depending on your needs and how you want to set up your development environment, there are different ways to install Django on Ubuntu. These have various advantages and any one method might accommodate you better than others.

    Some of the various methods are:

    1. Global install from packages: Django packages can be installed with the conventional apt package manager in the official repositories in Ubuntu. This is straightforward, but not so flexible. The version contained in the repositories may also be lagging behind the official project versions.
    2. Install with pip in a virtual environment: Using tools like venv and virtualenv, you can create an independent environment for your projects. A virtual environment enables you to install Django in the project directory, along with other project customizations and packages. This approach to work with Django is normally the most practical and recommended.
    3. Development version install with git: Instead of stable release, you can get the code from the Git repo if you want to install the latest development version. This needs to be done in your virtual environment and to obtain the most current features/fixes. However, development models do not have the same guarantees of stability as more stable versions.

    Prerequisites to Install Django for Ubuntu  

    Django Installation Hardware RequirementsDjango Installation Software Requirements
    CPU: 1 GHz or greaterPython
    RAM: min 1 GB of RAMPackage Manager
    Storage: 5-10 GB of free disk spacePython Virtual Environment

    Hardware Requirements

    • CPU: A modern CPU with a clock speed of at least 1 GHz or greater.
    • RAM: A minimum of 1 GB of RAM is required, but 2 GB or more is recommended for improved performance, especially when working with large apps.
    • Storage: At least 5-10 GB of free disk space is necessary to install Python, Django, and related packages.

    Software Requirements

    • Web Browser: Any browser such as Google Chrome, Mozilla Firefox, Microsoft Edge.
    • Operating System: An Ubuntu 18.04 server installed with a non-root sudo user and firewall.

    How to Install Django in Ubuntu? Step-by-Step

    Here is a step by step procedure to install Django on Ubuntu: Let's start

    A. Global Install from Packages

    The process is very simple if you want to install Django using the Ubuntu repositories.

    Step 1: update your local package index with apt:

    $ sudo apt update

    Step 2: Now check the version of python you have installed in your Ubuntu server. You can check it by using:

    $ python3 –V

    Step 3: Now, you can install Django on your system:

    $ sudo apt install python3-django

    Step 4: You can check the installation with:

    $ django-admin –version

    Output:

    This results in a successful installation of the software. You may also find that the version of Django is not the latest stable version.

    B. Install with pip in a Virtual Environment

    Django can be installed flexibly and easily on your system within a virtual environment. We'll demonstrate how to instal Django in a virtual environment that will be created in the standard Python 3 library with the venv module. With this tool, you can create virtual Python environment without affecting the rest of the system, and install python packages. Thus, regardless of conflict with the requirements of other projects, you can select Python packages on a project basis.

    1. First refresh the local package index:

    $ sudo apt update

    2. Check the installed python version:

    $ python3 -V 

    3. Now, use Ubuntu repositories to install pip:

    $ sudo apt install python3-pip

    4. Once the pip has been installed, the venv package can be installed:

    $ sudo apt install python3-venv

    Step 5: Now, you can create a virtual environment for it whenever you start a new project. Begin with creating a new project directory and move it to a new project directory:

    $ mkdir ~/newpProject
    $ cd ~/newProject

    Step 6: Next, create a virtual environment for your Python version in the project directory using the python command. We'll call my_env as our virtual environment, but you should call it descriptive:

    $ python3.6 - venv my_env

    Step 7: This will install standalone python versions and pip in your project directory into an isolated directory structure. A directory with the name you specify will be created to hold the file hierarchy where your packages will be installed.

    You can install packages into the isolated environment by:

    $ source my_env/bin/activate

    Step 8: Your prompt should update to show that your virtual environment is now in place. It will look something like (my env)username@hostname:~/newProject$.

    You can use pip to install Django in your new environment. Whatever version of your Python, when you are in your virtual environment you should only call pip. Also note that since you install locally, you won't have to use sudo:

    (my_env) $  pip install django

    Step 9: You can verify the installation by:

    (my_env) $  django-admin –version

    Note that the version shown here may differ.

    Step 10: You need to issue the deactivate command from anywhere on the system to get out of your virtual environment:

    (my_env) $ deactivate

    Step 11: The conventional display should return to your prompt. If you want to work on your project again, reactivate your virtual environment by returning to your project directory and activating:

    $ cd ~/newProject
    $ source my_env/bin/activate

    3. Development Version Install with Git

    You can download and install Django from its Git repository if you need a development version of Django. Let's do this in a virtual environment.

    Step 1: First update the local package index:

    $ sudo apt update

    Step 2: Check the installed python version:

    $ python3 -V

    Step 3: Now, use Ubuntu repositories to install pip:

    $ sudo apt install python3-pip

    Step 4: Once the pip has been installed, the venv package can be installed:

    $ sudo apt install python3-venv

    Step 5: The next step is to clone the repository of Django. This repository will have more up-to-date features and bug corrections at the possible cost of stability between releases. In your home directory, you can clone the repository into the directory ~/django-install using:

    $  git clone git://github.com/django/django ~/django-install

    Step 6: Change the directory:

    $  cd ~/django-install

    Step 7: Next, create a virtual environment for your Python version in the project directory using the python command.

    $ python3.6 -m venv my_env

    Step 8: You can install packages into the isolated environment by:

    $  source my_env/bin/activate

    Step 9: The repository can next be installed with a pip. In "editable" mode, the -e option is installed and required for installation from version control:

    (my_env)  $ pip install -e ~/django-install

    Step 10: Verify the installation by:

    $  django-admin –version

    How to Create a Sample Django Project? 

    You can start building your project with Django. We will discuss how to create a project and test it using a virtual environment on your development server.

    Step 1: Start by creating a directory for the project and switch to it.

    $ mkdir ~/django-demoproject
    $ cd ~/django-demoproject

    Step 2: Now, create a virtual environment:

    $ python3.6 -m venv my_env

    Step 3: Activate:

    $ source my_env/bin/activate

    Step 4: Install Django on Ubuntu:

    $ pip install django

    Step 5: You can use the django-admin command to build your project. with startproject. You could replace this with another name; however, we'll call our project djangoproject. In your existing working directory, startproject will create a directory containing:

    • manage.py management script that you can use to manage different tasks in Django.
    • A directory (which contains the actual project code) with the same name.
    • However, let us tell Django to place the management script and inner directory into the current directory to prevent too many nesting directories:
    (my-env) $ django-admin startproject

    Step 6: To migrate the database, using the manage.py command to migrate. Migration applies any changes to your database scheme that you have made to your Django models.

    To migrate the database use:

    (my_env) $ python manage.py migrate

    Output:

    Step 7: Let us create an admin user to use the admin interface of Django. Let's do this with the  createsuperuser command:

    (my_env) $ python manage.py createsuperuser

    Testing the Development Server in Django

    Step 1:  You can start the development server in Django to see what a new project looks like. You should use it exclusively for development. Be sure to follow Django's deployment guidelines if you are ready to deploy.

    We will use the created folder and navigate into it.

    $ mkdir ~/django-sample
    $ cd ~/django-sample

    Step 2: You can use the django-admin command to build your project.

    (my-env) $ django-admin startproject djangoproject

    Step 3: We can use the manage.py and the runserver command from this folder, as shown.

    $ python3 manage.py runserver

    $ python manage.py migrate

    Step 4: After the server running, you can view the site through your browser by navigating to the following URL: http://127.0.0.1:8000/ : A site that looks like this should be visible:

    Step 5: You can add /admin/ to the end to access the admin interface:

    You will see:

    When the default site is finished, the development server can be stopped by typing CTRL-C into your terminal.

    How to Uninstall Django?

    Method 1: How to remove Django from Ubuntu using pip?

    To uninstall Django from your Ubuntu system, you can also use pip, the Python package manager, to delete the Django package. If you have followed the steps to install Django on Ubuntu globally, you should execute the following command with administrative privileges:

    $ sudo pip uninstall django

    If you have followed the steps to install Django on Ubuntu in a virtual environment, make sure it is enabled before using the pip command without sudo. 

    Step 1: First activate the virtual environment. (Replace path_to_your_virtualenv with the actual path to the directory containing your virtual environment). 

    source path_to_your_virtualenv/bin/activate 

    Step 2: Execute the pip command. 

    pip uninstall django 

    Method 2: How to remove Django from Ubuntu using the official repository?

    To uninstall Django from the official Ubuntu repository, follow these steps:

    Step 1: Uninstall Django. 

    If you have followed the steps to install Django on Ubuntu using the Ubuntu package manager (apt), you can uninstall it with the following command: 

    $ sudo apt-get remove python-django

    Step 2: Cleanup Process. 

    Run the following command to remove any remaining Django configuration files or dependencies: 

    sudo apt autoremove  

    Learn more about the Python and Django in depth with the Web Development with Python and Django Course.

    Are you ready to unlock the power of Python? Join our online Python Developer Course and become a coding maestro. Start your journey today!

    Conclusion

    In this detailed article, we have discussed various approaches you can use to install Django on Ubuntu, providing flexibility to users with varying preferences. Starting with the official Ubuntu repository, we learned the simple procedure of utilizing apt commands to ensure a stable installation directly from Ubuntu's developed packages. 

    We went deeper into the Git repositories, which gave us access to the most recent Django developments. We used pip and venv to construct isolated environments, encouraging clean management of projects and flexibility in Django version control. The installation of the development version directly from GitHub gave us access to cutting-edge capabilities, which were great for testing and contributing to Django's constant developments. 

    The conclusion of this article emphasizes Django's adaptability and accessibility for web development and how to install Django on Ubuntu. Users can benefit from Django's robustness whether they want stability through official repositories or embrace the latest advancements from GitHub. The approaches described here ensure a smooth transition into web development with Django's strong toolkit within the Ubuntu ecosystem. Master web development by enrolling in KnowledgeHut's Django online course. 

    Frequently Asked Questions (FAQs)

    1What version of Django is compatible with Ubuntu?

    Django 4.2.7 is the most recent version that works with Ubuntu 22.04 and 20.04. While earlier iterations of Django might also work with certain Ubuntu versions, the most recent version, 4.2.7, is recommended. This list of latest Django versions compatible with different Ubuntu versions can be used to properly install Django on Ubuntu. 

    • Ubuntu Compatible Django 
    • Version Version 
    • 22.04, Django 4.2.7 
    • 20.04, Django 4.2.7 
    • 18.04, Django 3.2.13 
    • 16.04, Django 2.2.11 
    2What is the recommended method for installing Django on Ubuntu?

    It is recommended to utilize pip in a virtual environment to install Django on Ubuntu. By doing this, you may keep your Django project separate from the Python installation on your system and avoid problems with other Python packages. 

    3Is it necessary to create a virtual environment before installing Django on Ubuntu?

    Yes, creating a virtual environment is recommended if you plan to install Django on Ubuntu, or any Python project in general. A virtual environment is a separate Python environment that allows you to install packages without impacting the Python installation on your system. This is important since it may help in avoiding the occurrence of conflicts across versions of the same package. Furthermore, virtual environments make it simple to manage your project's dependencies. 

    4Are there any known issues or common errors during Django installation on Ubuntu?

    Yes, there are a few known concerns and common errors to be aware of while performing steps to install Django on Ubuntu. These are some examples: 

    Permission errors: You might run into permission errors if you are not using sudo when running the installation commands. You have two options to resolve this: either use sudo to execute the commands or modify the file ownership to your user. 

    Dependency errors: Before the steps to install Django on Ubuntu have been performed, a number of dependencies must be installed. An error will appear if any of these dependencies are not present. You can use pip to install the missing dependencies and fix this. 

    Apart from these two, there can also be virtual environment issues as well as socket timeout errors. 

    Profile

    Bala Krishna Ragala

    Blog Author

    Bala Krishna Ragala, Head of Engineering at upGrad, is a seasoned writer and captivating storyteller. With a background in EdTech, E-commerce, and LXP, he excels in building B2C and B2B products at scale. With over 15 years of experience in the industry, Bala has held key roles as CTO/Co-Founder at O2Labs and Head of Business (Web Technologies) at Zeolearn LLC. His passion for learning, sharing, and teaching is evident through his extensive training and mentoring endeavors, where he has delivered over 80 online and 50+ onsite trainings. Bala's strengths as a trainer lie in his extensive knowledge of software applications, excellent communication skills, and engaging presentation style.

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon