How to Create a Local Package Repository with `pkg repo` on FreeBSD

How to set up a local package repository on FreeBSD using pkg repo.

Setting up a local package repository on FreeBSD using pkg repo can be beneficial for system administrators who manage multiple FreeBSD systems. A local repository allows for centralized package management, controlled software versions, and optimized bandwidth usage. This guide walks you through the process of creating and configuring a local package repository in FreeBSD.

Why Use a Local Package Repository?

  1. Custom Package Management – Enables installation of specific package versions tailored to your environment.
  2. Faster Deployments – Reduces the need to download packages repeatedly from external sources.
  3. Bandwidth Efficiency – Conserves bandwidth by serving packages from a local server.
  4. Availability – Ensures package availability even when external repositories are down.

Prerequisites

  • FreeBSD system with root access
  • pkg installed and updated
  • Sufficient disk space for package storage

If pkg is not installed, initialize it with:

# /usr/sbin/pkg

Alternatively, install it from ports:

# cd /usr/ports/ports-mgmt/pkg
# make install clean

Step 1: Creating the Repository Directory

Select a directory to store your repository, such as /usr/local/pkgs:

# mkdir -p /usr/local/pkgs
# chmod 755 /usr/local/pkgs

Step 2: Downloading or Building Packages

Fetching Precompiled Packages

Use the pkg fetch command to download required packages:

# pkg fetch -o /usr/local/pkgs nano

Replace nano with the desired package name.

Building Packages from Ports

To build a package from source using the FreeBSD Ports Collection:

# cd /usr/ports/editors/nano
# make package

Move the created package to your repository directory:

# mv /usr/ports/editors/nano/work/pkg/nano-*.txz /usr/local/pkgs/

Step 3: Generating the Package Repository Index

Navigate to the repository directory and run:

# cd /usr/local/pkgs
# pkg repo .

This creates a repo.txz file containing the package metadata.

Step 4: Configuring Clients to Use the Local Repository

On each client system, create a new repository configuration file:

# mkdir -p /usr/local/etc/pkg/repos/
# ee /usr/local/etc/pkg/repos/local.conf

Add the following content:

local: {
  url: "file:///usr/local/pkgs",
  enabled: yes
}

Save and exit the file editor.

Step 5: Testing the Local Repository

On a client machine, update the package database:

# pkg update

Then, install a package from the local repository:

# pkg install nano

Step 6: Serving the Repository Over HTTP

To make the repository available across multiple machines, set up a web server (e.g., Nginx):

Installing Nginx

# pkg install nginx

Configuring Nginx to Serve Packages

Edit the Nginx configuration file:

# ee /usr/local/etc/nginx/nginx.conf

Add a server block:

server {
    listen 80;
    server_name your_server_ip;
    location / {
        root /usr/local/pkgs;
        autoindex on;
    }
}

Replace your_server_ip with your actual server IP or domain.

Starting Nginx

# service nginx start

Updating Client Configuration

On client machines, update the repository configuration:

local: {
  url: "http://your_server_ip",
  enabled: yes
}

Run pkg update again to verify access.

Conclusion

Setting up a local package repository on FreeBSD using pkg repo allows for efficient software management across multiple systems. By following these steps, you can create, populate, and distribute packages within your network, ensuring reliable and streamlined package installations.