Deploy OpenClaw AI on Raspberry Pi: Our Open Source Guide


In this tutorial, we’ll detail how to deploy the OpenClaw AI assistant platform on different Raspberry Pi models. Whether you have a high-performance Raspberry Pi 4B or a classic older Raspberry Pi 2, this guide will provide you with corresponding deployment solutions.

Comparison of Different Raspberry Pi Models

Hardware Specifications

Model CPU RAM Network USB Use Case
Raspberry Pi 4B Cortex-A72 Quad-core 1.5GHz 2GB/4GB/8GB Gigabit Ethernet, WiFi USB 3.0 Recommended, best performance
Raspberry Pi 3B+ Cortex-A53 Quad-core 1.4GHz 1GB Ethernet, WiFi USB 2.0 Medium load
Raspberry Pi Zero 2 W Cortex-A53 Single-core 1GHz 512MB WiFi USB 2.0 Lightweight applications
Raspberry Pi 2 Cortex-A7 Quad-core 900MHz 1GB Ethernet USB 2.0 Basic functionality

Selection Recommendations

General System Preparation

Hardware Requirements

System Installation

Option 1: Raspberry Pi OS (Recommended for beginners)

  1. Download Raspberry Pi Imager
  2. Select Raspberry Pi OS (64-bit)
  3. Choose SD card and write image

Option 2: DietPi (Recommended for advanced users, especially older models)

System Initialization

# Update system
sudo apt update && sudo apt upgrade -y

# Expand filesystem
sudo raspi-config
# Select "Advanced Options" -> "Expand Filesystem"

Raspberry Pi 4B and Above Deployment

Environment Configuration

# Install basic packages
sudo apt install -y \
    build-essential \
    curl \
    wget \
    git \
    vim \
    htop \
    python3-dev \
    python3-pip \
    python3-venv \
    libffi-dev \
    libssl-dev \
    zlib1g-dev

# Install Node.js
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts

Python Environment

# Create virtual environment
python3 -m venv ~/openclaw-env
source ~/openclaw-env/bin/activate
pip install --upgrade pip setuptools wheel

OpenClaw Installation

# Clone and install
mkdir -p ~/projects
cd ~/projects
git clone https://github.com/openclaw/openclaw.git
cd openclaw

source ~/openclaw-env/bin/activate
pip install -e .

Configuration File (For Raspberry Pi 4B)

# ~/.openclaw/config.yaml
server:
  host: 0.0.0.0
  port: 3000
  workers: 2  # Pi 4B can use 2 worker processes
  timeout: 300
  debug: false

models:
  defaults:
    text: "gpt-3.5-turbo"
    embedding: "text-embedding-3-small"

ollama:
  enabled: true
  host: "http://localhost:11434"
  model: "llama2:7b"  # Model suitable for Pi 4B

database:
  type: "sqlite"
  sqlite:
    path: "~/.openclaw/data.db"

cache:
  type: "redis"
  redis:
    host: "localhost"
    port: 6379
    db: 0

resources:
  memory_limit: "1G"
  cpu_limit: 0.8
  max_concurrent_requests: 4

logging:
  level: "INFO"
  file: "~/.openclaw/logs/openclaw.log"

Raspberry Pi 2 Deployment (Resource-Optimized Version)

System-Level Optimization

# Maximize swap space
sudo dphys-swapfile swapoff
sudo nano /etc/dphys-swapfile
# Set CONF_SWAPSIZE=1024
# Set CONF_MAXSWAP=2048
sudo dphys-swapfile setup
sudo dphys-swapfile swapon

# Disable more services to save resources
sudo systemctl disable bluetooth
sudo systemctl disable triggerhappy
sudo systemctl disable avahi-daemon
sudo systemctl disable alsa-state
sudo systemctl disable cups
sudo systemctl disable cups-browsed

# Adjust GPU memory allocation
echo "gpu_mem=16" | sudo tee -a /boot/config.txt

Lightweight Environment Configuration

# Install lightweight Node.js version
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 16  # Install older but stable version
nvm use 16

# Lightweight Python environment
python3 -m venv ~/openclaw-env --without-pip
source ~/openclaw-env/bin/activate
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py --no-cache-dir

