How to create a local repository in Linux (RHEL / AlmaLinux)

What is a local repository?

A Local Repository is a storage location on a Linux server that contains RPM packages and repository metadata.

Why use a Local Repository?

  • Offline Installation: Install software on servers without internet access.
  • Centralized Management: Store and manage RPM packages in one location.
  • Faster Installation: Install packages from a local network.
  • Dependency Management: Allow DNF to resolve package dependencies.
  • Version Control: Control which package versions are available.

Objectives:

In this guide, we will:

  1. Prepare RPM packages for the local repository.
  2. Create a local repository directory
  3. Generate repository metadata
  4. Configure DNF to use the local repository
  5. Verify that the repository works correctly

Implementation

We will use an internet-connected AlmaLinux 9 server to download PostgreSQL 17 and PostgreSQL 18 packages, then prepare them for an offline local repository.

  • Install the PostgreSQL Repository

    On the internet-connected server, install the official PostgreSQL repository.

    dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

    This adds the PostgreSQL Global Development Group (PGDG) repository, which provides multiple PostgreSQL versions.

  • Install the Download Tool
    dnf install -y dnf-plugins-core

    This provides the dnf download command.

  • Create Directories for PostgreSQL Packages/
    mkdir -p /repo/postgresql17
    mkdir -p /repo/postgresql18
  • Disable the Built-in PostgreSQL Module

    Disable PostgreSQL module to avoid conflicts with PGDG packages.

    dnf -qy module disable postgresql
  • Download PostgreSQL 17 Packages
    cd /repo/postgresql17
    dnf download --resolve --alldeps postgresql17-server
  • Download PostgreSQL 18 Packages
    cd /repo/postgresql18
    dnf download --resolve --alldeps postgresql18-server
    • --resolve : Downloads required dependencies.
    • --alldeps : Downloads dependencies even if they are already installed on the downloading server.
  • Verify Downloaded Packages

    Check the downloaded RPM files:

    ls -lh /repo/postgresql17/
    ls -lh /repo/postgresql18/
  • Create a Local Repository
    • Install the repository management tool
      dnf install -y createrepo_c
    • Generate repository metadata

      Run the following commands to create repository metadata for PostgreSQL 17 and 18.

      For PostgreSQL 17

      createrepo_c /repo/postgresql17/

      For PostgreSQL 18

      createrepo_c /repo/postgresql18/
    • Verify the repository structure
      ls -l /repo/
      ls -l /repo/postgresql17 | grep repodata
      ls -l /repo/postgresql18 | grep repodata
  • Configure the DNF Repository
    • DNF repository configuration files are stored in: /etc/yum.repos.d/
    • Create a configuration file for PostgreSQL 17 and 18
      vi /etc/yum.repos.d/local-postgresql.repo

      To configure PostgreSQL 17 and 18, add the following configuration:

      [local-postgresql17]
      name=Local PostgreSQL 17 Repository
      baseurl=file:///repo/postgresql17/
      enabled=1
      gpgcheck=0
      
      [local-postgresql18]
      name=Local PostgreSQL 18 Repository
      baseurl=file:///repo/postgresql18/
      enabled=1
      gpgcheck=0
    • Refresh DNF Cache
      dnf clean all
      dnf makecache
  • Verify Repository Configuration
    dnf repolist
  • Verify the Local Repository
    • List Available PostgreSQL 17 Packages

      dnf --disablerepo="*" --enablerepo="local-postgresql17" list available
    • List Available PostgreSQL 18 Packages

      dnf --disablerepo="*" --enablerepo="local-postgresql18" list available