Insights AI News How to run Gemma 4 locally and build offline AI agents
post

AI News

08 Apr 2026

Read 11 min

How to run Gemma 4 locally and build offline AI agents

how to run Gemma 4 locally to build private high-performance offline agents that run on your hardware.

Learn how to run Gemma 4 locally in minutes. Pick the right model, grab the weights under Apache 2.0, and launch on a GPU PC, Mac, or Android device. Then wire up tools for function calling, JSON output, and offline RAG to build fast, private, agentic apps. Gemma 4 is Google’s most capable open model family you can run on your own hardware. It comes in four sizes (E2B, E4B, 26B MoE, 31B Dense) and excels at reasoning, code, vision, audio, and long context. This guide shows how to run Gemma 4 locally on desktop GPUs, Macs, and Android devices, then turn it into a reliable offline agent.

How to run Gemma 4 locally: Quick start

1) Choose your model size

  • E2B (Effective 2B): Best for phones, Raspberry Pi, and IoT. Fast, low power, multimodal with audio.
  • E4B (Effective 4B): Stronger edge model for on-device agents, dictation, and visual understanding.
  • 26B MoE: Great speed-to-quality. Activates about 3.8B params per token for fast tokens/sec.
  • 31B Dense: Highest quality in the family. Strong for coding, reasoning, and fine-tuning on a workstation.
  • Tips:
  • Use E2B/E4B for mobile or very small devices and near-zero latency.
  • Use 26B MoE if you want fast responses on consumer GPUs.
  • Use 31B Dense when quality matters most and you have more VRAM or can quantize.
  • 2) Prepare your runtime

    If you wonder how to run Gemma 4 locally on Windows or Mac, choose a runtime that fits your hardware and workflow.
  • Transformers + an inference engine (vLLM or Text Generation Inference): Flexible, great for GPUs and long-context workloads.
  • llama.cpp (GGUF): Lightweight, runs on CPU and GPU backends (CUDA/ROCm/Metal). Ideal for laptops and Macs.
  • Ollama: Simple local runner with one-line model pulls and an HTTP API. Good for quick starts and agents.
  • Android AICore Developer Preview: Prototype on-device agents for E2B/E4B, with offline multimodal support.
  • 3) Get the weights (Apache 2.0)

  • Download from official Gemma 4 repositories on Hugging Face for E2B, E4B, 26B MoE, and 31B Dense.
  • Pick “it” instruction-tuned models for chat/agents, or base models if you plan to fine-tune.
  • For llama.cpp or Ollama, use trusted GGUF conversions. For GPU-first Python stacks, use bfloat16 or 8/4-bit quantized weights (e.g., GPTQ, AWQ) from reputable maintainers.
  • Review each repo’s README for exact commands and supported context windows (128K on edge, up to 256K on larger models).
  • 4) Run your first prompt

  • Transformers: Install the libraries, load the tokenizer and model (bf16 or 4/8-bit), set generation params (temperature, max tokens), then generate.
  • vLLM or TGI: Start a local server that exposes an OpenAI-compatible API, then call it from your app or curl.
  • llama.cpp: Download a GGUF file, run the interactive CLI or start the server mode, then send prompts via HTTP.
  • Ollama: Pull the Gemma 4 build, then “ollama run …” or call the API from your app.
  • Android AICore: Use the Developer Preview samples to run E2B/E4B offline for text, vision, and audio tasks.
  • 5) Speed and memory tips

  • Quantize for consumer hardware. 4-bit or 8-bit often gives large speed and memory wins with small quality tradeoffs.
  • Prefer 26B MoE when you want fast tokens/sec; it activates fewer experts per token.
  • Use streaming to show tokens as they generate and improve perceived latency.
  • Tune context length. 128K–256K is powerful, but long windows use more RAM and slow generation. Keep prompts lean.
  • Batch multiple prompts when throughput matters. For single-user chat, keep batch small to reduce latency.
  • Build offline AI agents on your laptop and phone

    Gemma 4 includes native features for function calling, structured JSON, and system instructions. This makes it easy to build agents that call tools, follow rules, and return clean outputs without cloud access.

    Function calling and tools

  • Define your tools with names, clear descriptions, and JSON schemas for arguments.
  • Ask the model to pick the right tool and return function_call JSON. Validate it, then execute the tool offline.
  • Handle errors with retries and short clarifying messages. Keep tool outputs short to save tokens.
  • Use frameworks like LangChain or LlamaIndex if you want ready-made routing, memory, and tool management.
  • Private RAG and long context

  • For small corpora, drop full docs into the 128K–256K window and let the model read them directly.
  • For larger corpora, build a local RAG stack: embed docs, index with FAISS or SQLite, retrieve top chunks, and ground the answer.
  • Store embeddings and documents locally for full privacy and offline work.
  • Keep chunk sizes coherent (e.g., paragraphs) and set a modest top-k to control latency.
  • Multimodal skills at the edge

  • All Gemma 4 models understand images and video frames. Use them for OCR, UI understanding, and chart Q&A.
  • E2B/E4B accept audio input for offline speech recognition and voice commands.
  • Combine sensors and tools: take a photo, parse its content, then trigger local actions with function calls.
  • Evaluate and guard

  • Write small tests for prompts, tools, and JSON outputs.
  • Measure agent accuracy on math, code, and reasoning tasks that match your use case.
  • Add safety checks. Filter tool outputs and user inputs. Validate JSON before execution.
  • Why local Gemma 4 stands out

  • Quality per parameter: 31B ranks #3 and 26B ranks #6 among open models on Arena AI’s text leaderboard, often beating much larger models.
  • Edge-first design: E2B/E4B are built for low latency and multimodality on phones and small devices.
  • Long context: 128K on edge and up to 256K on larger models supports full repos and long docs in one go.
  • Global reach: Trained across 140+ languages for inclusive apps.
  • Open and commercial-friendly: Apache 2.0 license supports private, on-prem, and offline deployments with full control.
  • Example local setups

  • Consumer GPU PC: Use 26B MoE quantized for a fast coding assistant in your IDE. Serve via vLLM and call tools over a local API.
  • MacBook with Apple Silicon: Run E4B or a quantized 26B via llama.cpp. Build a private RAG chatbot with local PDFs.
  • Android phone: Prototype an on-device voice agent with E2B/E4B through the AICore Developer Preview. Keep all data on-device.
  • Workstation or server GPU: Run 31B Dense for top quality, or fine-tune with LoRA on domain data for specialized agents.
  • Gemma 4 makes advanced reasoning, multimodal I/O, and agentic workflows possible entirely on your hardware. With the right size, runtime, and a simple tool layer, you get fast, private apps that work anywhere. Now you know how to run Gemma 4 locally and turn it into a dependable offline agent.

    (Source: https://blog.google/innovation-and-ai/technology/developers-tools/gemma-4/)

    For more news: Click Here

    FAQ

    Q: What devices and hardware can I use to run Gemma 4 locally? A: If you wonder how to run Gemma 4 locally, you can use GPU PCs, Macs (including Apple Silicon), Android phones, Raspberry Pi, NVIDIA devices and Jetson Orin Nano, as shown in the guide. Unquantized bfloat16 26B/31B weights fit on a single 80GB NVIDIA H100 GPU, while quantized versions and the E2B/E4B edge models run on consumer GPUs and small devices for near-zero latency. Q: How should I choose a Gemma 4 model size for my local use case? A: Use E2B for phones, Raspberry Pi and IoT when you need low power, multimodal and audio input, and E4B for stronger on-device agents and visual understanding. Pick 26B MoE for a speed-to-quality tradeoff and fast tokens-per-second, and 31B Dense for the highest quality, coding, reasoning and fine-tuning when you have more VRAM or can quantize. Q: Which runtimes and inference engines are recommended for local Gemma 4 deployments? A: Transformers plus an inference engine (vLLM or Text Generation Inference) work well for GPU-first and long-context workloads, while llama.cpp (GGUF) is a lightweight option that runs on CPU and GPU backends including CUDA, ROCm and Metal. Ollama provides simple local model pulls and an HTTP API for quick starts, and the Android AICore Developer Preview supports prototyping on-device E2B/E4B agents. Q: Where do I download Gemma 4 weights and which formats should I use for local inference? A: Download official Gemma 4 weights from the model repositories on Hugging Face, choosing “it” instruction-tuned builds for chat/agents or base models if you plan to fine-tune. For llama.cpp use trusted GGUF conversions, and for GPU-first Python stacks use bfloat16 or 4/8-bit quantized weights (e.g., GPTQ, AWQ) from reputable maintainers. Q: What are the basic steps to run my first prompt with Gemma 4 locally? A: Install the libraries, load the tokenizer and model (bf16 or quantized), set generation parameters like temperature and max tokens, then generate locally or via a local server. Alternatively, start vLLM or TGI to expose an OpenAI-compatible API, use llama.cpp’s interactive CLI or server mode, or run the model with Ollama or Android AICore samples. Q: How do I build an offline agent with function calling and tools using Gemma 4? A: Define tools with names, clear descriptions and JSON schemas, ask the model to return function_call JSON, validate the output, then execute the tool offline and handle errors with retries and short clarifying messages. You can use frameworks like LangChain or LlamaIndex for routing, memory and tool management to simplify agent construction. Q: What tips improve latency and reduce memory when running Gemma 4 locally? A: Quantize models to 4-bit or 8-bit for large speed and memory wins with small quality tradeoffs, prefer 26B MoE for fast tokens-per-second, and use streaming to show tokens as they generate. Also tune context length—128K–256K windows use more RAM and slow generation—batch prompts for throughput, and keep single-user chats small to reduce latency. Q: How can I set up a private RAG workflow and handle long documents offline with Gemma 4? A: For small corpora you can drop full documents into the 128K–256K context window so the model reads them directly, while for larger corpora build a local RAG stack by embedding documents, indexing with FAISS or SQLite, retrieving top chunks, and grounding answers. Store embeddings and documents locally for privacy, keep chunk sizes coherent and use a modest top-k to control latency.

    Contents