准备工作
在开始安装 node-llama-cpp 之前,先确认本地环境是否就绪。这个库依赖 Node.js 运行时,建议直接使用最新的 LTS 版本。
- Node.js 环境(推荐最新 LTS)
- npm 包管理器
- Git 版本控制工具
快速安装
node-llama-cpp 提供了预构建的二进制文件,覆盖 macOS、Linux 和 Windows 系统,所以日常使用直接通过 npm 就能搞定。
npm install node-llama-cpp
这条命令会自动检测当前系统并下载对应的二进制包。如果当前系统没有现成的预编译文件,它也会尝试自动拉取 llama.cpp 源码进行本地构建,不过这个过程可能需要一些时间。
Windows 系统详细指南
依赖安装
如果在 Windows 上需要源码构建,得准备好 C++ 构建工具。WinGet 是 Win10/11 自带的包管理工具,可以直接一键安装所需组件:
winget install --id Microsoft.VisualStudio.2022.BuildTools --force --override "--add Microsoft.VisualStudio.Component.VC.CMake.Project Microsoft.VisualStudio.Component.VC.CoreBuildTools Microsoft.VisualStudio.Component.VC.Tools.x86.x64 Microsoft.VisualStudio.Component.VC.ATL Microsoft.VisualStudio.Component.VC.ATLMFC Microsoft.VisualStudio.Component.VC.Llvm.ClangToolset Microsoft.VisualStudio.Component.VC.Llvm.Clang Microsoft.VisualStudio.Component.VC.Redist.14.Latest Microsoft.Component.VC.Runtime.UCRTSDK Microsoft.VisualStudio.Component.Windows10SDK Microsoft.VisualStudio.Component.Windows10SDK.20348"
当然,也可以手动去官网下载 Visual C++ Build Tools,记得勾选以下关键项:
- C++ CMake 工具
- C++ Clang 编译器
- Windows 10 SDK
- Windows Universal CRT SDK
ARM64 架构补充
如果你用的是 Windows on Arm 设备,构建指令里还得加上 ARM64 相关的组件,比如 Microsoft.VisualStudio.Component.VC.Tools.ARM64 等,确保编译器支持目标架构。
Linux 系统详细指南
依赖安装
Linux 下主要缺的是编译器和构建工具链。Debian/Ubuntu 系可以用 apt 直接拉取:
sudo apt-get update
sudo apt-get install build-essential cmake git libstdc++6 libgomp1
其中 libgomp1 是为了支持 OpenMP 并行计算,别漏掉。
源码构建
如果预编译包不满足需求,可以手动触发构建流程:
npx node-llama-cpp source download
npx node-llama-cpp source build
Mac 系统详细指南
Xcode 命令行工具
macOS 用户第一步通常是安装 Xcode 的命令行工具,这是编译的基础:

