Saltar al contenido

Lección 7 · Running Llama Locally

Run Llama with llama.cpp

Use llama.cpp to run GGUF Llama models from the command line, set context size and GPU layers, and start a local server with an OpenAI-compatible API.

llama.cpp is an open source project that runs language models efficiently, written mainly in C++ and C. It works on CPUs and on several kinds of GPUs, and it uses model files in the GGUF format. It gives you more direct control than many friendlier apps. The commands here follow its official repository as checked in December 2026. The project changes quickly, so compare with the current documentation.

Install

The repository describes several ways: install scripts, prebuilt binaries from the releases page, Docker, or building from source. Choose the build that matches your hardware (CPU only, NVIDIA, Apple and so on). Download only from the official project.

Run a model

The current README shows a unified llama command. Separate tool names such as llama-cli, llama-server and llama-quantize appear in tool documentation and source builds. Use whichever your install provides:

Terminal
# Unified command: fetch a GGUF repository from Hugging Face and chat
llama cli -hf <user>/<model-repo>-GGUF:Q4_K_M

# Or run a file you already have
llama-cli -m ./models/your-llama-instruct-Q4_K_M.gguf

The angle-bracket parts are placeholders. The -hf flag takes a Hugging Face repository, optionally followed by a quantization tag. Before you download, check who uploaded the file. A GGUF made by a third party is not an official Meta release, and the Llama license still applies to it.

Flags you will use often

These come from the server documentation:

Flag Meaning
-m FILE Path to a GGUF model
-hf REPO[:QUANT] Download from Hugging Face
-c N Context size in tokens (0 takes it from the model)
-ngl N Number of layers to place on the GPU
--host, --port Server address and port
--api-key KEY Require a key for the server
-np N Number of parallel request slots

GPU offload deserves a word. A model is a stack of layers. -ngl sets how many go into GPU memory. If all of them fit, you usually get the best speed. If only some fit, the rest run on the CPU, and speed falls. If you run out of GPU memory, lower -ngl or -c.

Start a server

Terminal
llama-server -m ./models/your-llama-instruct-Q4_K_M.gguf \
  -c 8192 -ngl 99 --host 127.0.0.1 --port 8081 --api-key "$LOCAL_LLM_KEY"

The server offers its own endpoints (such as /health and /completion) and OpenAI-style ones such as /v1/chat/completions and /v1/embeddings. The server documentation checked for this lesson gives 9931 as the default port, and older guides may show another number. This example sets the port explicitly, so it does not depend on the default. With the unified command you can start a server with llama serve.

For machine-readable replies, the server can constrain output with a grammar or a JSON schema. In the chat endpoint, use the response_format field. Constraining the format helps the reply parse correctly, but it cannot make the content true.

Once the server is running, test it from another terminal:

Terminal
curl http://127.0.0.1:8081/v1/chat/completions \
  -H "Authorization: Bearer $LOCAL_LLM_KEY" \
  -H "Content-Type: application/json" \
  -d '{"messages": [{"role": "user", "content": "Say hello."}]}'

Estimate your launch

Python Stand-in: build a launch command from your hardware

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 4.9 GB figure matches the size that llama.cpp's quantization documentation gives for an 8B Llama 3.1 model in Q4_K_M. The memory reserve and the layer count are invented for the exercise. Start from an estimate, then watch the memory use that the program reports.

Ollama or llama.cpp?

You want Consider
The quickest first chat Ollama
Detailed control of flags and files llama.cpp
To convert and quantize models llama.cpp tools
A small local server Either

You can use both. Ollama can also import a GGUF file with a Modelfile line such as FROM ./model.gguf. According to its import documentation, it does not quantize GGUF files while importing.

Bind to 127.0.0.1 unless you have a clear reason not to. An open model server can be used by others to spend your hardware, or to probe your system.

Reading the startup log

When llama.cpp loads a model, it prints a lot of text. Do not skip it. It usually tells you the model architecture and size, the quantization type, how many layers went to the GPU, how much memory the context needs and which chat format was chosen. When something behaves strangely, compare this log with what you expected. A wrong chat format or fewer GPU layers than you planned explains many problems.

Choosing the context size

Passing -c 0 takes the context size from the model file, which can be very large for recent models and can use a lot of memory. Choose an explicit number that fits your task. With -np set above 1, the server splits capacity between several request slots, so each conversation may have less room than you expect. Check the log and the server documentation for how your version divides it.

Updating safely

The project releases very often. Before you upgrade a setup that others depend on, note the version that works, run your test prompts on the new one and compare. Keep the old binary until you are satisfied. Treat the runtime like any other dependency in your software.

A safe habit for downloads

Model files are large and often come from many different uploaders. Prefer well-known sources, read the repository page and keep the checksum of the file you tested. Treat a surprising file, such as one that asks you to run a script first, as a reason to stop.

Getting help. When you report a problem, include the version, your hardware, the full command and the relevant log lines. Remove keys and private text first.

Practice idea. Start the server twice with two context sizes, and compare the memory figures in the two startup logs.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Which model file format does llama.cpp use?

    GGUF is the format llama.cpp loads.
  2. 2. What does -ngl control?

    It sets how many layers are offloaded to GPU memory.
  3. 3. What can a JSON schema constraint do?

    Shape and truth are different. You still must validate the values.

Summary

  • llama.cpp runs GGUF models on CPUs and GPUs.
  • -m, -hf, -c and -ngl are core options.
  • Its server offers OpenAI-style endpoints and schema constraints.
  • Check file sources, set ports explicitly and keep the server on localhost.

Next: Run Llama with Hugging Face Transformers

Tu progreso se guarda en este navegador. No necesitas una cuenta.