Package releases can lag behind a new llama.cpp feature or omit a build option needed on a development machine. A source build with CMake produces the llama-cli and llama-server binaries directly from the upstream project.

On Ubuntu or Debian, the prerequisite packages provide the compiler, Git, CMake, and OpenSSL development libraries for HTTPS support. Once equivalent build tools are installed, the configure and build sequence is the same on most Linux distributions.

A CPU build provides a baseline before adding GPU backends and their vendor SDKs. The resulting tools run from the build directory without a system-wide installation, and a local chat model in GGUF format allows a short inference run after compilation.

Steps to build llama.cpp from source with CMake:

  1. Refresh the APT package index.
    $ sudo apt-get update
  2. Install the build dependencies from the distribution repositories.
    $ sudo apt-get install --assume-yes ca-certificates git cmake build-essential pkg-config libssl-dev

    APT changes system packages and needs sudo; the source checkout and build run as your regular user. Current upstream uses OpenSSL for HTTPS support, supplied by libssl-dev on Ubuntu and Debian.

    On macOS, the platform prerequisites are Apple's command line developer tools, Git, and CMake rather than these APT packages. Upstream enables Metal by default on macOS; the configure option -DGGML_METAL=OFF disables it for a CPU-only build.

  3. Clone the upstream llama.cpp source tree.
    $ git clone --depth 1 https://github.com/ggml-org/llama.cpp llama.cpp
    Cloning into 'llama.cpp'...

    Omit --depth 1 when the checkout needs full project history for development or patch review.

  4. Enter the source directory.
    $ cd llama.cpp
  5. Record the upstream revision used for the build.
    $ git rev-parse HEAD
    d05706b4f32a545814856e98ca518292585443b9

    The shallow clone follows the current upstream branch. The full commit identifies the source if a later checkout behaves differently.

  6. Configure a release build.
    $ cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
    -- The C compiler identification is GNU 15.2.0
    ##### snipped #####
    -- Including CPU backend
    ##### snipped #####
    -- Found OpenSSL: /usr/lib/aarch64-linux-gnu/libcrypto.so (found version "3.5.5")
    ##### snipped #####

    The CPU backend appears in the configure output without a GPU SDK. GPU-specific CMake options require the matching driver and development libraries.

  7. Build the llama-cli and llama-server targets.
    $ cmake --build build --config Release --target llama-cli llama-server --parallel 2
    ##### snipped #####
    [100%] Built target llama-cli
    ##### snipped #####
    [100%] Built target llama-server

    --parallel 2 limits compilation to two concurrent jobs, reducing peak memory use. The build may also download web UI assets for llama-server.

  8. Confirm the llama-cli binary starts.
    $ ./build/bin/llama-cli --version
    version: 0.7.0-dev (build 1, commit d05706b)
    built with GNU 15.2.0 for Linux aarch64
  9. Confirm the llama-server binary starts.
    $ ./build/bin/llama-server --version
    version: 0.7.0-dev (build 1, commit d05706b)
    built with GNU 15.2.0 for Linux aarch64

    The commit, compiler version, and architecture reflect the checkout and build host. The binaries remain under build/bin alongside their runtime libraries; a build alone does not add them to PATH.

  10. Run a single-turn prompt with the newly built llama-cli.
    $ ./build/bin/llama-cli -m ../models/chat-model.gguf -p "What is two plus two?" -n 32 --single-turn --no-display-prompt
    ##### snipped #####
    build      : b1-d05706b
    model      : ../models/chat-model.gguf
    ##### snipped #####
    Two plus two is four.
    
    [ Prompt: 58.3 t/s | Generation: 21.3 t/s ]
    
    
    Exiting...

    The model path refers to an existing local chat GGUF saved in a models directory beside the llama.cpp checkout. A returned answer followed by Exiting… shows that the new binary can load a model and generate text. The answer varies with the model and sampling settings.
    Related: How to run a local GGUF model with llama-cli
    Related: How to run a Hugging Face GGUF model with llama.cpp

Share this guide