pip install --upgrade pip setuptools wheel
pip install --upgrade \
    pip \
    requests \
    pyyaml \
    python-dotenv \
    flask \
    gevent \
    redis \
    pillow

Configuration File (For Raspberry Pi 2)

# ~/.openclaw/config.yaml
server:
  host: 0.0.0.0
  port: 3000
  workers: 1  # Single worker process
  timeout: 600  # Longer timeout period
  debug: false

models:
  defaults:
    text: "gpt-3.5-turbo"  # Must use API models

ollama:
  enabled: false  # Pi 2 cannot run local models

database:
  type: "sqlite"
  sqlite:
    path: "~/.openclaw/data.db"
    timeout: 60

cache:
  type: "memory"
  memory:
    max_size: 50MB  # Minimal cache

resources:
  memory_limit: "512M"  # Strict memory limit
  cpu_limit: 0.5
  max_concurrent_requests: 1  # Single request processing
  slow_down_requests: true

network:
  timeout: 300
  retry_attempts: 3
  retry_delay: 5

logging:
  level: "INFO"
  file: "~/.openclaw/logs/openclaw.log"

Generic Configuration Script

Create a script that can select configuration based on hardware:

# ~/setup_openclaw.sh
#!/bin/bash

# Detect Raspberry Pi model and apply corresponding configuration
detect_raspberry_pi_model() {
    # Get CPU info
    CPU_INFO=$(cat /proc/cpuinfo | grep "model name" | head -1 | cut -d ":" -f 2)
    
    if [[ $CPU_INFO == *"ARMv7"* ]]; then
        # Check CPU cores and frequency
        CORES=$(nproc)
        FREQUENCY=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq 2>/dev/null | head -c 3)
        
        if [[ $FREQUENCY -gt 1400 ]]; then
            echo "pi4"  # Raspberry Pi 4B+
        elif [[ $FREQUENCY -gt 1000 ]]; then
            echo "pi3"  # Raspberry Pi 3B+
        else
            echo "pi2"  # Raspberry Pi 2 or Zero 2 W
        fi
    else
        echo "unknown"
    fi
}

# Create config directory
mkdir -p ~/.openclaw

# Select config based on model
MODEL_TYPE=$(detect_raspberry_pi_model)
echo "Detected Raspberry Pi type: $MODEL_TYPE"

case $MODEL_TYPE in
    "pi4")
        echo "Setting up for Raspberry Pi 4B+"
        cat > ~/.openclaw/config.yaml << 'EOF'
server:
  host: 0.0.0.0
  port: 3000
  workers: 2
  timeout: 300
  debug: false

models:
  defaults:
    text: "gpt-3.5-turbo"
    embedding: "text-embedding-3-small"

ollama:
  enabled: true
  host: "http://localhost:11434"
  model: "llama2:7b"

database:
  type: "sqlite"
  sqlite:
    path: "~/.openclaw/data.db"

cache:
  type: "redis"
  redis:
    host: "localhost"
    port: 6379
    db: 0

resources:
  memory_limit: "1G"
  cpu_limit: 0.8
  max_concurrent_requests: 4

logging:
  level: "INFO"
  file: "~/.openclaw/logs/openclaw.log"
EOF
        ;;
    "pi2")
        echo "Setting up for Raspberry Pi 2/Zero 2 W"
        cat > ~/.openclaw/config.yaml << 'EOF'
server:
  host: 0.0.0.0
  port: 3000
  workers: 1
  timeout: 600
  debug: false

models:
  defaults:
    text: "gpt-3.5-turbo"

ollama:
  enabled: false

database:
  type: "sqlite"
  sqlite:
    path: "~/.openclaw/data.db"
    timeout: 60

cache:
  type: "memory"
  memory:
    max_size: 50MB

resources:
  memory_limit: "512M"
  cpu_limit: 0.5
  max_concurrent_requests: 1
  slow_down_requests: true

network:
  timeout: 300
  retry_attempts: 3
  retry_delay: 5

logging:
  level: "INFO"
  file: "~/.openclaw/logs/openclaw.log"
EOF
        ;;
    *)
        echo "Unknown model, using minimal configuration"
        cat > ~/.openclaw/config.yaml << 'EOF'
server:
  host: 0.0.0.0
  port: 3000
  workers: 1
  timeout: 600
  debug: false

