Hands-on tutorials

Run Llama on a GPU with llama.cpp

Build a CUDA-enabled llama.cpp server, load a GGUF model and test a local API through SSH. A practical starting point for self-hosted inference.

Examples for Linux and NVIDIA GPUs. Adapt paths and requirements to your instance. Commands are based on upstream documentation; they have not been benchmarked on every NOSGPU configuration.

1. Prepare the server and model

This example uses Linux, an NVIDIA GPU, a working driver, a compatible CUDA toolkit, Git, CMake and a C++ compiler. Llama is the model family; llama.cpp is the runtime. Obtain a compatible GGUF instruction model, for example a quantised Llama 3.2 3B Instruct, from a trusted publisher and comply with its licence. Store it at the example path below, or replace that path with yours.

nvidia-smi
nvcc --version
git clone https://github.com/ggml-org/llama.cpp.git
cd llama.cpp
cmake -B build -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j 4

2. Start a local endpoint

Run this in the llama.cpp directory after placing the model file. The server listens only on localhost. The GPU-layer option requests offloading; confirm in the startup log which layers actually use the GPU. A 4096-token context is an initial test setting, not a memory guarantee. Keep this terminal running.

./build/bin/llama-server \
  -m /srv/models/llama-3.2-3b-instruct.gguf \
  --alias local-llama \
  --n-gpu-layers 99 --ctx-size 4096 \
  --host 127.0.0.1 --port 8080

3. Connect from your computer

On your computer, replace USER and SERVER_IP with the instance credentials and create this SSH tunnel. Keep it open, then run the request in a second local terminal. You do not need to expose port 8080 in the firewall. The response should contain generated assistant text; the first request may take longer.

ssh -N -L 8080:127.0.0.1:8080 USER@SERVER_IP

curl http://127.0.0.1:8080/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{"model":"local-llama","messages":[{"role":"user","content":"Explain GPU memory in two sentences."}],"max_tokens":128}'

4. Diagnose before scaling

If loading fails, check the file path, GGUF compatibility and available RAM and VRAM. For memory errors, try a smaller model or shorter context; offloading fewer layers trades GPU memory for CPU work. Record the source revision, model checksum and settings. Before public use, add authentication, TLS, request limits and a supervised service process.

Official documentation

Put your next idea to work.

Choose the resources for your workload, or talk to us about requirements before deploying.