版本说明: 本教程基于 OpenClaw 最新版本(2025年2月)
官方资源:
- 官网: https://openclaw.ai
- 文档: https://docs.openclaw.ai
- GitHub: https://github.com/openclaw/openclaw
- Discord: https://discord.gg/clawd
| 项目 | 要求 |
|---|---|
| Node.js | ≥ 22 (必须) |
| 操作系统 | macOS / Linux / Windows (WSL2) |
| 包管理器 | npm / pnpm / bun |
| 内存 | 建议 4GB+ |
| 磁盘空间 | 至少 2GB 可用空间 |
bashnode --version
# 输出应该 >= v22.0.0
如果版本过低,请先升级 Node.js:
bash# 使用 nvm 升级(推荐)
nvm install 22
nvm use 22
# 或使用官方安装包
# https://nodejs.org/
这是最简单、最推荐的安装方式:
bash# 全局安装 OpenClaw
npm install -g openclaw@latest
# 验证安装
openclaw --version
bash# 使用 pnpm 安装
pnpm add -g openclaw@latest
# 验证安装
openclaw --version
bash# 使用 bun 安装
bun add -g openclaw@latest
# 验证安装
openclaw --version
适合想参与开发或使用最新功能的用户:
bash# 克隆仓库
git clone https://github.com/openclaw/openclaw.git
cd openclaw
# 安装依赖(推荐使用 pnpm)
pnpm install
# 构建 UI
pnpm ui:build
# 构建项目
pnpm build
# 运行引导向导
pnpm openclaw onboard --install-daemon
# 开发模式(自动重载)
pnpm gateway:watch
bash# 拉取镜像
docker pull openclaw/openclaw:latest
# 运行容器
docker run -d \
--name openclaw \
-p 18789:18789 \
-v ~/.openclaw:/root/.openclaw \
openclaw/openclaw:latest
bashcurl -fsSL https://openclaw.ai/install.sh | bash
详细 Docker 配置请参考:https://docs.openclaw.ai/install/docker
OpenClaw 提供了一个交互式向导,会一步步引导你完成配置:
bash# 运行向导并安装守护进程
openclaw onboard --install-daemon
向导会帮助你配置:
配置文件位于:~/.openclaw/openclaw.json
json5{ "agent": { "model": "anthropic/claude-opus-4-6" } }
json5{ // === AI 模型配置 === "agent": { // 默认使用的 AI 模型 "model": "anthropic/claude-opus-4-6", // 思考级别: off | minimal | low | medium | high | xhigh "thinking": "medium", // 工作空间路径 "workspace": "~/.openclaw/workspace", // 默认行为 "defaults": { // 沙盒模式: off | non-main | all "sandbox": { "mode": "non-main" } } }, // === Gateway 配置 === "gateway": { // 监听地址 "bind": "loopback", // 端口号 "port": 18789, // 详细日志 "verbose": false, // 身份验证 "auth": { "mode": "tailscale", // none | password | tailscale "password": "your-secure-password" }, // Tailscale 配置(可选) "tailscale": { "mode": "off", // off | serve | funnel "resetOnExit": false } }, // === 通讯频道配置 === "channels": { // Telegram 配置 "telegram": { "botToken": "123456:ABCDEF", "allowFrom": ["your_telegram_id"], "groups": { "*": { "requireMention": true } } }, // WhatsApp 配置 "whatsapp": { "allowFrom": ["+86138xxxxxxxx"], "groups": ["*"] }, // Slack 配置 "slack": { "botToken": "xoxb-xxx", "appToken": "xapp-xxx", "allowFrom": ["U123456"] }, // Discord 配置 "discord": { "token": "MTIx...", "allowFrom": ["123456789"], "dmPolicy": "pairing" // pairing | open }, // Signal 配置 "signal": { "account": "+86138xxxxxxxx", "allowFrom": ["+86138xxxxxxxx"] } }, // === 模型提供商配置 === "models": { "anthropic": { "apiKey": "sk-ant-api...", "maxTokens": 8192 }, "openai": { "apiKey": "sk-...", "maxTokens": 4096 } }, // === 工具配置 === "tools": { // 浏览器配置 "browser": { "enabled": true, "color": "#FF4500" }, // Web 搜索配置 "web": { "braveApiKey": "your-brave-api-key" } } }
OpenClaw 支持多种 AI 模型,推荐配置如下:
json5{ "agent": { "model": "anthropic/claude-opus-4-6" }, "models": { "anthropic": { "apiKey": "sk-ant-api03-..." } } }
获取 API Key: https://console.anthropic.com/
json5{ "agent": { "model": "openai/gpt-4o" }, "models": { "openai": { "apiKey": "sk-..." } } }
json5// Moonshot / 月之暗面 "agent": { "model": "moonshot/kimi-k2.5" } // Google Gemini "agent": { "model": "google/gemini-2.0-flash" } // DeepSeek "agent": { "model": "deepseek/deepseek-chat" }
创建 Bot:
/newbot获取你的 Chat ID:
配置:
json5{ "channels": { "telegram": { "botToken": "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11", "allowFrom": ["123456789"] } } }
bash# 登录 WhatsApp
openclaw channels login
按提示扫描二维码即可。
json5{ "channels": { "slack": { "botToken": "xoxb-xxx", "appToken": "xapp-xxx", "allowFrom": ["U12345678"] } } }
json5{ "channels": { "discord": { "token": "MTIx...", "allowFrom": ["123456789"], "dmPolicy": "pairing" } } }
bash# 前台启动(查看日志)
openclaw gateway --verbose
# 指定端口启动
openclaw gateway --port 18789 --verbose
# 后台启动(通过守护进程)
openclaw gateway start
bash# 查看 Gateway 状态
openclaw gateway status
# 运行诊断
openclaw doctor
bash# 发送消息
openclaw message send --to +1234567890 --message "Hello from OpenClaw"
# 与 AI 对话
openclaw agent --message "你好,介绍一下自己"
# 查看帮助
openclaw --help
openclaw gateway --help
在与 AI 对话时可以使用以下命令:
| 命令 | 说明 |
|---|---|
/status | 查看当前会话状态 |
/new 或 /reset | 重置会话 |
/compact | 压缩会话上下文 |
/think <级别> | 设置思考级别 |
/verbose on/off | 开启/关闭详细模式 |
/usage | 查看使用量 |
/restart | 重启 Gateway(仅限拥有者) |
bash# 卸载全局包
npm uninstall -g openclaw
# 清理配置文件(可选)
rm -rf ~/.openclaw
bash# macOS (launchd)
launchctl unload ~/Library/LaunchAgents/com.openclaw.gateway.plist
rm ~/Library/LaunchAgents/com.openclaw.gateway.plist
# Linux (systemd)
systemctl --user stop openclaw-gateway
systemctl --user disable openclaw-gateway
rm ~/.config/systemd/user/openclaw-gateway.service
bash# 停止并删除容器
docker stop openclaw
docker rm openclaw
# 删除镜像
docker rmi openclaw/openclaw:latest
# 清理数据卷(谨慎操作)
rm -rf ~/.openclaw
bash# 删除所有 OpenClaw 相关数据
rm -rf ~/.openclaw
rm -rf ~/Library/Logs/openclaw # macOS 日志
rm -rf ~/.cache/openclaw # 缓存
错误信息:
npm ERR! code EACCES npm ERR! syscall access
解决方案:
bash# 方法1:修改 npm 默认目录
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
npm install -g openclaw@latest
# 方法2:使用 npx(无需全局安装)
npx openclaw@latest ...
# 方法3:使用 sudo(不推荐)
sudo npm install -g openclaw@latest
错误信息:
error openclaw@x.x.x: The engine "node" is incompatible with this module
解决方案:
bash# 使用 nvm 安装 Node.js 22
nvm install 22
nvm use 22
nvm alias default 22
# 验证版本
node --version # 应显示 v22.x.x
解决方案:
bash# 检查全局安装路径
npm config get prefix
# 添加到 PATH
echo 'export PATH="$(npm config get prefix)/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
# 验证
which openclaw
错误信息:
Error: listen EADDRINUSE: address already in use :::18789
解决方案:
bash# 查找占用端口的进程
lsof -i :18789
# 或
netstat -tuln | grep 18789
# 杀死进程
kill -9 <PID>
# 或使用其他端口
openclaw gateway --port 18790
错误信息:
SyntaxError: Unexpected token } in JSON at position ...
解决方案:
bash# 验证 JSON 格式
openclaw doctor
# 或使用在线工具验证 ~/.openclaw/openclaw.json
# 注意:JSON5 允许注释,但必须是有效的 JSON5 格式
解决方案:
bash# macOS
launchctl list | grep openclaw
launchctl start com.openclaw.gateway
# Linux
systemctl --user status openclaw-gateway
systemctl --user restart openclaw-gateway
journalctl --user -u openclaw-gateway -f
错误信息:
401 Unauthorized - Invalid API key
解决方案:
bash# 测试 API Key
curl https://api.anthropic.com/v1/models \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01"
解决方案:
json5// 在配置中添加超时设置 { "agent": { "model": "anthropic/claude-opus-4-6", "timeout": 120000 // 120秒 } }
解决方案:
bash# 重启 Gateway
openclaw gateway restart
# 或使用命令行指定模型
openclaw agent --model openai/gpt-4o --message "Hello"
排查步骤:
/startbashopenclaw gateway --verbose
解决方案:
bash# 重新登录
openclaw channels login
# 检查 creds 文件
ls ~/.openclaw/credentials/whatsapp/
解决方案:
json5{ "channels": { "discord": { "dmPolicy": "pairing", // 改为 "open" 允许所有人 "allowFrom": ["*"] // 允许所有用户 } } }
解决方案:
bash# 确保已安装 Chrome/Chromium
# macOS
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --version
# Linux
google-chrome --version
# 在配置中启用浏览器
{
"browser": {
"enabled": true
}
}
错误信息:
missing_brave_api_key
解决方案:
bashopenclaw configure --section web
# 或手动编辑配置文件
{
"tools": {
"web": {
"braveApiKey": "your-api-key"
}
}
}
解决方案:
json5{ "agent": { // 启用会话修剪 "pruneSessions": true, "maxSessionAge": 3600000 // 1小时 } }
优化建议:
json5{ "agent": { "model": "anthropic/claude-haiku", "streaming": true, "contextWindow": 4000 } }
json5{ "agent": { "model": "anthropic/claude-opus-4-6", "fallbackModels": [ "openai/gpt-4o", "moonshot/kimi-k2.5" ] } }
json5{ "gateway": { "bind": "loopback", "auth": { "mode": "tailscale" }, "tailscale": { "mode": "serve", // serve 或 funnel "resetOnExit": false } } }
json5{ "agent": { "defaults": { "sandbox": { "mode": "non-main", // 非主会话使用 Docker 沙盒 "allowlist": ["bash", "read", "write", "edit"], "denylist": ["browser", "cron", "gateway"] } } } }
bash# 安装技能
openclaw skills install weather
openclaw skills install 1password
# 查看已安装技能
openclaw skills list
# 更新技能
openclaw skills update
bash# 添加定时任务
openclaw cron add --name "daily-summary" \
--schedule "0 9 * * *" \
--message "总结今天的日程"
# 列出定时任务
openclaw cron list
# 删除定时任务
openclaw cron remove --name "daily-summary"
bash# 检查更新
openclaw --version
# 更新到最新版本
npm update -g openclaw@latest
# 或重新安装
npm install -g openclaw@latest
# 运行迁移
openclaw doctor
bash# 运行完整诊断
openclaw doctor
# 查看日志
openclaw logs
# 检查配置
openclaw config get
bash# 开启详细日志
openclaw gateway --verbose
# 调试特定频道
DEBUG=telegram:* openclaw gateway
allowFrom 限制谁可以与 Bot 交互dmPolicy: "pairing"openclaw doctor 检查配置恭喜! 你已经完成了 OpenClaw 的完整安装配置。现在可以开始与你的个人 AI 助手对话了!
如果遇到问题,记得查看第六部分的常见问题,或加入 Discord 社区寻求帮助。
本文作者:JACK WEI
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!