How To Install Django on Linux Mint 21
In this tutorial, we will show you how to install Django on Linux Mint 21. Django is a powerful and popular web framework that enables developers to create robust, scalable, and secure web applications quickly. Built using the Python programming language, Django follows the model-template-view (MTV) architectural pattern, which streamlines the development process and promotes code reusability. With its extensive set of features, Django simplifies common web development tasks, such as URL routing, database management, authentication, and more.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step installation of the Django web application framework on Linux Mint 21.
Prerequisites
- A server running one of the following operating systems: Linux Mint 21.
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- While we’ll guide you through the process, a basic understanding of the command line will be beneficial. If you’re new to the CLI, you might want to acquaint yourself with some fundamental commands.
- An active internet connection.
- Administrative privileges are essential for installing and configuring software on your system. Ensure that you have superuser or sudo access.
Install Django on Linux Mint 21
Step 1. Before proceeding with the installation of Django, it’s crucial to ensure that your Linux Mint 21 system is up to date. Updating the system packages helps in maintaining a secure and stable environment, and it also resolves any potential compatibility issues that may arise during the installation process. To update your Linux Mint packages, open a terminal and run the following commands:
sudo apt update sudo apt upgrade
Step 2. Installing Python.
Django is a Python web framework, so having Python installed on your system is a fundamental requirement. Linux Mint 21 typically comes with Python 3 pre-installed, but it’s always a good practice to verify its presence and install it if necessary. To check if Python 3 is already installed on your Linux Mint 21 system, open a terminal and run the following command:
python3 --version
If Python 3 is not found, you can easily install it using the following command:
sudo apt install python3
This command will download and install the latest version of Python 3 from the Linux Mint repositories. Once the installation is complete, you can verify the installation by running python3 --version
again.
Step 3: Installing Pip and Venv.
To install Django and manage its dependencies, we will use pip, the standard package installer for Python. Additionally, we will install the venv package, which allows us to create isolated Python virtual environments for our Django projects. First, let’s check if pip is already installed for Python 3 by running the following command in the terminal:
pip3 --version
If pip is installed, you will see the version number displayed in the terminal output. If pip is not found, you can install it by running the following command:
sudo apt install python3-pip
With pip and venv installed, you now have the necessary tools to install Django and manage its dependencies within a virtual environment, ensuring a clean and conflict-free development setup.
Step 4. Create a Python Virtual Environment.
Using Python virtual environments is a best practice when working with Django projects. Virtual environments allow you to create isolated Python environments for each project, ensuring that dependencies and packages used in one project do not interfere with those used in another. This helps maintain a clean and organized development setup, avoiding potential conflicts between different projects’ requirements. To create a Python virtual environment for your Django project, follow these steps:
mkdir djangoproject
Navigate into the newly created directory using the cd command:
cd djangoproject
Create a new virtual environment within the project directory by running the following command:
python3 -m venv env
Activate the virtual environment by running the following command:
source env/bin/activate
This command activates the virtual environment, and you will notice that your terminal prompt changes to indicate that you are now working within the virtual environment. It will typically look something like this:
(env) user@localhost:~/djangoproject$
The “(env)” prefix indicates that the virtual environment named “env” is currently active.
Step 5. Installing Django in the Virtual Environment.
Now that you have created and activated a Python virtual environment, you can proceed with installing Django within that environment. Installing Django in a virtual environment ensures that it is isolated from other Python projects on your system, preventing potential conflicts and maintaining a clean development setup.
To install Django, make sure that your virtual environment is active (you should see the “(env)” prefix in your terminal prompt). Then, run the following command:
pip install django
This command uses pip, the Python package installer, to download and install the latest stable version of Django and its dependencies within the active virtual environment.
If your project requires a specific version of Django, you can specify the version number when installing it. For example, to install Django 5.0.4, you would run:
pip install django==5.0.4
The installation process may take a few moments as pip downloads and installs Django and its dependencies. Once the installation is complete, you can verify that Django is installed correctly by running the following command:
django-admin --version
Step 6. Create a New Django Project.
Now that you have Django installed in your virtual environment, you can create a new Django project. A Django project is a collection of settings, configurations, and applications that together form a complete web application.
To create a new Django project, use the django-admin startproject command followed by the desired project name. For example, let’s create a project named “mysite”:
django-admin startproject mysite
This command will generate a new directory named “mysite” in your current location, which will contain the project’s files and directories. The project structure will look like this:
mysite/ manage.py mysite/ __init__.py settings.py urls.py asgi.py wsgi.py
Within a Django project, you can create one or more Django apps. An app is a self-contained module that encapsulates a specific functionality of your web application. To create a new app within your project, navigate into the project directory and run the following command:
cd mysite python manage.py startapp myapp
To integrate the newly created app into your project, you need to add it to the INSTALLED_APPS list in the project’s settings.py file. Open mysite/settings.py
and locate the INSTALLED_APPS list. Add your app’s name to the list, like this:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', # Add your app's name here ]
With your Django project created and your app integrated, you are ready to start building your web application’s functionality.
Step 7. Set Up a Database.
Django uses a database to store and retrieve data for your web application. By default, Django is configured to use SQLite, which is a lightweight and file-based database engine. SQLite is suitable for development and testing purposes, as it doesn’t require any additional setup or configuration.
If you are using SQLite for your Django project, you don’t need to perform any extra steps to set up the database. Django will automatically create the necessary database file when you run the database migrations.
However, for production environments or when you need more advanced database features, you may want to use a more robust database system like PostgreSQL, MySQL, or Oracle. Django provides built-in support for these databases, and you can easily switch to them by modifying the database settings in your project’s settings.py file.
To use a different database system, you’ll need to install the appropriate database bindings. For example, to use PostgreSQL with Django, you can install the psycopg2 package using pip:
pip install psycopg2
Similarly, for MySQL, you can install the mysqlclient package:
pip install mysqlclient
After installing the required database bindings, you need to update the DATABASES setting in your project’s settings.py file to specify the database connection details. Here’s an example configuration for PostgreSQL:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_database_name', 'USER': 'your_username', 'PASSWORD': 'your_password', 'HOST': 'localhost', 'PORT': '5432', } }
Once you have configured the database settings, you need to run the database migrations to create the necessary tables and schema in your database. To do this, navigate to your project’s root directory (where the manage.py file is located) and run the following command:
python manage.py migrate
This command will apply the initial database migrations, creating the required tables for Django’s built-in apps and any additional migrations defined in your project’s apps.
Step 8. Create a Superuser.
Django provides a built-in administration interface that allows you to manage your project’s data, users, and other resources through a web-based interface. To access the Django admin, you need to create a superuser account.
To create a superuser, navigate to your project’s root directory (where the manage.py file is located) and run the following command:
python manage.py createsuperuser
This command will prompt you to enter a username, email address, and password for the superuser account. Follow the prompts and provide the necessary information.
Username: admin Email address: admin@example.com Password: ******** Password (again): ******** Superuser created successfully.
Once the superuser is created, you can access the Django admin interface by starting the development server (which we will cover in the next step) and navigating to the /admin/ URL in your web browser. For example, if your development server is running at http://127.0.0.1:8000/
, you can access the admin interface at http://127.0.0.1:8000/admin/
.
Congratulations! You have successfully installed Django. Thanks for using this tutorial to install the latest version of the Django web application framework on the Linux Mint system. For additional help or useful information, we recommend you check the official Django website.