CLAWIST.
🟡 中级阅读时间 15 分钟••作者:Lin6

在树莓派上设置 OpenClaw:完整安装指南

服务器机架——示意图

想要一个在家中 24 小时不间断运行、完全由你掌控的个人 AI 助手吗?树莓派是自托管 OpenClaw 的理想平台——价格实惠、节能高效,而且性能足以运行具备浏览器自动化、消息处理和工作流集成功能的完整 AI 代理。

本指南将带你一步步在 Raspberry Pi 4 或 5 上安装 OpenClaw,从操作系统设置到与你的新 AI 助手进行第一次对话。

为什么选择树莓派来运行 OpenClaw?

与云托管相比,在树莓派上运行 OpenClaw 有以下几个优势:

完全隐私: 你的对话和数据永远不会离开本地网络。不涉及任何第三方服务器(除了对 Claude 的 API 调用)。

随时可用: 全天候 24/7 运行,功耗极低(3-5W,而台式机为 50-100W)。

经济实惠: 在约 $100 的初始硬件投入之后,后续成本只有电费和 Claude API 使用费(通常为每月 $5-20)。

学习平台: 非常适合了解 AI 助手的工作原理——你可以掌控整个技术栈的每一个组件。

家庭自动化中枢: 可连接 Home Assistant、Zigbee 设备和网络附加存储等本地服务,而无需将它们暴露到互联网。

硬件要求

最低配置(Raspberry Pi 4)

  • 型号: 配备 4GB 内存的 Raspberry Pi 4B
  • 存储: 32GB microSD 卡(推荐 UHS-I、A2 等级)
  • 电源: 官方 15W USB-C 电源适配器
  • 网络: 推荐使用以太网连接以保证稳定性

推荐配置(Raspberry Pi 5)

  • 型号: 配备 8GB 内存的 Raspberry Pi 5
  • 存储: 搭配 M.2 HAT 的 64GB NVMe 固态硬盘(比 SD 卡快得多)
  • 电源: 官方 27W USB-C PD 电源适配器
  • 网络: 千兆以太网或 WiFi 6

为什么 Pi 5 更好: 更快的 CPU(性能提升 2-3 倍)、支持 PCIe M.2 NVMe 存储、更高的 I/O 带宽以及更好的散热性能。8GB 型号为浏览器自动化和多个并发任务提供了充裕的余量。

你需要准备的东西

  • Raspberry Pi 4(4GB 以上)或 Pi 5(推荐 8GB)
  • 适合您型号的电源
  • microSD 卡(32GB 以上,Class 10)或 NVMe SSD
  • 以太网线(如有需要也可使用 WiFi)
  • 用于初始设置的电脑
  • 来自 Anthropic 的 Claude API 密钥
  • (可选)带风扇的外壳,以获得更好的散热效果

第 1 步:安装 Raspberry Pi OS

我们将使用 Raspberry Pi OS Lite(64 位)以获得最佳性能——没有桌面图形界面占用资源。

使用 Raspberry Pi Imager

  1. 下载 Raspberry Pi Imager 到您的电脑上

  2. 插入您的 microSD 卡

  3. 打开 Imager 并进行配置:

    • 设备: Raspberry Pi 4 或 5
    • 操作系统: Raspberry Pi OS Lite(64 位)
    • 存储: 您的 microSD 卡
  4. 点击齿轮图标(⚙️)进入高级选项:

    Hostname: openclaw
    Enable SSH: Yes
    Username: pi
    Password: [choose a strong password]
    WiFi: [configure if not using Ethernet]
    Locale: [your timezone]
    
  5. 点击 写入 并等待完成

  6. 将卡插入您的 Pi 并接通电源

首次启动

等待 1-2 分钟完成首次启动,然后通过 SSH 连接到您的 Pi:

ssh pi@openclaw.local

或者,如果 mDNS 无法使用,请从路由器中查找 IP 地址并使用:

ssh pi@192.168.1.XXX

第 2 步:系统准备

登录后,更新系统并安装依赖项:

# Update package lists and system
sudo apt update && sudo apt upgrade -y

# Install Node.js 22 (required for OpenClaw)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs

# Verify Node.js installation
node --version  # Should show v22.x.x
npm --version   # Should show 10.x.x

# Install Git
sudo apt install -y git

# Install Chromium dependencies (for browser automation)
sudo apt install -y \
  chromium-browser \
  chromium-codecs-ffmpeg-extra \
  fonts-liberation \
  libatk-bridge2.0-0 \
  libatk1.0-0 \
  libatspi2.0-0 \
  libcups2 \
  libdbus-1-3 \
  libgdk-pixbuf2.0-0 \
  libgtk-3-0 \
  libnspr4 \
  libnss3 \
  libx11-xcb1 \
  libxcomposite1 \
  libxdamage1 \
  libxrandr2 \
  xdg-utils