models:
  defaults:
    text: "gpt-3.5-turbo"

ollama:
  enabled: false

database:
  type: "sqlite"
  sqlite:
    path: "~/.openclaw/data.db"
    timeout: 60

cache:
  type: "memory"
  memory:
    max_size: 25MB

resources:
  memory_limit: "256M"
  cpu_limit: 0.4
  max_concurrent_requests: 1

logging:
  level: "INFO"
  file: "~/.openclaw/logs/openclaw.log"
EOF
        ;;
esac

echo "Configuration created for $MODEL_TYPE"

Performance Optimization and Monitoring

System-Level Optimization

# Optimize kernel parameters
sudo tee -a /etc/sysctl.conf << EOF
# File descriptor limits
fs.file-max = 4096

# Network optimization
net.core.somaxconn = 128

# Memory optimization (adjust based on device)
vm.swappiness = 20
vm.vfs_cache_pressure = 200
EOF

sudo sysctl -p

Monitoring Script

# ~/openclaw-monitor.sh
#!/bin/bash

LOG_FILE="$HOME/.openclaw/logs/monitor.log"
MODEL_TYPE=$(bash -c 'cat /proc/cpuinfo | grep "model name" | head -1 | grep -o "[0-9]*" | head -1')

# Set different thresholds based on model
if [ "$MODEL_TYPE" -gt 1400 ]; then
    # Raspberry Pi 4B+
    MAX_MEM_USAGE=80
    MAX_DISK_USAGE=85
else
    # Raspberry Pi 2/Zero 2 W
    MAX_MEM_USAGE=70
    MAX_DISK_USAGE=80
fi

# Check OpenClaw service status
if ! pgrep -f "openclaw" > /dev/null; then
    echo "$(date): OpenClaw service not running, restarting..." >> $LOG_FILE
    source ~/openclaw-env/bin/activate
    nohup ~/openclaw-env/bin/openclaw gateway start > ~/.openclaw/logs/openclaw.log 2>&1 &
fi

# Check memory usage
MEM_USAGE=$(free | awk 'NR==2{printf "%.0f", $3*100/$2}')
if (( $(echo "$MEM_USAGE > $MAX_MEM_USAGE" | bc -l) )); then
    echo "$(date): High memory usage: ${MEM_USAGE}%" >> $LOG_FILE
    
    # Restart service if memory usage is too high
    if [ $MEM_USAGE -gt 90 ]; then
        echo "$(date): Memory critical (${MEM_USAGE}%), restarting OpenClaw" >> $LOG_FILE
        pkill -f openclaw
        sleep 5
        source ~/openclaw-env/bin/activate
        nohup ~/openclaw-env/bin/openclaw gateway start > ~/.openclaw/logs/openclaw.log 2>&1 &
    fi
fi

# Check disk space
DISK_USAGE=$(df / | awk 'NR==2{print $5}' | sed 's/%//')
if [ $DISK_USAGE -gt $MAX_DISK_USAGE ]; then
    echo "$(date): High disk usage: ${DISK_USAGE}%" >> $LOG_FILE
    # Clean up old logs
    find ~/.openclaw/logs -name "*.log" -mtime +7 -delete
fi

Troubleshooting

Common Issues

  1. Insufficient Memory
    # Check memory usage
    free -h
    htop
       
    # Increase swap space
    sudo dphys-swapfile swapoff
    sudo nano /etc/dphys-swapfile
    # Adjust CONF_SWAPSIZE
    sudo dphys-swapfile setup
    sudo dphys-swapfile swapon
    
  2. Service Startup Failure
    # Check service status
    sudo systemctl status openclaw
       
    # View logs
    journalctl -u openclaw -f
    tail -f ~/.openclaw/logs/openclaw.log
    

Summary

This tutorial provides OpenClaw deployment solutions for different Raspberry Pi models:

Through proper configuration and optimization, OpenClaw can run on various Raspberry Pi models to achieve basic AI assistant functionality. Select the appropriate configuration scheme based on your specific hardware to successfully deploy.

Regardless of which Raspberry Pi model you have, you can enjoy the convenience brought by AI assistants while controlling hardware costs.

© 2018 JIAWEI    粤ICP备18035774号