llama.cpp 是轻量级、高性能的本地 LLM 推理引擎,纯 C/C++ 实现,无冗余依赖,支持 GGUF 模型量化、多硬件加速(CPU/GPU)、多模态推理,是本地部署翻译 / 对话类 LLM 的核心工具。 本地部署理论上比ollama更快速。
一、环境前置准备
通用依赖(Win11/Ubuntu 均需)
下载 b8370 最新版本资源:从 llama.cpp b8370 Release 下载对应系统的预编译包(推荐), 克隆源码编译:1
2
git clone --depth 1 --branch b8370 https://github.com/ggml-org/llama.cpp.git
cd llama.cpp
模型准备下载 GGUF 格式模型(如 Llama-3、LLaVA、Gemma 3): Win11 额外依赖
安装 Visual Studio 生成工具 (勾选“C++ 生成工具”+“Windows SDK”),或直接安装 CMake (3.20+)。 若用 Winget 安装,需确保 Win11 已启用 Winget(系统自带,可通过微软商店升级)。 Ubuntu 额外依赖
1
2
3
# 安装编译依赖
sudo apt update && sudo apt install -y git cmake build-essential libopenblas-dev libgomp1
# 若需 CUDA 加速,额外安装 NVIDIA 驱动 + CUDA Toolkit(11.8+)
二、安装方式(优先预编译,次选源码编译)
方式1:预编译包安装(最快)
Win11
从 Release 页面下载 llama-b8370-bin-win64.zip; 解压到任意目录(如 D:\llama.cpp),解压后目录包含 llama-cli.exe、llama-server.exe、llama-mtmd-cli.exe(多模态)等可执行文件; 验证:双击 llama-cli.exe,若输出帮助信息则安装成功。 Ubuntu
从 Release 页面下载 llama-b8370-bin-ubuntu-x64.tar.gz; 解压并赋予执行权限:1
2
3
4
tar -zxvf llama-b8370-bin-ubuntu-x64.tar.gz -C ~/llama.cpp
cd ~/llama.cpp && chmod +x ./llama-cli ./llama-server ./llama-mtmd-cli
# 验证
./llama-cli --version # 输出 b8370 版本信息则成功
方式2:源码编译(自定义编译选项,如 CUDA/OpenBLAS)
Win11(CMake + VS 生成工具)
打开“x64 Native Tools Command Prompt for VS”(VS 编译终端),进入 llama.cpp 目录:1
2
3
4
5
cd D:\llama.cpp
# 初始化构建目录(默认 CPU 版;需 CUDA 则加 -DGGML_CUDA=ON)
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
# 编译(-j 后接核心数,如 8)
cmake --build build --config Release -j 8
编译产物在 build\bin\Release 下,包含 llama-cli.exe 等可执行文件。 Ubuntu(CMake)
1
2
3
4
5
6
7
8
9
10
cd llama.cpp
# 基础 CPU 版编译
cmake -S . -B build -DCMAKE_BUILD_TYPE= Release -DGGML_OPENBLAS= ON # 启用 OpenBLAS 加速
cmake --build build --config Release -j $( nproc)
# 若需 CUDA 加速(需已装 CUDA Toolkit)
cmake -S . -B build -DCMAKE_BUILD_TYPE= Release -DGGML_CUDA= ON
cmake --build build --config Release -j $( nproc)
# 编译产物在 build/bin 下
方式3:包管理器安装(仅基础版,版本可能略滞后,可选)
Win11(Winget)
1
winget install llama.cpp # 自动安装最新版(若需严格 b8370,建议用预编译/源码)
Ubuntu(无官方包管理器直接安装 b8370,需源码/预编译;Nix 可选)
1
2
# Nix 安装(版本可能非 b8370,仅参考)
nix profile install nixpkgs#llama-cpp
三、核心功能使用
1. 基础文本推理(llama-cli)
Win11
1
2
3
4
# 进入可执行文件目录
cd D:\llama.cpp\build\bin\Release
# 运行 Llama-3 模型(替换为你的 GGUF 模型路径)
llama-cli.exe -m D:\models\llama-3-8b-instruct-q4_0.gguf -c 4096 -p "请介绍大语言模型的应用场景" --temp 0.1
Ubuntu
1
2
3
cd ~/llama.cpp/build/bin
# 运行模型(替换模型路径)
./llama-cli -m ~/models/llama-3-8b-instruct-q4_0.gguf -c 4096 -p "Explain the application scenarios of large language models" --temp 0.1
2. 多模态推理(llama-mtmd-cli,如 LLaVA、Gemma 3 Vision)
前提:下载多模态 GGUF 模型(如 LLaVA-1.6、Gemma 3-4b-it)
Win11
1
2
3
4
# LLaVA 1.6 示例(需 LLM 模型 + 视觉投影文件)
llama-mtmd-cli.exe -m D:\models\llava-1.6-7b-q4_0.gguf --mmproj D:\models\llava-1.6-mmproj-q4_0.gguf -c 4096 --image D:\test.jpg -p "描述这张图片的内容" --temp 0.1
# Gemma 3 Vision 示例(直接调用 Hugging Face 预量化模型)
llama-mtmd-cli.exe -hf ggml-org/gemma-3-4b-it-GGUF --image D:\test.jpg -p "What's in this image?"
Ubuntu
1
2
3
4
# LLaVA 1.6 示例
./llama-mtmd-cli -m ~/models/llava-1.6-7b-q4_0.gguf --mmproj ~/models/llava-1.6-mmproj-q4_0.gguf -c 4096 --image ~/test.jpg -p "描述这张图片" --temp 0.1
# Gemma 3 Vision 示例
./llama-mtmd-cli -hf ggml-org/gemma-3-4b-it-GGUF --image ~/test.jpg -p "What's in this image?"
3. 启动 OpenAI 兼容服务(llama-server)
Win11
1
llama-server.exe -m D:\models\llama-3-8b-instruct-q4_0.gguf -c 4096 --port 8080 --host 0.0.0.0 --n-gpu-layers 99 # -ngl 99 启用全部 GPU 层
Ubuntu
1
./llama-server -m ~/models/llama-3-8b-instruct-q4_0.gguf -c 4096 --port 8080 --host 0.0.0.0 --n-gpu-layers 99
调用示例:通过 curl 测试1
2
3
4
5
6
7
8
curl http://localhost:8080/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "llama-3-8b",
"prompt": "Hello!",
"max_tokens": 100,
"temperature": 0.1
}'
4. 性能测试(llama-bench)
Win11
1
llama-bench.exe -m D:\models\llama-3-8b-instruct-q4_0.gguf -ngl 99 --batch-size 128 -t 4 -p 128 -n 32
Ubuntu
1
./llama-bench -m ~/models/llama-3-8b-instruct-q4_0.gguf -ngl 99 --batch-size 128 -t 4 -p 128 -n 32
四、关键参数说明(Win11/Ubuntu 通用)
参数 作用 -m指定 GGUF 模型路径(核心参数) -c上下文窗口大小(如 4096、8192,需匹配模型支持的最大上下文) --temp温度(越低越精准,如 0.1;越高越有创意,如 0.8) --n-gpu-layers (-ngl)卸载到 GPU 的层数(99 表示全部,仅 CUDA/Metal 生效) --image多模态推理时指定图片路径(llama-mtmd-cli 专用) --mmproj多模态投影文件路径(LLaVA 等模型专用) -tCPU 线程数(建议设为物理核心数)
版本验证
1
2
3
4
5
# Win11
llama-cli.exe --version # 输出 b8370 则为目标版本
# Ubuntu
./llama-cli --version