# Install optional tools
sudo apt install -y \
  ffmpeg \        # For media processing
  imagemagick \   # For image operations
  trash-cli       # Safe file deletion

第 3 步:安装 OpenClaw

# Install OpenClaw globally
sudo npm install -g openclaw

# Verify installation
openclaw --version

# Initialize OpenClaw workspace
mkdir -p ~/.openclaw/workspace
cd ~/.openclaw/workspace

第 4 步:配置 Claude API

您需要一个来自 Anthropic 的 Claude API 密钥。如果您还没有:

  1. 访问 console.anthropic.com
  2. 创建账户并添加付款方式
  3. 在“账户设置 → API 密钥”下生成一个 API 密钥

使用您的密钥配置 OpenClaw:

# 设置 Claude API 密钥
openclaw config set claude.apiKey "your-api-key-here"

# 配置默认模型(Opus 质量最佳)
openclaw config set claude.defaultModel "claude-opus-4"

# 或使用 Sonnet 以获得更快/更便宜的响应
openclaw config set claude.defaultModel "claude-sonnet-4"

第 5 步:配置浏览器自动化

OpenClaw 使用 Playwright 进行浏览器自动化。安装 Chromium:

# 安装 Playwright
npx playwright install chromium

# 在 OpenClaw 配置中设置浏览器路径
CHROMIUM_PATH=$(find ~/.cache/ms-playwright -name chrome -type f 2>/dev/null | head -1)
openclaw config set browser.executablePath "$CHROMIUM_PATH"

# 启用无头模式(无显示器的 Pi 必须启用)
openclaw config set browser.headless true

# 禁用沙盒(在 Pi 上必须这样做)
openclaw config set browser.noSandbox true

第 6 步:创建工作区文件

OpenClaw 使用 markdown 文件来存储记忆和配置。创建基本结构:

cd ~/.openclaw/workspace

# 创建 SOUL.md(智能体个性)
cat > SOUL.md << 'EOF'
# SOUL.md

I am ClawPi, an AI assistant running on Raspberry Pi. I'm helpful, 
efficient, and focused on practical automation. I specialize in 
home automation, data processing, and local network tasks.

I run 24/7 in my human's home, providing always-available assistance 
for daily tasks, reminders, information lookup, and system management.
EOF

# 创建 USER.md(您的偏好)
cat > USER.md << 'EOF'
# USER.md

Name: [Your Name]
Timezone: [Your Timezone]

Preferences:
- Direct, concise communication
- Focus on practical solutions
- Proactive reminders for calendar events
EOF

# 创建 AGENTS.md(运行规则)
cat > AGENTS.md << 'EOF'
# AGENTS.md

## First Run
1. Read SOUL.md to understand identity
2. Read USER.md for context about the human
3. Check memory/ directory for recent context

## Memory
- Daily logs in memory/YYYY-MM-DD.md
- Long-term memory in MEMORY.md (main session only)

## Execution
- Always complete tasks fully
- Report errors clearly
- Use browser for visual verification
EOF

# 创建 memory 目录
mkdir -p memory

# 创建 TOOLS.md(本地笔记)
cat > TOOLS.md << 'EOF'
# TOOLS.md - Local Notes

## Raspberry Pi
- Model: [Your Pi model]
- Hostname: openclaw
- Location: [Physical location]

## Services
- OpenClaw Gateway: localhost:3000
EOF

第 7 步:启动 OpenClaw Gateway

Gateway 负责管理持久连接并运行您的智能体:

# 在后台启动 Gateway
openclaw gateway start

# 检查状态
openclaw gateway status

# 查看日志
openclaw gateway logs

Gateway 应显示为正在运行。如果看到错误,请检查:

  • Node.js 版本为 22.x: node --version
  • 已设置 Claude API 密钥: openclaw config get claude.apiKey
  • 工作区的权限: ls -la ~/.openclaw/

第 8 步:第一次对话

与您的智能体开始聊天会话:

openclaw chat

尝试以下命令来测试您的设置:

You: Hello! Tell me about yourself.

You: What's the weather today? [tests web search]

You: Navigate to example.com and take a screenshot [tests browser]

You: Create a file called test.txt with "Hello from OpenClaw" [tests file operations]

You: Read test.txt back to me

输入 /exit 即可退出聊天。

第 9 步:启用开机自启

让 OpenClaw 在开机时自动启动:

# 创建 systemd 服务
sudo tee /etc/systemd/system/openclaw-gateway.service > /dev/null << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target

