Skip to content

Instantly share code, notes, and snippets.

@syntaxhacker
Created May 13, 2026 05:49
Show Gist options
  • Select an option

  • Save syntaxhacker/1155967b47876438449d4ab8de8b22ca to your computer and use it in GitHub Desktop.

Select an option

Save syntaxhacker/1155967b47876438449d4ab8de8b22ca to your computer and use it in GitHub Desktop.
llama.cpp with CUDA on Ubuntu 24.04 Setup Guide

llama.cpp with CUDA on Ubuntu 24.04 — Complete Setup Guide

A step-by-step guide to install CUDA toolkit and build llama.cpp with GPU acceleration on Ubuntu 24.04 LTS.

System Requirements

  • OS: Ubuntu 24.04 LTS (Noble Numbat)
  • GPU: NVIDIA GPU (e.g., GeForce GTX 1650 Ti — 4 GB VRAM)
  • Disk: ~10 GB free space (CUDA toolkit ~6.8 GB + build artifacts)
  • RAM: 8+ GB recommended

1. Install NVIDIA Driver

First check your GPU and current driver:

lspci | grep -i nvidia
nvidia-smi  # if driver already installed

Install the recommended driver (use ubuntu-drivers devices to list options):

sudo apt update
sudo apt install -y nvidia-driver-595
sudo reboot

After reboot, verify:

nvidia-smi

Expected output:

+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 595.58.03              Driver Version: 595.58.03      CUDA Version: 13.2     |
+-----------------------------------------------------------------------------------------+

Note: The driver version must match the CUDA toolkit you plan to install.
nvidia-smi shows CUDA Version: X.Y — this is the maximum CUDA toolkit version the driver supports. Install a driver that supports your target CUDA version.


2. Install CUDA Toolkit

Add the NVIDIA CUDA repository:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update

Install the full CUDA toolkit:

sudo apt install -y cuda-toolkit

Add CUDA to your PATH (add to ~/.bashrc):

echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

Verify installation:

nvcc --version

Expected output:

nvcc: NVIDIA (R) Cuda compiler driver
Cuda compilation tools, release 13.2, V13.2.78

Test CUDA with a Simple Program

Create /tmp/test.cu:

#include <cstdio>
int main() {
  int count;
  cudaGetDeviceCount(&count);
  printf("CUDA devices found: %d\n", count);
  cudaDeviceProp prop;
  cudaGetDeviceProperties(&prop, 0);
  printf("GPU: %s\n", prop.name);
  return 0;
}

Compile and run:

nvcc -o /tmp/test /tmp/test.cu && /tmp/test

Expected output:

CUDA devices found: 1
GPU: NVIDIA GeForce GTX 1650 Ti

3. Build llama.cpp with CUDA

Install Build Dependencies

sudo apt install -y git cmake build-essential

Clone and Build

git clone --depth 1 https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
mkdir build && cd build
cmake .. -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)

The -DGGML_CUDA=ON flag enables GPU acceleration via CUDA.
CMake will auto-detect your CUDA installation and GPU architecture.

Verify CUDA Detection in llama.cpp

Run the binary to confirm GPU detection:

./bin/llama-cli --help

Expected output contains:

ggml_cuda_init: found 1 CUDA devices (Total VRAM: 3715 MiB):
  Device 0: NVIDIA GeForce GTX 1650 Ti, compute capability 7.5, VMM: yes, VRAM: 3715 MiB

4. Using llama.cpp

Download a Model

LLMs are distributed as GGUF files. Download one from Hugging Face:

# Example: TinyLlama (1.1B params, ~700 MB)
wget -O /tmp/tinyllama.gguf https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v1.0-GGUF/resolve/main/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf

Run Inference on GPU

./bin/llama-cli -m /tmp/tinyllama.gguf -p "Hello, how are you?" -n 128
  • -m: path to model file
  • -p: prompt text
  • -n: number of tokens to generate

By default llama.cpp will use GPU (CUDA) for computation. You can verify GPU usage with nvidia-smi while running.

Run the HTTP Server

./bin/llama-server -m /tmp/tinyllama.gguf --port 8080

Then send requests:

curl -X POST http://localhost:8080/completion \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Hello!", "n_predict": 64}'

5. Troubleshooting

"Driver/library version mismatch"

The kernel still has old NVIDIA modules loaded. Reboot:

sudo reboot

"nvcc: command not found"

CUDA toolkit is not in PATH. Either source .bashrc or run:

export PATH=/usr/local/cuda/bin:$PATH

"libcuda.so not found" during build

Ensure LD_LIBRARY_PATH includes CUDA's library path:

export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Slow Performance / "suboptimal performance due to a lack of tensor cores"

The GTX 1650 Ti has Turing architecture (compute 7.5) but lacks tensor cores. This is expected — the model will still run on CUDA cores. To suppress the warning:

cmake .. -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_CUDA_ARCHITECTURES="61-virtual;80-virtual" \
  -DGGML_CUDA_FORCE_MMQ=ON

Package Conflicts during Driver Install

If libnvidia-egl-xcb1 conflicts with the new driver:

sudo dpkg --purge --force-all libnvidia-egl-xcb1 libnvidia-egl-xlib1
sudo apt --fix-broken install -y

Summary

Component Version
OS Ubuntu 24.04 LTS
GPU NVIDIA GeForce GTX 1650 Ti (4 GB)
Driver nvidia-driver-595 (595.58.03)
CUDA Toolkit 13.2
llama.cpp Latest (git main)
Build Config -DGGML_CUDA=ON

Total installation time: ~20-30 minutes (download speed dependent)
Total disk usage: ~8 GB (CUDA toolkit + build)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment