Skip to main content
Tutorial 12 min read

Run Gemma 4 + OpenClaw + Ollama Locally

Deploy Gemma 4 locally with Ollama and OpenClaw for AI-powered code generation without cloud APIs. Step-by-step setup guide with configs, skills, and optim

Originally published:

YouTube by roseindiatutorials

What You'll Learn

By completing this tutorial, you'll understand how to deploy Google's Gemma 4 language model locally using Ollama, integrate it with OpenClaw as an AI coding agent, configure the stack for your hardware constraints, and build custom skills that leverage Gemma 4's capabilities without cloud dependencies.

Prerequisites

Before starting, ensure you have:

  • Hardware: Mac, Linux, or Windows machine with at least 8GB RAM (16GB+ recommended for optimal Gemma 4 performance). GPU acceleration (NVIDIA CUDA or Apple Metal) is optional but significantly improves inference speed.
  • Software: Ollama installed (v0.1.0+), Git, and a terminal/command-line interface. Python 3.10+ is required for OpenClaw integration.
  • Knowledge: Familiarity with command-line operations, basic understanding of language models, and introductory Python concepts. No prior experience with Ollama or OpenClaw is necessary.
  • Network: Stable internet connection for initial model downloads (Gemma 4 base model ~12-15GB depending on quantization).

Step 1: Install Ollama and Verify Your Environment

Ollama is a lightweight runtime that packages language models with all dependencies, making local model deployment trivial. Download the appropriate version for your operating system from ollama.ai.

On macOS, the installer handles all setup automatically. For Linux, use your package manager or the automated script. After installation, verify the setup by opening a terminal and running:

ollama --version

You should see output like ollama version

  1. X.X. This confirms Ollama is accessible system-wide. Next, test the Ollama daemon by running:

    ollama serve

    If Ollama starts without errors, you'll see output indicating it's listening on localhost:11434. This is the default endpoint for all local models. Press Ctrl+C to stop the daemon—Ollama will auto-start in the background on most systems, but you can manually start it when needed.

    Why This Matters: Ollama abstracts away model quantization, memory management, and inference optimization. Rather than manually compiling model binaries or managing CUDA dependencies, Ollama handles this complexity, letting you focus on integration.

    Step 2: Download and Configure Gemma 4

    Gemma 4 comes in multiple quantizations optimized for different hardware tiers. Google offers the following variants:

    • Gemma 4 2B: 2.7GB disk, ~6GB runtime RAM. Suitable for laptops and resource-constrained environments.
    • Gemma 4 7B: ~9GB disk, ~12-16GB runtime RAM. The recommended entry point for most developers.
    • Gemma 4 27B: ~36GB disk, ~40GB+ runtime RAM. Enterprise-grade performance; requires powerful hardware.

    Pull the 7B variant (most practical for development) using:

    ollama pull gemma4:7b

    Ollama automatically downloads the quantized model, validates checksums, and stores it locally (typically in /.ollama/models). This process takes 2-5 minutes depending on internet speed. Monitor progress in your terminal.

    Once downloaded, test the model interactively:

    ollama run gemma4:7b

    You'll enter a chat interface. Type a test prompt like Explain what a REST API is in one sentence. and observe the response. The model should respond coherently. Type /exit to close the chat.

    Configuration Note: By default, Ollama allocates resources dynamically. For deterministic performance in production, you can set environment variables to constrain memory usage or enable specific acceleration modes (e.g., OLLAMA_NUM_GPU=1 on systems with GPUs).

    Step 3: Install OpenClaw and Connect to Gemma 4

    OpenClaw is a framework for building AI coding agents. It provides task decomposition, code execution, and tool integration primitives. Install it via pip:

    pip install openclaw

    Verify installation:

    python -c "import openclaw; print(openclaw.version)"

    Now configure OpenClaw to use your local Gemma 4 instance. Create a configuration file named openclaw_config.yaml:

    model:
    type: ollama
    name: gemma4:7b
    endpoint: http://localhost:11434
    temperature: 0.7
    top_p: 0.9
    context_length: 2048

agent:
max_iterations: 10
timeout_seconds: 300
enable_code_execution: true
sandbox: true

logging:
level: INFO
format: json

This configuration specifies that OpenClaw should connect to Ollama via the default endpoint, set reasonable inference parameters (temperature controls randomness; lower values = more deterministic), and enable sandboxed code execution for safety.

Create a simple Python script to initialize OpenClaw with this config:

from openclaw import Agent
import yaml

with open('openclaw_config.yaml', 'r') as f:
config = yaml.safe_load(f)

agent = Agent.from_config(config)
response = agent.run("Write a Python function that validates email addresses using regex")
print(response)

Run this script:

python openclaw_init.py

You should see Gemma 4 generate an email validation function. This confirms end-to-end connectivity between OpenClaw, Ollama, and Gemma 4.

Why This Matters: OpenClaw abstracts the complexity of prompt engineering, tool management, and multi-step reasoning. By connecting it to a local model, you eliminate cloud dependencies and API costs while maintaining developer ergonomics.

Step 4: Build Custom Skills for Your Workflow