[Service]
Type=simple
User=pi
Environment=HOME=/home/pi
ExecStart=/usr/bin/openclaw gateway start
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
EOF

# 启用并启动服务
sudo systemctl enable openclaw-gateway
sudo systemctl start openclaw-gateway

# 检查状态
sudo systemctl status openclaw-gateway

现在,每当您的 Pi 启动时,OpenClaw 都会自动启动。

性能优化

使用 NVMe 存储(Pi 5)

如果您使用的是 Pi 5,从 microSD 切换到 NVMe 可显著提升性能:

  1. 安装 NVMe HAT 和 SSD
  2. 使用 Raspberry Pi Imager 将操作系统刷入 NVMe
  3. 从 NVMe 启动(在 Raspberry Pi Imager 的高级选项中配置)

浏览器操作和文件 I/O 的速度将提高 5-10 倍。

减少内存占用

# 使用 Sonnet 而非 Opus(占用的上下文更少)
openclaw config set claude.defaultModel "claude-sonnet-4"

# 限制浏览器资源占用
openclaw config set browser.args '["--disable-dev-shm-usage", "--disable-gpu"]'

监控资源

# 检查 CPU 和内存
htop

# 检查磁盘空间
df -h

# 检查温度(保持在 80°C 以下)
vcgencmd measure_temp

如果温度经常超过 75°C,请添加主动散热(风扇)。

集成示例

Home Assistant

将 OpenClaw 连接到 Home Assistant,实现语音控制智能家居:

# 在 ~/.openclaw/workspace/TOOLS.md 中添加:
## Home Assistant
- URL: http://homeassistant.local:8123
- Token: [长期访问令牌]

然后对 OpenClaw 说:"关闭客厅的灯" 或 "卧室的温度是多少?"

计划任务

为周期性任务创建 cron 作业:

# 编辑 crontab
crontab -e

# 添加每天早上 7 点的晨间简报
0 7 * * * /usr/bin/openclaw exec "Generate morning briefing: weather, calendar, news summary"

# 添加每小时的日历检查
0 * * * * /usr/bin/openclaw exec "Check calendar for events in next 2 hours and notify if any"

Discord 机器人

将 OpenClaw 作为 Discord 机器人运行(请参阅我们的 Discord 集成指南):

openclaw message setup --channel discord
# 按照提示添加机器人令牌

现在,您部署在树莓派上的代理可以在 Discord 频道中响应了。

故障排除

网关无法启动

# 检查日志中的错误
openclaw gateway logs

# 常见问题:
# - 端口 3000 被占用:使用 `openclaw config set gateway.port 3001` 更改
# - 权限错误:检查 ~/.openclaw/ 的所有权:`ls -la ~/.openclaw/`
# - 缺少依赖项:使用 `sudo npm install -g openclaw --force` 重新安装

浏览器自动化失败

# 验证 Chromium 是否已安装
chromium-browser --version

# 重新安装 Playwright
npx playwright install chromium --force

# 检查浏览器配置
openclaw config get browser

内存不足错误

配备 4GB 内存的 Pi 4 在处理复杂的浏览器任务时可能会吃力。解决方案:

# 增加交换空间
sudo dphys-swapfile swapoff
sudo nano /etc/dphys-swapfile
# 设置 CONF_SWAPSIZE=2048
sudo dphys-swapfile setup
sudo dphys-swapfile swapon

# 使用 Sonnet 模型(更轻量)
openclaw config set claude.defaultModel "claude-sonnet-4"

# 对内存密集型任务禁用浏览器
openclaw config set browser.enabled false

性能缓慢

  • 使用以太网,而不是 WiFi: 降低 API 调用的延迟
  • 切换到 NVMe(Pi 5): 存储访问速度提升 10 倍
  • 减少浏览器使用: 浏览器自动化对 CPU/内存消耗很大
  • 使用更轻量的模型: 日常任务使用 Sonnet 而不是 Opus

后续步骤

现在 OpenClaw 已在您的 Pi 上运行,可以探索:

  1. 设置消息功能: 连接到 Discord、Telegram 或电子邮件
  2. 创建自定义技能: 构建针对 Pi 的集成(GPIO、传感器等)
  3. 自动化工作流程: 使用 cron 作业执行计划任务
  4. 添加摄像头: 连接 USB 网络摄像头以获取视觉输入
  5. 构建语音控制: 集成唤醒词检测

您的 Raspberry Pi 现在已成为一个强大且始终在线的 AI 助手——完全由您掌控,运行在您的家中,随时准备 24/7 为您提供帮助。

资源

祝您自动化愉快!🤖🥧