Back to All Guides
AI Engineering10 min readPublished: August 15, 2026Updated: August 15, 2026

Google Gemma 2: Running and Fine-Tuning Open AI Models Locally with Ollama and Python

A hands-on guide to Google's open-weights Gemma 2 models (2B, 9B, 27B). Learn local inference with Ollama, 4-bit quantization, and LoRA fine-tuning with Hugging Face.

Vyuhantrix Team
Vyuhantrix Team
Open Source AI & ML · Vyuhantrix

Google's Open Weights Revolution: Gemma 2#

While Gemini models power Google's cloud APIs, Google also releases state-of-the-art open-weights models under the Gemma family. Built from the same research, dataset filtering, and architectural technology used to create Gemini, Gemma 2 delivers class-leading reasoning and coding performance in compact model sizes: 2B, 9B, and 27B parameters.

Because Gemma 2 models are open-weights, you can run them completely offline on your own local laptop or private GPU servers with zero API costs, zero vendor lock-in, and 100% data privacy.


1. Running Gemma 2 Locally with Ollama in 60 Seconds#

[Ollama](https://ollama.com) is the easiest way to run open LLMs locally with automated hardware acceleration (Apple Silicon Metal & NVIDIA CUDA):

bash
# 1. Pull and run Gemma 2 9B model (requires ~6GB RAM)
ollama run gemma2:9b

# 2. Or run the ultra-lightweight Gemma 2 2B model on low-end hardware
ollama run gemma2:2b

2. Calling Local Gemma 2 from Node.js or Python#

Ollama exposes a standard OpenAI-compatible REST API at http://localhost:11434:

typescript
import OpenAI from "openai";

// Connect to local Ollama instance
const localAI = new OpenAI({
  baseURL: "http://localhost:11434/v1",
  apiKey: "ollama", // Required by SDK but unused locally
});

export async function summarizeTextLocally(text: string) {
  const response = await localAI.chat.completions.create({
    model: "gemma2:9b",
    messages: [
      { role: "system", content: "You are a concise technical summarizer." },
      { role: "user", content: text },
    ],
    temperature: 0.2,
  });

  return response.choices[0].message.content;
}

3. Gemma 2 Model Sizes & Hardware Requirements#

Model SizeParametersMinimum RAM (4-bit Quantized)Recommended HardwareIdeal Use Case
Gemma 2 2B2.6 Billion2.5 GB RAMAny Laptop, Raspberry Pi 5, iPhoneOn-device classification, edge devices
Gemma 2 9B9.2 Billion6.0 GB RAMApple M1/M2/M3 (16GB), NVIDIA RTX 3060Code generation, general reasoning, summarization
Gemma 2 27B27.2 Billion18.0 GB RAMApple M-Max (36GB+), NVIDIA RTX 4090 / A100Near GPT-4 level complex analytical reasoning

4. Fine-Tuning Gemma 2 with LoRA in Python (PEFT)#

When you need Gemma 2 to follow specific enterprise formatting or domain terminology, use Low-Rank Adaptation (LoRA) to fine-tune model weights efficiently:

python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TrainingArguments
from peft import LoraConfig, get_peft_model
from trl import SFTTrainer

model_id = "google/gemma-2-9b-it"

# 1. Load tokenizer and 4-bit quantized base model
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.bfloat16,
    device_map="auto"
)

# 2. Configure Parameter-Efficient Fine-Tuning (LoRA)
lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

peft_model = get_peft_model(model, lora_config)
print("Trainable parameters:", peft_model.print_trainable_parameters())

5. Summary Key Takeaways#

  1. Gemma 2 9B Punches Above Its Weight: In independent benchmarks, Gemma 2 9B matches or outperforms models twice its size.
  2. Total Privacy & Offline Execution: Sensitive medical, legal, or proprietary codebases can be processed entirely offline without sending a single byte over the internet.
  3. Open Commercial License: Google licenses Gemma under permissive terms that allow free commercial deployment in applications.

6. Frequently Asked Questions (FAQ)#

Q: Can Gemma 2 run on Apple Silicon Macs with unified memory? Yes, exceptionally well. Using Ollama or MLX (Apple's machine learning framework for silicon), Gemma 2 9B runs at over 35 tokens/second on an M2/M3 Pro Mac with 16GB of unified memory.

Q: What is the difference between Gemma Base and Gemma Instruct? **Gemma Base** models are pre-trained on raw internet text for continuation and completions. **Gemma Instruct (-it)** models have undergone Supervised Fine-Tuning (SFT) and Reinforcement Learning from Human Feedback (RLHF) to follow structured conversational prompts and markdown output instructions. For 99% of applications, always use the instruct version!

Article Note & VerificationThis guide was written and reviewed by the Vyuhantrix Team for educational and practical accuracy. For framework-specific breaking changes, verify against the official documentation of the relevant project. Last updated: August 15, 2026. Disclaimer
Tags:#Gemma#Open Source AI#Python#Ollama#Local LLM#Hugging Face
Vyuhantrix Team

Published by

Vyuhantrix Team

Open Source AI & ML · Vyuhantrix

Vyuhantrix is an open technology learning platform based in Ahmedabad, India, publishing step-by-step programming tutorials, system design breakdowns, and free developer tools.