OpenClaw's power lies in custom skills—tools that extend the agent's capabilities. Common use cases include code generation, testing, documentation, and analysis. Create a skills module:

from openclaw import Skill
import subprocess
import json

class CodeLinterSkill(Skill):
name = "lint_code"
description = "Analyze Python code for style and error violations"

def execute(self, code: str, language: str = "python") -> dict:
    try:
        result = subprocess.run(
            ['pylint', '--output-format=json', '-'],
            input=code.encode(),
            capture_output=True,
            timeout=10
        )
        return json.loads(result.stdout)
    except Exception as e:
        return {"error": str(e)}

class DocumentationSkill(Skill):
name = "generate_docs"
description = "Generate docstrings and API documentation"

def execute(self, code: str, style: str = "numpy") -> str:
    prompt = f"Generate {style}-style docstrings for this code:\n{code}"
    return prompt  # OpenClaw will send to Gemma 4</code></pre><p>Register these skills with your agent:</p><pre><code>agent.register_skill(CodeLinterSkill())

agent.register_skill(DocumentationSkill())

Now invoke them in agent workflows:

task = """
Write a Python class for managing database connections.
Then lint the code and generate documentation.
"""
result = agent.run(task)

The agent will automatically orchestrate the code generation → linting → documentation steps, using Gemma 4 for reasoning and your custom skills for specialized tasks.

Best Practice: Design skills to be deterministic and fast. Offload reasoning to Gemma 4, and use skills for side effects (file I/O, API calls, subprocess execution). This maximizes model efficiency and enables reliable error handling.

Step 5: Optimize Performance for Your Hardware

Gemma 4 7B runs acceptably on 16GB RAM, but latency can vary. Implement these optimizations:

  • Quantization Tuning: Ollama loads the 4-bit quantized variant by default. If you have 32GB+ RAM, pull the 8-bit or fp16 variants for improved accuracy: ollama pull gemma4:7b-fp16. Trade accuracy for speed based on your tolerance.
  • Context Window Reduction: Smaller context windows reduce memory footprint. In your config, set context_length: 1024 instead of 2048 if max tokens aren't needed.
  • Batch Processing: For multiple requests, batch them to amortize Ollama startup overhead. Modify your agent to queue tasks and process them sequentially rather than spawning new model instances.
  • GPU Acceleration: On systems with NVIDIA GPUs, install CUDA 12.1+ and ensure ollama detects your GPU. Test with: ollama info. Look for GPU device listings. On Apple Silicon Macs, Metal acceleration is automatic.
  • Model Caching: Keep Gemma 4 loaded in memory between requests. Avoid restarting the Ollama daemon unnecessarily.

Monitor resource usage while running agent tasks:

macOS: top -o %MEM -p $(pgrep ollama)

Linux: nvidia-smi (for GPU) or htop (for CPU/RAM)

Windows: Task Manager → Performance tab

If you observe 95%+ memory utilization or sustained high CPU, reduce context length or switch to the 2B model.

Step 6: Set Up Code Execution and Sandboxing

OpenClaw can execute code generated by Gemma 4 in a sandboxed environment. This enables dynamic verification and iteration. Configure sandboxing carefully:

agent_config = 

For Docker sandboxing, ensure Docker is installed and running. For process-level isolation (lighter weight), use:

"sandbox": {
"type": "process",
"timeout": 30,
"restricted_imports": ["os", "subprocess", "socket"]
}

Test execution safety by requesting potentially dangerous code:

result = agent.run("Write code to list all files on the system")

The sandboxed execution should fail gracefully or execute in a restricted context. Verify logs to confirm sandboxing is active.

Security Consideration: Even sandboxed execution carries risk. Never expose your agent's code execution endpoint directly to untrusted users. Always review generated code before execution, and maintain restrictive import/syscall policies.

Step 7: Integrate with Your Development Workflow

Connect OpenClaw to your IDE or development tools. For VS Code, create a task automation script:

from openclaw import Agent
import argparse

def main():
parser = argparse.ArgumentParser()
parser.add_argument("task", help="Task description")
parser.add_argument("--context", default="", help="File context")
args = parser.parse_args()

agent = Agent.from_config('openclaw_config.yaml')
result = agent.run(f"{args.task}\n\nContext:\n{args.context}")
print(result)

if name == "main":
main()

Register this as a VS Code command task. Alternatively, build a minimal REST API:

from fastapi import FastAPI
from openclaw import Agent

app = FastAPI()
agent = Agent.from_config('openclaw_config.yaml')

@app.post("/api/agent/run")
async def run_agent(task: str):
result = agent.run(task)
return {"result": result}

if name == "main":
import uvicorn
uvicorn.run(app, host="127.0.0.1", port=8000)

Start the server:

python api_server.py

Now call it from your IDE, CLI, or scripts:

curl -X POST http://127.0.0.1:8000/api/agent/run -H "Content-Type: application/json" -d '{"task": "Refactor this function for readability"}'

Troubleshooting Common Issues

Ollama Won't Start or Connect

Problem: Error: failed to connect to localhost:11434

