Skip to main content

👍Running Gemma with Ollama on Windows

🌍"Local server running… because world domination starts on localhost"


Let's run a Powerful Open Model Locally!

In this issue, we will cover:

    • Benefits of running Gemma Locally
    • Hardware requirement
    • Intro on Ollama and ways to run
    • List of some helpful commands

After going through a brief understanding of Gemma in the previous blog, it would be a good idea to do some hands-on. Gemma, Google's family of lightweight, state-of-the-art open models inspired by the same technology behind Gemini.

Ever wanted to experiment with a powerful language model without worrying about API keys, usage costs, or sending your data to the cloud? Let's go through the steps needed to use it! 

This post will guide you through setting up and running Google's Gemma model right on your own laptop or desktop. It's surprisingly accessible and opens up a world of possibilities.

Before we answer How, let's answer Why.

There are benefits of running the model locally:

  • Privacy: The data and prompts remain in the local server, which is crucial for sensitive information or just peace of mind.
  • Cost-Effective: Free to download, no API fees, and no per-token charges.
  • Offline Access: No internet is needed to use it.
  • Control & Customization: direct access that means it can be customized.
  • Learning: a great way to understand the model

Hardware Requirement:

  • RAM: For Gemma 2B, 8GB of RAM is often the minimum, but 16GB or more is strongly recommended for smoother performance and to run other applications alongside it. 
  • Disk Space: Gemma 2B is roughly 3-5GB, while Gemma 7B is significantly larger (around 10-15GB), depending on the specific version/quantization. 
  • CPU: Multi-Core would work
  • GPU (optional but recommended)
Don't be discouraged if you don't have a high-end gaming rig! Many standard laptops and desktops built in the last few years can handle Gemma 2B.

Good news!! You don't need to be an expert in ML to run Gemma!! There are pretty handy tools that make our lives easier. Two such tools are Ollama and LM Studio.

Ollama is highly recommended for beginners and is a great command-line tool that bundles models and their configurations. Whereas, LM Studio is a polished desktop application with a GUI.

Running Gemma 2B with Ollama

Firstly, install Ollama from the official website: https://ollama.com/
Click the "Download" button and choose the version for your operating system. 


And, that's it! Once the Ollama is downloaded, you can use it directly or you can integrate in your code through API's. Let's see how:

Run using Command Prompt

Open Command Prompt, PowerShell, or Windows Terminal and run the below command :




What it does when you run the command 'ollama run gemma:2b' is, it checks if Gemma model files (gguf format) are already downloaded. If not, it downloads them from Ollama's model hub. Then it loads the model into RAM. This can take a few seconds, depending on your RAM and CPU/GPU.

Integrate with Code

Behind the scenes, it runs a local server at http://localhost:11434. So if you open this link in any browser, it responds to 'Ollama is running'

This server handles 
  • Accepting HTTP API requests
  • Managing model inheritance
  • Managing model sessions/conversations
To stop the server completely, you can run the "ollama stop" command.

Process name(System Level)

If you open your Task Manager / Activity Monitor, you'll see something like:
  • ollama
  • A child process for model inference (can be threads, depending on the system)
You won't see "Gemma" directly; it's all inside the Ollama server.

Integrate to Python Web App

Below is an example of how to integrate in web app:


from flask import Flask, request, render_template
import requests

app = Flask(__name__)

def query_gemma(prompt):
    response = requests.post(
        'http://localhost:11434/api/generate',
        json={"model": "gemma", "prompt": prompt, "stream": False}
    )
    return response.json()['response']

@app.route('/', methods=['GET', 'POST'])
def index():
    answer = ""
    if request.method == 'POST':
        user_input = request.form['prompt']
        answer = query_gemma(user_input)
    return render_template('index.html', answer=answer)

if __name__ == '__main__':
    app.run(debug=True)
  

Bonus 💥

Command Description
ollama run gemma Start chatting with Gemma model
ollama pull gemma Download Gemma model locally
ollama list Show all models installed on your machine
ollama ps Show running model sessions (like a "process list")
ollama stop Stop the Ollama server and all sessions
ollama rm gemma Remove (delete) a specific model
ollama create Create a custom model (fine-tune a base model!)
ollama server Start Ollama server manually without running a model
ollama help Show the command help menu
ollama pull gemma:2b Pull a specific version (like 2B variant)
ollama run gemma --gpu Force model to run on GPU
ollama run gemma --system "You are a helpful assistant." Set a system prompt (personality setup)
ollama run gemma --temperature 0.2 Control randomness (lower = more predictable)
ollama run gemma --num-predict 512 Limit the number of tokens to predict
ollama run gemma --top-p 0.9 --top-k 40 Control sampling diversity (fine-tuning generation style)
ollama serve --port 12345 Serve Ollama API on a custom port



Hope this gives an idea of how to get started with Gemma and integrate into the code.

Cheers.


Comments

Popular posts from this blog

AI Is Building the Future of Software. But What Happens to Software Engineers?

Hey Google... Wait, Who's Gemini? - The Takeover of Your Voice Assistant