Quick Django Installation Script
Published: 2024-06-14 10:50:06

I work with [Django projects](https://stacknatic.com/blog/why-i-switched-to-headless-django-with-nextjs) frequently, and often find myself repeating the same commands during Django installation. 

To streamline the initialization of Django projects, I created an installation script for easy setup, which I’m sharing in this post. The script is a bash script designed to work seamlessly on Ubuntu 22 and should also be compatible with other versions. If you are using MacOs, you can simply remove the part that begins with '# Update package list and install dependencies' up until the next 'fi' line or comment out the block. While it is not strictly necessary to update your system before installing Django, it is generally a good practice to ensure your system is up to date.

What the script does:
    1. It checks for system updates. If updates are available, it will attempt to update your system.
    2. Creates a virtual environment for Django. If you’re new to Python development or the ecosystem, virtual environments are useful for isolating your project’s dependencies from other Python projects on your system. This ensures that any packages or libraries required by your Django project are installed and managed separately, avoiding conflicts and keeping your environment clean and organized.
    3. Installs Django.
    4. Starts the Django application, which you can access at http://127.0.0.1:8000 in your web browser.

```
#!/bin/bash

# Function to check the last command status
check_command_success() {
    if [ $? -ne 0 ]; then
        echo "Error: $1"
        exit 1
    fi
}

# Prompt for the desired Django project name
read -p "Enter the name of your Django project [default: my_django_project]: " DJANGO_PROJECT_NAME
DJANGO_PROJECT_NAME=${DJANGO_PROJECT_NAME:-my_django_project}

# Update package list and install dependencies
echo "Updating package list..."
sudo apt update
check_command_success "Failed to update package list."

# update the system if there are updates
updates_available=$(apt list --upgradable 2>/dev/null | grep -v 'Listing...' | wc -l)

if [ "$updates_available" -gt 0 ]; then
    echo "Upgrading system..."
    sudo apt upgrade -y
    check_command_success "Failed to upgrade system."
else
    echo "System is already up to date. No upgrades available."
fi

echo "Installing pip, virtualenv, git, and PostgreSQL..."
sudo apt install -y python3-pip python3-venv
check_command_success "Failed to install necessary packages."

# Create a new directory for the Django project if it does not exist
if [ ! -d "~/$DJANGO_PROJECT_NAME" ]; then
    echo "Creating a directory for the Django project..."
    mkdir -p ~/$DJANGO_PROJECT_NAME
    check_command_success "Failed to create Django project directory."
fi

cd ~/$DJANGO_PROJECT_NAME
check_command_success "Failed to navigate to Django project directory."

# Set up a virtual environment if it does not exist
if [ ! -d "venv" ]; then
    echo "Setting up a virtual environment..."
    python3 -m venv venv
    check_command_success "Failed to create virtual environment."
fi

source venv/bin/activate
check_command_success "Failed to activate virtual environment."

# Install Django if not already installed
if ! pip show django > /dev/null 2>&1; then
    echo "Installing Django..."
    pip install django
    check_command_success "Failed to install Django."
fi

# Start a new Django project if it does not exist
if [ ! -f "manage.py" ]; then
    echo "Starting a new Django project named $DJANGO_PROJECT_NAME..."
    django-admin startproject $DJANGO_PROJECT_NAME .
    check_command_success "Failed to start Django project."
fi

# Start the Django development server
echo "Starting the Django development server..."
python manage.py runserver
check_command_success "Failed to start Django development server."

echo "Django installation and setup complete. Visit http://127.0.0.1:8000 to see your site."

```