fbpx

If you’re a Linux user and want to dive into Django development, this step-by-step guide will walk you through the installation of Django on Linux process. Django is a powerful and popular web framework for building robust and scalable web applications. By the end, you’ll have Django up and running on your Linux machine, ready to create amazing web applications.

Section 1: Preparing Your Linux Environment

Before installing Django on Linux, let’s ensure your Linux environment is ready. Follow these steps to set up the necessary dependencies:

Update Your System:

To begin, open a terminal and run the following command to update your system:

sudo apt update && sudo apt upgrade

Install Python:

Django on Linux is built with Python, so make sure it’s installed on your Linux machine. Run the following command to install Python:

sudo apt install python3

Install Pip:

Pip is a package manager for Python, which we’ll need to install Django and its dependencies. Execute the following command to install Pip:

sudo apt install python3-pip

Section 2: Installing Django on Linux

Now that your Linux environment is prepared, let’s move on to installing Django on Linux itself. Follow these steps:

Create a Virtual Environment:

A virtual environment isolates your Django project’s dependencies from your system’s Python installation. Run the following command to create a virtual environment named “myenv”:

apt install python3.10-venv
python3 -m venv myenv

Activate the Virtual Environment:

To activate the virtual environment, run the appropriate command based on your shell:

For Bash/Zsh: source myenv/bin/activate

For Fish: . myenv/bin/activate.fish

For Csh/Tcsh: source myenv/bin/activate.csh

Install Django on Linux:

Now that your virtual environment is active, use Pip to install Django:

pip install django
How to Install Django on Linux

Verify the Installation:

To ensure Django on Linux is successfully installed, run the following command to check the version:

python -m django --version
4.2.1

If the version is displayed, congratulations! Django on Linux is installed correctly.

Section 3: Creating Your First Django Project

With Django installed, it’s time to create your first project. Follow these steps:

Start a New Django Project:

Run the following command to create a new Django project named “virtono”:

django-admin startproject virtono

Navigate to the Project Directory:

Move into the project directory by executing the command:

cd virtono

Run the Development Server:

Start the Django development server with the following command:

python manage.py migrate
python manage.py runserver 0.0.0.0:8000
How to Install Django on Linux

Access Your Django Application:

Open a web browser and visit http://you-server-IP:8000/. If you see the default Django landing page, your application is running successfully.

Access Your Django on Linux Application

Section 4: Setting up a Database

Django supports various databases, and you can choose the one that suits your project requirements. Here’s an example of setting up a PostgreSQL database:

Install PostgreSQL:

Run the following command to install PostgreSQL:

sudo apt install postgresql

Create a Database:

Next, create a new database for your Django project. Execute the following commands:

sudo su - postgres
createdb mydatabase

Create a new user:

Once you are in the PostgreSQL command-line interface, run the following command to create a new user:

sudo -u postgres psql
CREATE USER your_username WITH PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE your_database_name TO your_username;

Update Django Settings:

In your Django project directory, open the settings.py file and locate the DATABASES section. Update it as follows:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'postgres',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '',
    }
}

Migrate the Database:

To apply the database changes, run the following command:

source myenv/bin/activate
pip install psycopg2-binary
sudo apt-get install libpq-dev
python manage.py migrate

Section 5: Additional Django Configuration

Now that you have Django installed and your project set up, let’s cover some additional configuration steps:

Creating a Superuser:

A superuser account allows you to access the Django administration interface. Run the following command and follow the prompts to create a superuser:

python manage.py createsuperuser

Modifying Allowed Hosts:

By default, Django only allows requests from localhost. To allow requests from your domain or IP address, open the settings.py file and locate the ALLOWED_HOSTS list. Add your domain or IP address to the list, like this:

ALLOWED_HOSTS = ['yourdomain.com', 'your_ip_address']

Final thoughts

Congratulations! You have successfully installed Django on your Linux machine, created your first Django project, set up a database, and performed additional configurations. You’re now equipped to embark on your Django development journey. Remember to explore the vast Django documentation and engage in the vibrant Django community to enhance your skills and build exceptional web applications.


0 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.