Kura Zetu Setup Instructions

This guide will walk you through setting up the KuraZetu project, from cloning the repository to configuring the database and installing dependencies.

Important

Before you start, please note that this project is in its early stages and is not yet ready for production use. It is intended for development and testing purposes only.

There are two ways to run the project: using Docker or setting it up locally. This guide will focus on the local setup. For Docker setup, refer to the Docker Setup Guide.

Prerequisites

Ensure you have the following installed:

  • Python (>= 3.10)

  • Ubuntu 24.04 (NB: You can use MacOS with a few tweaks around Postgres setup)

  • pip

  • PostgreSQL (>= 14) with PostGIS extension

  • Git

Clone the Repository

Warning

Ensure you are using an anonymous GitHub account to clone the repository. This is crucial for your safety and privacy !

Start here: how to create an anonymous GitHub account.

git clone https://github.com/shamash92/KuraZetu.git
cd KuraZetu

Setup Instructions Frontend (React + Webpack)

Important

Running the frontend does not open any ports e.g 3000. It only enables webpack to build a bundle on code change and hot reload to help with development. The bundle is injected inside a django template and django serves all the consecutive react-router links under the /ui/ paths.

Open a new terminal to run the following commands parallel to the backend commands

  1. Navigate to the frontend directory:

    cd src/ui
    
  2. Install Nodejs and Yarn

    Ensure you have Nodejs (>= 20) and Yarn installed on your system. You can install them using the following commands:

     sudo apt update
     curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
     sudo apt install -y nodejs
    
  3. Install Yarn globally

    sudo npm install -g yarn
    
  4. Install frontend dependencies:

    yarn install
    
  5. Copy the example .env.sample file to .env:

    cp .env.sample .env
    
  6. Run the frontend development server:

    yarn run dev
    

2. Setup Instructions (Django Backend)

Important

This is supposed to be done in a separate terminal from the frontend setup. The backend will run on port 8000 and the frontend will run on watch mode

Install Dependencies

Create a virtual environment and install the required Python packages:

  • Navigate to the project directory:

    cd src/
    sudo apt update
    sudo apt install python3-venv python3-pip
    
    python -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    pre-commit install
    

Install System Dependencies

Before proceeding, install the required system dependencies for PostgreSQL, PostGIS, and GeoDjango: NB: This project is built and tested with Postgres 14.

sudo apt update

sudo apt install -y wget gnupg lsb-release ca-certificates

# Import PostgreSQL signing key
wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg

# Add PostgreSQL 14 repository
echo "deb [signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | \
  sudo tee /etc/apt/sources.list.d/pgdg.list

sudo apt update

# Install PostgreSQL 14, PostGIS, and related packages
sudo apt install -y postgresql-14 postgresql-client-14 postgresql-14-postgis-3 postgresql-14-postgis-3-scripts libpq-dev

Enable and start Postgres

sudo systemctl enable postgresql
sudo systemctl start postgresql

GeoDjango / GDAL dependencies

sudo apt-get install software-properties-common
sudo apt-add-repository ppa:ubuntugis/ubuntugis-unstable
sudo apt-get update
sudo apt-get install libgdal-dev

sudo apt-get install binutils libproj-dev gdal-bin

3. Configure Environment Variables

Copy the example .env.local file to .env:

cp .env.local .env

Edit the .env file to configure your environment-specific settings. e.g.

# Database settings
DATABASE_NAME=kurazetu_db
DATABASE_USER=kurazetu_user
DATABASE_PASSWORD=your_password
DATABASE_HOST=localhost
DATABASE_PORT=5432

ALLOWED_HOSTS= http://localhost:8000, 127.0.0.1, localhost, 10.0.2.2, multipass-ipv4-address
CORS_ALLOWED_ORIGINS=http://localhost:8000, http://<your-multipass-ipv4-address>

# Other environment variables

