AI-generated AI News
08 Apr 2026
Read 12 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
Keep reading, keep building
Three things from our own workshop that fit this topic:
- Diktly Free is our dictation app for Mac: speech becomes text entirely on your own machine, nothing goes to the cloud. Free version, Basic 14.99 €.
- 1000+ Prompts to Level Up Your Life is our most downloaded pack. Pay what you want, including 0.
- OpenAI Agent Mode for Beginners walks you through letting AI agents handle tasks for you. 19.99 $.
Our own products. Tech Moments is our newsletter on AI and tech in plain language, one email, no noise. Subscribe below.
Tech Moments newsletter
A weekly dose of actionable AI knowledge. The sign-up form is provided by Beehiiv and only loads once you accept marketing cookies.
Contents





