Ctrl K
ring2all.com

⚡ Part 3: Compiling and Packaging Kamailio on Debian 13 (Trixie)

Welcome to the third and final installment of our "Debian 13 Packaging & Distribution" series. In the previous parts, we set up our GPG-signed APT repository and compiled the FreeSWITCH telephony core. Now, we will compile and package Kamailio (v6.1), the high-performance SIP routing engine, from source on Debian 13. Finally, we will publish the resulting modular packages to our custom repository server and perform an end-to-end client installation.

🏗️ Why Compile Kamailio from Source?

While Debian provides pre-compiled Kamailio packages in its upstream repositories, they are often lagging behind the latest stable upstream branch or missing custom modules. Compiling Kamailio on our own build node gives us:

  1. Access to the Latest Features: Target the stable 6.1 upstream branch.
  2. Custom Modules: Build only the modules we require (e.g., PostgreSQL support, JSON/JWT processing, WebSockets, or TLS).
  3. Optimization: Compile binaries compiled specifically for your deployment target's processor architecture (e.g., AMD64 vs. ARM64).


🛠️ Phase 1: Environment & Dependency Setup

Kamailio has a modular architecture. In order to build database, presence, secure transport, and logging modules, we must install their corresponding development packages.

1.1 Install Compiling and Packaging Tools

Bash
apt-get update
apt-get install -y \
  sudo git build-essential devscripts dpkg-dev equivs fakeroot \
  autoconf automake libtool libtool-bin cmake pkg-config \
  wget curl unzip python-is-python3 python3-all-dev \
  python3-setuptools docbook-xsl xsltproc \
  libglib2.0-dev graphviz dh-make

1.2 Install Kamailio Build Prerequisites

Install the developer libraries required to compile Kamailio's core modules:

Bash
apt-get install -y \
  debhelper dh-cmake dh-sequence-cmake bison flex cmake \
  default-libmysqlclient-dev docbook-xml docbook-xsl \
  erlang-dev libcurl4-openssl-dev libev-dev libevent-dev \
  libexpat1-dev libhiredis-dev libjansson-dev libjson-c-dev \
  libjwt-dev libldap2-dev liblua5.4-dev libmaxminddb-dev \
  libmemcached-dev libmicrohttpd-dev libmnl-dev libmongoc-dev \
  libmosquitto-dev libnats-dev libncurses-dev libnghttp2-dev \
  libpcre2-dev libperl-dev libphonenumber-dev libpq-dev \
  librabbitmq-dev libradcli-dev librdkafka-dev libreadline-dev \
  libsasl2-dev libsctp-dev libsecp256k1-dev libsecsipid-dev \
  libsnmp-dev libsqlite3-dev libssl-dev libsystemd-dev \
  libunistring-dev libwebsockets-dev libwolfssl-dev libxml2-dev \
  lynx openssl python3 python3-dev ruby-dev \
  unixodbc-dev uuid-dev zlib1g-dev


⚙️ Phase 2: Compiling & Packaging Kamailio

We clone Kamailio into /usr/src/kamailio/src. This isolates our build directories and outputs the packaged files in the parent directory (/usr/src/kamailio/), keeping our source tree clean.

Clone the stable 6.1 branch of Kamailio:

Bash
mkdir -p /usr/src/kamailio/deb
git clone -b 6.1 https://github.com/kamailio/kamailio.git /usr/src/kamailio/src
cd /usr/src/kamailio/src

# Link native Debian Trixie packaging control files
ln -s pkg/kamailio/deb/trixie debian

# Rename source and binary packages in debian/control to start with sbc-kamailio
# This adds Provides/Conflicts/Replaces to prevent collisions with official Debian packages
python3 -c "
import re, os
content = open('debian/control').read()

# Prefix all occurrences of kamailio with sbc-
content = re.sub(r'(?<!sbc-)kamailio', 'sbc-kamailio', content)

# Split into stanzas, strip whitespace, and inject compatibility directives safely
stanzas = [s.strip() for s in content.split('\n\n') if s.strip()]
new_stanzas = []
for stanza in stanzas:
    match = re.search(r'^Package:\s*(sbc-kamailio[^\s]*)', stanza, re.MULTILINE)
    if match:
        pkg = match.group(1)
        orig = pkg[4:] # Remove 'sbc-' prefix
        fields = {'Provides': orig, 'Conflicts': orig, 'Replaces': orig}
        for field, value in fields.items():
            field_pattern = rf'^({field}:\s*)(.*)'
            if re.search(field_pattern, stanza, re.MULTILINE):
                stanza = re.sub(field_pattern, rf'\1{value}, \2', stanza, flags=re.MULTILINE)
            else:
                stanza += f'\n{field}: {value}'
    new_stanzas.append(stanza)

content = '\n\n'.join(new_stanzas) + '\n'
open('debian/control', 'w').write(content)

# Rename installation files in debian/ EXCEPT for the service unit, init script, and default config
# This ensures that the installed system service and configuration files retain their standard names
for f in os.listdir('debian'):
    if 'kamailio' in f and not f.startswith('sbc-'):
        if f in ['kamailio.service', 'kamailio@.service', 'kamailio.init', 'kamailio.default']:
            continue
        new_f = 'sbc-' + f
        os.rename(os.path.join('debian', f), os.path.join('debian', new_f))

# Inject dh_installsystemd and dh_installinit overrides in debian/rules
# to direct debhelper to build the packages using the standard name 'kamailio'
rules = open('debian/rules').read()
overrides = '''
override_dh_installsystemd:
\tdh_installsystemd --name=kamailio

override_dh_installinit:
\tdh_installinit --name=kamailio
'''
if 'override_dh_installsystemd' not in rules:
    open('debian/rules', 'a').write(overrides)
"

# Update package source name in changelog to match renamed source package
sed -i 's/^kamailio/sbc-kamailio/g' debian/changelog

# Add a new custom changelog version entry using dch (use -b to force version downgrade/bad version check)
DEBEMAIL="[EMAIL_ADDRESS]" DEBFULLNAME="[YOUR_FULL_NAME]" dch -b -v "6.1.0-0+sbc" "Custom compile with sbc-kamailio package rename"

NOTE
Debian Packaging Link: Linking pkg/kamailio/deb/trixie to the debian directory is a crucial step. Kamailio maintains native, distribution-specific build scripts within its repository. This link instructs dpkg-buildpackage to use the official rules designed specifically for Debian 13.

2.2 Generate Configuration

Create the build config targets:

Bash
make cfg

2.3 Build Debian Packages

Clean the workspace and trigger compilation:

Bash
# Clean packaging workspace
fakeroot debian/rules clean

# Compile binary-only packages without signing metadata
dpkg-buildpackage -us -uc -b -j$(nproc)

# Move all generated packages and build receipts to our deb directory
mv /usr/src/kamailio/*.deb /usr/src/kamailio/deb/
mv /usr/src/kamailio/*.changes /usr/src/kamailio/deb/ 2>/dev/null || true
mv /usr/src/kamailio/*.buildinfo /usr/src/kamailio/deb/ 2>/dev/null || true

The directory /usr/src/kamailio/deb/ will now contain modular packages, including:

sbc-kamailio_.deb: The core binary and routing engine. sbc-kamailio-postgres-modules_.deb: Database backends (PostgreSQL engine). sbc-kamailio-presence-modules_.deb: Presence services (SIP SUBSCRIBE/NOTIFY, dialog info). sbc-kamailio-websocket-modules_.deb: WebSocket engines (SIP-over-WebSockets for WebRTC clients).

🌐 Phase 3: Publishing Kamailio to the Custom Repository

With our packages compiled and located inside /usr/src/kamailio/deb/, we can push them to our repository server (IP: ) for distribution.

3.1 Upload Packages

From your build node, upload the Kamailio packages to the repository's incoming base pool:

Bash
scp /usr/src/kamailio/deb/*.deb root@<YOUR_REPO_IP>:/var/www/pbx-repo/ring2all/trixie/apt/pool/b/base/

3.2 Update Repository Metadata

Log into your repository server over SSH and rebuild the repository index:

Bash
ssh root@<YOUR_REPO_IP> "BuildRepoRing2All"

The script cleans up older package builds, scans the new packages, updates Packages.gz indexes, and signs the Release logs cryptographically.


🚀 Phase 4: End-to-End Client Installation

To deploy both Kamailio and FreeSWITCH on a clean Debian 13 (Trixie) server, run these commands:

4.1 Setup the Repository

Run the repository bootstrap script to register the GPG key and APT source configurations:

Bash
wget -qO- https://repo.softswitchone.com/apt/setup_repo | bash

4.2 Install Packages

Update the local package manager and install Kamailio alongside PostgreSQL and WebSocket modules:

Bash
apt-get update

# Install Kamailio and its essential modules
apt-get install -y \
  sbc-kamailio \
  sbc-kamailio-postgres-modules \
  sbc-kamailio-presence-modules \
  sbc-kamailio-websocket-modules


# Verify the installation and check the compiled version
kamailio -v

Example Output:

Text
kamailio 6.1.0 (x86_64/linux)


🏁 Series Summary & Best Practices

Congratulations! You have completed the 3-part Packaging and Repository series. Here is a review of what we built:

  1. Part 1: Deployed an HTTPS-enabled repository server with RSA GPG key signing and automated indexing scripts.
  2. Part 2: Built FreeSWITCH and its core telephony dependencies (SpanDSP, Sofia-SIP, libks2, etc.) from source, and published them.
  3. Part 3: Compiled Kamailio v6.1 from source using native packaging links and published its modular parts.

Best Practices for Maintenance

Version Pinning: On production nodes, configure APT pinning to prevent upstream Debian repositories from overwriting your custom-built packages.

Clean Keyring Management: Always distribute the public signing GPG key via /etc/apt/keyrings/ instead of legacy deprecated paths like apt-key add.

  • Sequential Builds: When rebuilding FreeSWITCH modules, make sure to keep parallel compilation turned off (parallel=1) to prevent build locks.

AI Assistant

👋 Hello! I'm your Ring2All documentation assistant. I can help you find information about configuring and using the Ring2All PBX platform.

How can I help you today?