4. Set Up PostgreSQL and PostGIS

  1. Log in to PostgreSQL:

    sudo -u postgres psql
    
  2. Create a database:

    CREATE DATABASE kurazetu_db;
    

    NB: You can use any name for the database, password and user but ensure to update the .env file accordingly.

  3. Create a database user and set a password:

    CREATE USER kurazetu_user WITH PASSWORD 'your_password';
    
  4. Grant privileges to the user:

    GRANT ALL PRIVILEGES ON DATABASE kurazetu_db TO kurazetu_user;
    
    ALTER DATABASE kurazetu_db OWNER TO kurazetu_user; # This is ONLY needed for Postgres 16, earlier versions are OK
    
    \q
    
  5. Enable the PostGIS extension:

        sudo -i -u postgres;
        psql -d kurazetu_db;
        CREATE EXTENSION postgis;
        \q
        exit
    
        export CPLUS_INCLUDE_PATH=/usr/include/gdal
        export C_INCLUDE_PATH=/usr/include/gdal
    
  6. Setup the test database

        sudo -u postgres psql;
        CREATE DATABASE kurazetu_test_db;
        CREATE USER test_admin WITH PASSWORD 'kurazetu_password';
        ALTER USER test_admin CREATEDB;
        ALTER USER test_admin WITH SUPERUSER;
    
        \c kurazetu_test_db
        CREATE EXTENSION postgis;
    
        \q
    

5. Apply Migrations

Run the following commands to apply database migrations:

python manage.py migrate

6. Collect Static Files

Collect static files for the Django application:

python manage.py collectstatic --noinput

7. Run the Development Server

  • Run the Django development server:

    python manage.py runserver 0.0.0.0:8000
    
  • You can now access the application at http://127.0.0.1:8000 or http://localhost:8000.

8. Create a Superuser

Exit the running Django instance (Ctrl + C) and create a superuser to access the Django admin interface.

python manage.py createsuperuser

9. Run the backend server

python manage.py runserver 0.0.0.0:8000

10. Next steps: Load Administrative Boundaries Data

This step is optional but recommended for testing purposes. You can load administrative boundaries data (Counties, Constituencies, and Wards) into your system using the Django manage.py shell. The necessary geojson data and the scripts to save them are already provided in the stations/scripts directory.

See the Load Administrative Boundaries Data guide for detailed instructions.

11. Django Admin and OTP

You will need to set up the One Time Password (OTP) for the admin interface. This is a security feature that requires you to enter a one-time password sent to your email or phone number. Since you will the first to login to the admin in your system, you will need to set up the OTP first. To set up the OTP, follow these steps:

  • Comment out this line in the src/CommunityTally/urls.py file:

- from django_otp.admin import OTPAdminSite
- admin.site.__class__ = OTPAdminSite
python manage.py runserver
  • Login to the Django admin interface at http://localhost:8000/<ADMIN_URL_SUFFIX> or http://<your-multipass-ipv4-address>:8000/ADMIN_URL_SUFFIX using the superuser credentials you created earlier.

  • Under TOTP devices, click “Add”. After filling in the form, click on “Save and Continue editing”. At the bottom of the page, you will see a QR code link.Click it and scan this QR code using an authenticator app (e.g., Google Authenticator, Microsoft Authenticator etc.) on your phone.

Go back to the urls file and un-comment the lines you commented out earlier. This will enable the OTP authentication for the admin interface.

+ from django_otp.admin import OTPAdminSite
+ admin.site.__class__ = OTPAdminSite

3. Setup Tailwind CSS

In a new terminal, run the following commands to set up Tailwind CSS for styling.

Important

This is only needed for the first time you run the project. After that, you can skip this step.

cd src/tailwind
yarn install

yarn run dev

The following command will start the tailwind server and watch for changes in the CSS files. This is needed for development purposes. Note that this command will not open any ports. It will only watch for changes in the CSS files and rebuild the CSS bundle.

python manage.py tailwind start