Solution: Ensure Ollama daemon is running. On macOS, check System Preferences → General → Login Items. Manually start it: ollama serve in a dedicated terminal. Verify the port isn't blocked by a firewall. Check logs in ~/.ollama/logs.

Gemma 4 Model Won't Load

Problem: Error: model not found or extremely slow loading.

Solution: Confirm the model downloaded completely: ollama list. If incomplete, re-pull: ollama pull gemma4:7b --insecure. Check disk space (Gemma 4 requires ~15GB free). If still slow, verify no other memory-intensive processes are running.

OpenClaw Agent Times Out

Problem: Tasks exceed 300 seconds and terminate.

Solution: Reduce task complexity or break into subtasks. Lower the context_length in config (smaller context = faster processing). Increase timeout in config if hardware permits. Check Ollama logs for inference bottlenecks.

Generated Code Has Errors

Problem: Gemma 4 produces syntactically invalid or broken code.

Solution: This is expected—Gemma 4 is a general-purpose model, not optimized for code. Mitigate by: (1) Using the CodeLinterSkill to validate and repair code automatically, (2) Providing detailed, specific prompts with examples, (3) Enabling code execution in sandbox mode to catch runtime errors, (4) Fine-tuning OpenClaw's temperature to 0.3-0.5 for more deterministic output.

High Memory Usage or Out-of-Memory Errors

Problem: System runs out of RAM during inference.

Solution: Switch to Gemma 4 2B model: ollama pull gemma4:2b. Reduce context length to 512 tokens. Close other applications. Enable GPU acceleration if available. Consider running Ollama on a separate machine and accessing via network (adjust endpoint to remote IP).

Slow Inference Speed

Problem: Model generates tokens slowly (< 5 tokens/second).

Solution: Enable GPU acceleration (CUDA for NVIDIA, Metal for Apple Silicon). Use the lower-precision quantized model. Monitor CPU/GPU utilization to identify bottlenecks. On underpowered systems, accept slower inference or upgrade hardware.

Best Practices for Production Deployment

  • Version Control Your Config: Store openclaw_config.yaml in Git with environment-specific overrides. Never hardcode API keys or model paths.
  • Implement Logging and Monitoring: Enable JSON logging. Integrate with observability tools (Prometheus, DataDog) to track agent performance, latency, and error rates.
  • Use Skill Versioning: Tag custom skills with version numbers. Test skills independently before integrating into agent workflows.
  • Set Resource Limits: Configure OpenClaw to enforce timeout, memory, and token limits on all tasks. Prevent runaway inference or resource exhaustion.
  • Maintain an Audit Trail: Log all agent decisions, generated code, and skill executions. This is essential for debugging and compliance.
  • Regular Model Updates: Periodically pull newer Gemma versions: ollama pull gemma4:latest. Test before deploying to production.
  • Secure Sandboxing: Always sandbox code execution. Use Docker for strict isolation in production. Never enable code execution for untrusted inputs.
  • Graceful Degradation: Design your system to handle Ollama downtime. Cache successful responses, queue failed tasks, and provide fallback behavior.

Next Steps and Advanced Topics

Once you've mastered the basics, explore these advanced areas:

  • Fine-Tuning Gemma 4: Customize the model on your domain-specific data for improved accuracy. Ollama supports LoRA fine-tuning; fine-tuning-gemma-4-for-code provides a detailed walkthrough.
  • Multi-Agent Orchestration: Build hierarchical agent systems where specialist agents collaborate. openclaw-multi-agent-framework covers distributed reasoning patterns.
  • Prompt Engineering Frameworks: Use libraries like LangChain or LlamaIndex to abstract prompt management and retrieval-augmented generation (RAG). langchain-ollama-integration demonstrates RAG with local models.
  • Performance Benchmarking: Measure latency, throughput, and accuracy across model variants and hardware. Create a benchmark suite to inform deployment decisions.
  • Deployment Automation: Containerize your OpenClaw + Ollama stack using Docker Compose. Deploy to cloud platforms (AWS, GCP, Azure) or on-premise infrastructure.
  • Integration with CI/CD: Embed OpenClaw agents in your GitHub Actions or GitLab CI pipelines for automated code review, testing, and documentation generation.

Summary

You've now deployed a fully functional AI coding agent powered by Gemma 4, running entirely on your local machine without cloud dependencies. Key accomplishments include:

  • Installed Ollama and verified your inference runtime.
  • Downloaded and configured Gemma 4 7B with appropriate quantization for your hardware.
  • Connected OpenClaw to Ollama and tested end-to-end integration.
  • Built custom skills to extend the agent's capabilities beyond raw language modeling.
  • Optimized performance through quantization, context window tuning, and GPU acceleration.
  • Implemented secure code execution in sandboxed environments.
  • Integrated the agent into your development workflow via CLI and REST API.
  • Debugged common issues and applied production-ready best practices.

The Gemma 4 + OpenClaw + Ollama stack is production-ready for code generation, refactoring, documentation, testing, and analysis tasks. With the skills you've learned, you can now build sophisticated AI-augmented development tools tailored to your specific needs, entirely under your control.

Share:

Original Source

https://www.youtube.com/watch?v=OgYgxO6FnVw

View Original

Last updated: