Introduction to llama.cpp

llama.cpp is a popular C/C++-based inference runtime for Large Language Models (LLMs). Its primary appeal lies in its lightweight design, speed, and efficiency, making it ideal for running GGUF-formatted models locally. This runtime supports a wide array of hardware, including x86_64 servers, Linux workstations, mini PCs, Raspberry Pis, Orange Pis, other ARM64 Single Board Computers (SBCs), and Linux containers.

When deploying llama.cpp in a real-world scenario, two main approaches are commonly adopted:

  1. Native Build: This involves compiling the code directly on the target machine that will eventually run llama.cpp. This method ensures optimal performance for the specific architecture of the target hardware.
  2. Cross-Compile: This approach is beneficial when the build machine is significantly faster than the target deployment machine (e.g., compiling on a powerful x86_64 PC for an ARM64 Orange Pi). It allows for faster build times and the creation of binaries for architectures different from the build environment.

Native Compilation on Debian 12/13

This section details the process of compiling llama.cpp directly on a Debian 12 or Debian 13 system. This is the most straightforward method if your target machine is also your build machine.

1. Install Essential Dependencies

Before you can compile llama.cpp, you need to ensure your system has the necessary build tools and libraries. Open a terminal and execute the following commands:

sudo apt update
sudo apt install -y build-essential git cmake pkg-config

build-essential provides the fundamental GNU compiler toolchain (gcc, g++, make). git is needed to clone the llama.cpp repository. cmake is the build system generator used by llama.cpp, and pkg-config helps manage library dependencies.

2. Clone the llama.cpp Repository

Next, clone the official llama.cpp repository from GitHub. It's good practice to clone it into a dedicated directory, such as a ~/projects folder.

mkdir -p ~/projects
cd ~/projects
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp

Cloning the repository fetches the latest stable version of the code. You can check out a specific release tag if you need a particular version.

3. Compile llama.cpp

llama.cpp uses CMake for its build process. The standard compilation involves creating a build directory, configuring with CMake, and then building the project. For a basic build, execute:

mkdir build
cd build
cmake ..
make -j

The cmake .. command configures the build process based on your system's environment. The make -j command compiles the project using all available CPU cores, significantly speeding up the compilation time. The -j flag tells make to run jobs in parallel.

After a successful compilation, you will find the executable binaries in the build directory, including main (for running inference) and server (for running an HTTP server).

Terminal output showing successful compilation of llama.cpp binaries

Cross-Compilation for ARM64 on Debian

Cross-compilation is essential when you want to build llama.cpp on a powerful x86_64 machine but deploy it on an ARM64 device, such as a Raspberry Pi or an Orange Pi. This requires a cross-compiler toolchain.

1. Install the ARM64 Cross-Compiler Toolchain

First, install the necessary cross-compilation tools. For Debian systems, you can install the GCC toolchain for AArch64 (ARM64).

sudo apt update
sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

This installs the GNU Compiler Collection (GCC) and its C++ counterpart, specifically targeting the AArch64 architecture.

2. Clone the llama.cpp Repository

If you haven't already, clone the llama.cpp repository as described in the native build section. Ensure you are in the cloned directory:

cd ~/projects/llama.cpp

3. Configure and Compile with CMake for ARM64

When using CMake for cross-compilation, you need to specify the target architecture and the cross-compiler. Create a separate build directory for cross-compilation to avoid conflicts with native builds.

mkdir build-arm64
cd build-arm64
cmake .. -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64 -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++
make -j
  • -DCMAKE_SYSTEM_NAME=Linux: Specifies the target operating system.
  • -DCMAKE_SYSTEM_PROCESSOR=aarch64: Specifies the target CPU architecture.
  • -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc: Sets the C compiler to the ARM64 cross-compiler.
  • -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++: Sets the C++ compiler to the ARM64 cross-compiler.

The make -j command will then compile llama.cpp using the specified cross-compiler. The resulting binaries will be located in the build-arm64 directory and are ready to be transferred to your ARM64 target device.

4. Transferring Binaries to Target Device

Once compilation is complete, you need to copy the generated executables (e.g., main, server) from your build machine to your ARM64 device. Tools like scp or rsync are commonly used for this purpose.

# Example using scp from your build machine:
scp build-arm64/main user@your_arm64_ip:/path/to/destination/

Ensure you have SSH access configured on your ARM64 device and replace user, your_arm64_ip, and /path/to/destination/ with your specific details.

Optimizations and Further Steps

llama.cpp supports various build-time optimizations. For instance, you can enable specific hardware acceleration features if your target CPU supports them. For native builds on modern x86_64 CPUs, enabling AVX, AVX2, or AVX512 can yield significant performance improvements. This is often done by passing flags to CMake, such as -DLLAMA_AVX2=ON.

For ARM64 cross-compilation, you might explore flags related to NEON instructions. Always refer to the official llama.cpp documentation for the most up-to-date compilation flags and optimization options specific to different architectures and instruction sets.

After compiling, you'll need to download compatible model weights in GGUF format and use the compiled executables to run inference.