本文最后更新于 2026年8月7日 上午
llama.cpp纯CPU部署指南
llama.cpp 是一个高性能的 C/C++ 本地推理引擎,支持在各种硬件上高效运行 LLM 模型。本文将详细介绍如何在国内和通用环境下正确部署 llama.cpp。
为什么选择 llama.cpp
- ⚡ 高性能:极致优化的推理引擎,支持多种硬件加速
- 🔧 纯 C/C++ 实现:无 Python 依赖,编译后即可运行
- 📱 多平台支持:支持 x86、ARM、Apple Silicon 等
- 🧠 量化支持:支持 4-bit、5-bit、8-bit 等量化格式
- 🔌 多种前端:支持命令行、服务器模式、嵌入式设备
官方链接
一、国内部署方式
前置条件
在开始之前,需要安装编译环境:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-make cmake
sudo apt update sudo apt install build-essential cmake git
sudo yum groupinstall "Development Tools" sudo yum install cmake git
xcode-select --install
|
方式一:获取源码
方法 1:使用 Gitee 镜像克隆(推荐)
1 2 3
| git clone https://gitee.com/mirrors/llama-cpp.git cd llama-cpp
|
方法 2:GitHub 加速访问
1 2 3 4 5 6
| git clone https://gh-proxy.com/https://github.com/ggml-org/llama.cpp.git cd llama.cpp
|
方法 3:从国内平台下载
方式二:国内编译方法
1. 编译 llama.cpp
使用 CMake 编译(推荐):
1 2 3 4 5 6 7 8 9 10
| mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
|
Makefile 方式:
1 2 3 4 5
| make
make LLAMA_NATIVE=1
|
2. 国内下载模型
由于 HuggingFace 在国内访问较慢,可以使用以下方式下载模型:
方法一:使用 ModelScope(魔搭社区)
方法二:使用国内 HuggingFace 镜像
1 2 3 4 5
| export HF_ENDPOINT=https://hf-mirror.com
hf download ggml-org/models llama2-7b.Q4_0.gguf
|
方法三:手动下载
- 访问 ModelScope 或其他国内模型站点
- 搜索 llama.cpp 支持的 GGUF 格式模型
- 下载后放入指定目录
方式三:Docker 部署(国内镜像)
1 2 3 4 5 6 7 8 9 10 11 12 13
|
{ "registry-mirrors": [ "https://docker.1ms.run", ] }
docker pull registry.cn-hangzhou.aliyuncs.com/library/llama-cpp:latest
docker pull registry.cn-hangzhou.aliyuncs.com/nvidia/llama-cpp:latest
|
国内常见问题
下载源码慢怎么办?
使用国内 Gitee 镜像或 GitHub 加速代理:
模型下载慢怎么办?
使用 ModelScope 国内平台:
编译依赖下载失败?
可以提前下载依赖源码,或使用国内 npm/maven 镜像源。
二、通用部署方式
前置条件
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
sudo apt update sudo apt install build-essential cmake git
sudo dnf groupinstall "Development Tools" sudo dnf install cmake git
xcode-select --install
|
方式一:从源码编译(推荐)
1. 克隆仓库
1 2 3
| git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp
|
2. 编译
基础编译:
1 2 3 4
| mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j$(nproc)
|
带 GPU 加速编译(NVIDIA CUDA):
1 2 3 4 5
| mkdir build && cd build cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DGGML_CUDA=ON make -j$(nproc)
|
带 Metal 加速编译(Apple Silicon):
1 2 3 4
| mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j$(sysctl -n hw.ncpu)
|
带 OpenCL 加速编译:
1 2 3 4 5
| mkdir build && cd build cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DGGML_OPENCL=ON make -j$(nproc)
|
编译选项说明:
| 选项 |
说明 |
-DGGML_CUDA=ON |
启用 NVIDIA GPU 加速 |
-DGGML_OPENCL=ON |
启用 OpenCL 加速 |
-DGGML_METAL=ON |
启用 Apple Metal 加速(默认开启) |
-DGGML_VULKAN=ON |
启用 Vulkan 加速 |
-DCMAKE_BUILD_TYPE=Release |
Release 优化模式 |
3. 下载模型
从 HuggingFace 下载:
1 2 3 4 5 6 7 8 9
| pip install huggingface-hub
hf download ggml-org/models llama2-7b.Q4_0.gguf
hf download ggml-org/models llama2-7b.Q4_0.gguf \ --local-dir ./models
|
从modelscope下载:
1 2
| pip install modelscope modelscope download --model unsloth/Qwen3.5-4B-GGUF Qwen3.5-4B-Q4_K_M.gguf --local_dir ./models
|
常用 GGUF 模型地址:
方式二:Docker 部署
使用官方 Docker 镜像
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| docker pull ghcr.io/ggml-org/llama.cpp:latest
docker run -d \ --name llama-cpp \ -v $(pwd)/models:/models \ -p 8080:8080 \ ghcr.io/ggml-org/llama.cpp:latest \ --model /models/llama2-7b.Q4_0.gguf \ --host 0.0.0.0 \ --port 8080
docker logs -f llama-cpp
|
使用 NVIDIA GPU 加速
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
docker run -d \ --name llama-cpp-gpu \ --gpus all \ -v $(pwd)/models:/models \ -p 8080:8080 \ ghcr.io/ggml-org/llama.cpp:latest \ --model /models/llama2-7b.Q4_0.gguf \ --host 0.0.0.0 \ --port 8080 \ --cuda
|
三、使用方法
命令行运行
1 2 3 4 5 6 7 8 9 10 11 12 13
| ./build/bin/llama-cli -m ./models/llama2-7b.Q4_0.gguf
./build/bin/llama-cli \ -m ./models/llama2-7b.Q4_0.gguf \ -t 8 \ -n 256 \ -c 4096 \ -p "你好,请介绍一下自己"
echo "你好" | ./build/bin/llama-cli -m ./models/llama2-7b.Q4_0.gguf
|
服务器模式
1 2 3 4 5 6 7 8 9 10 11 12
| ./build/bin/llama-server \ -m ./models/llama2-7b.Q4_0.gguf \ --host 0.0.0.0 \ --port 8080
curl http://localhost:8080/completion -d '{ "prompt": "你好", "n_predict": 128, "temperature": 0.7 }'
|
模型量化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| pip install -r requirements.txt
python convert-hf-to-gguf.py /path/to/model \ --output-dir ./models \ --outtype f16
./build/bin/llama-quantize \ ./models/model-f16.gguf \ ./models/model-q4_k_m.gguf \ Q4_K_M
|
Python API 调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import requests
BASE_URL = "http://localhost:8080"
response = requests.post(f"{BASE_URL}/completion", json={ "prompt": "Hello, introduce yourself", "n_predict": 128, "temperature": 0.7, "top_p": 0.9, "stop": ["\n"] })
result = response.json() print(result["content"])
|
使用 llama-cpp-python
1 2 3 4 5
| pip install llama-cpp-python
CMAKE_ARGS="-DGGML_CUDA=ON" pip install llama-cpp-python
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| from llama_cpp import Llama
llm = Llama( model_path="./models/llama2-7b.Q4_0.gguf", n_ctx=4096, n_gpu_layers=-1 )
output = llm( "Q: 你好,请介绍一下自己\nA: ", max_tokens=128, stop=["\n"] )
print(output["choices"][0]["text"])
output = llm.create_chat_completion( messages=[ {"role": "user", "content": "你好"} ], max_tokens=128 )
print(output["choices"][0]["message"]["content"])
|
四、常见问题
编译错误怎么办?
1 2 3 4 5 6 7 8 9
|
sudo apt install build-essential cmake git python3
rm -rf build mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j$(nproc)
|
显存/内存不足?
1 2 3 4 5 6 7 8
|
./build/bin/llama-cli -m model.gguf -c 2048
./build/bin/llama-cli -m model.gguf -t 4
|
如何选择量化级别?
| 量化类型 |
大小 |
质量 |
推荐场景 |
| Q2_K |
~1.5GB |
较低 |
测试、极小设备 |
| Q3_K_M |
~2GB |
一般 |
嵌入式使用 |
| Q4_K_M |
~2.5GB |
良好 |
日常使用(推荐) |
| Q5_K_M |
~3GB |
很好 |
质量优先 |
| Q8_0 |
~5GB |
优秀 |
高质量需求 |
| F16 |
~7GB |
原始 |
研究、精度要求高 |
模型不支持怎么办?
- 检查模型格式是否为 GGUF
- 参考 llama.cpp 的 README.md 查看支持的模型列表
- 某些新模型可能需要最新版本的 llama.cpp
性能优化建议
1 2 3 4 5 6 7 8 9 10
|
./build/bin/llama-cli -m model.gguf -t $(nproc)
|
五、进阶配置
环境变量
| 变量名 |
说明 |
默认值 |
GGML_THREADS |
线程数 |
CPU 核心数 |
LLAMA_MAX_LOADED_MODELS |
最大加载模型数 |
1 |
配置开机自启(Linux 服务器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| sudo nano /etc/systemd/system/llama-cpp.service
[Unit] Description=llama.cpp Server After=network.target
[Service] ExecStart=/path/to/llama-server \ -m /path/to/model.gguf \ --host 0.0.0.0 \ --port 8080 User=your_username Restart=always RestartSec=5
[Install] WantedBy=default.target
sudo systemctl daemon-reload sudo systemctl enable llama-cpp sudo systemctl start llama-cpp
|
多模型服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| sudo nano /etc/systemd/system/llama-cpp.service
[Unit] Description=llama.cpp Server After=network.target
[Service] ExecStart=/root/llama.cpp/build/bin/llama-server \ --models-dir /root/llama.cpp/models \ --host 0.0.0.0 \ --port 8080 User=root Restart=always RestartSec=5
[Install] WantedBy=default.target
sudo systemctl daemon-reload sudo systemctl enable llama-cpp sudo systemctl start llama-cpp
|
适用场景
- 💻 高性能本地推理
- 📱 嵌入式设备 AI
- 🔧 模型转换与量化
- 🏢 私有化部署
- 🎓 AI 研究与教学
- ⚙️ 定制化 AI 应用