A step-by-step guide to install CUDA toolkit and build llama.cpp with GPU acceleration on Ubuntu 24.04 LTS.
- 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
First check your GPU and current driver:
lspci | grep -i nvidia
nvidia-smi # if driver already installedInstall the recommended driver (use ubuntu-drivers devices to list options):
sudo apt update
sudo apt install -y nvidia-driver-595
sudo rebootAfter reboot, verify:
nvidia-smiExpected 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-smishowsCUDA Version: X.Y— this is the maximum CUDA toolkit version the driver supports. Install a driver that supports your target CUDA version.
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 updateInstall the full CUDA toolkit:
sudo apt install -y cuda-toolkitAdd 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 ~/.bashrcVerify installation:
nvcc --versionExpected output:
nvcc: NVIDIA (R) Cuda compiler driver
Cuda compilation tools, release 13.2, V13.2.78
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/testExpected output:
CUDA devices found: 1
GPU: NVIDIA GeForce GTX 1650 Ti
sudo apt install -y git cmake build-essentialgit 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.
Run the binary to confirm GPU detection:
./bin/llama-cli --helpExpected 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
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./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.
./bin/llama-server -m /tmp/tinyllama.gguf --port 8080Then send requests:
curl -X POST http://localhost:8080/completion \
-H "Content-Type: application/json" \
-d '{"prompt": "Hello!", "n_predict": 64}'The kernel still has old NVIDIA modules loaded. Reboot:
sudo rebootCUDA toolkit is not in PATH. Either source .bashrc or run:
export PATH=/usr/local/cuda/bin:$PATHEnsure LD_LIBRARY_PATH includes CUDA's library path:
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATHThe 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=ONIf 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| 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)