Setting Up a Local LLM with Ollama and Python

Setting Up a Local LLM with Ollama and Python

Maya AhmedBy Maya Ahmed
How-ToHow-To & Fixespythonllmollamaailocal-ai
Difficulty: intermediate

The glow of a mechanical keyboard in a dark room is a familiar sight for any developer, but there's a new kind of weight to the hardware we use today. Running large language models (LLMs) used to mean renting expensive GPU clusters from cloud providers, but that's changing. This post walks through setting up a local LLM environment using Ollama and Python, giving you full control over your data and your compute. You'll learn how to pull models, run them on your own machine, and write a script to interact with them via an API.

What is Ollama?

Ollama is an open-source framework designed to run large language models locally on macOS, Linux, and Windows. It simplifies the process of managing model weights and provides a clean API for developers to interact with. Instead of wrestling with complex Python dependencies or CUDA configurations just to get a model running, Ollama handles the heavy lifting of model execution and hardware acceleration. It's a lightweight way to bring much-needed intelligence to your local development workflow.

The software works by packaging models like Llama 3 or Mistral into "blobs" that are easy to pull and run. It's remarkably efficient. If you've ever struggled with Docker container overhead or complex environment setups, you'll find the Ollama interface refreshingly straightforward.

How Do I Install Ollama on My Machine?

You can install Ollama by downloading the installer directly from the official Ollama website. The process varies slightly depending on your operating system, but it's generally a one-click affair.

  1. For macOS: Download the .zip file, unzip it, and move the Ollama application to your Applications folder.
  2. For Linux: Run the installation script via your terminal using curl -fsSL https://ollama.com/install.sh | sh.
  3. For Windows: Currently, the Windows version is available in a preview/installer format that works similarly to standard Windows software.

Once installed, open your terminal. Type ollama serve to ensure the background process is running. You'll know it's working if the command doesn't return an error immediately. It's a good idea to keep this running in the background while you develop your Python applications.

Pulling Your First Model

Before you can write code, you need a brain. You can't just call a function and expect an answer; you need to download a specific model weight file first. Use the ollama run command to download and execute a model. For instance, running ollama run llama3 will pull the latest Llama 3 weights from the registry and start a chat session right in your terminal.

If your machine doesn't have a massive amount of VRAM, don't worry. You can choose smaller models. A 7B parameter model is usually the sweet spot for modern laptops. If you're working on a massive server, you might want to try something larger, but for most of us, the 7B or 8B models are plenty fast.

How Do I Use Python to Talk to Ollama?

You interact with Ollama via a local HTTP API, which makes it incredibly easy to use with the requests library or the official Python library. The most common way to build a production-ready integration is to use the official Python package to handle the communication.

First, you'll need to install the library. Run this in your terminal:

pip install ollama

Here is a basic script to get you started. This script sends a prompt to the model and prints the response. It's a simple implementation, but it's the foundation for everything else you'll build.

import ollama

def chat_with_model(prompt):
    try:
        response = ollama.chat(model='llama3', messages=[
            {
                'role': 'user',
                'content': prompt,
            },
        ])
        return response['message']['content']
    except Exception as e:
        return f"An error occurred: {e}"

if __name__ == "__main__":
    user_input = "Explain the concept of a decorator in Python."
    print(f"User: {user_input}")
    print("AI is thinking...")
    
    result = chat_with_model(user_input)
    print(f"AI: {result}")

That's it. That simple script is all you need to start building AI-powered features. You aren't just calling an external API—you're talking to a process running on your own hardware. This is a huge win for privacy and latency.

Streaming Responses

The script above waits for the entire response to finish before printing anything. That can feel slow. If you want that "typing" effect where words appear one by one, you'll want to use the streaming feature. This is much better for user experience. It's a small detail, but it makes a world of difference.

import ollama

def stream_chat(prompt):
    stream = ollama.chat(
        model='llama3',
        messages=[{'role': 'user', 'content': prompt}],
        stream=True,
    )

    for chunk in stream:
        print(chunk['message']['content'], end='', flush=True)

stream_chat("Write a short poem about a coffee shop in Chicago.")

Notice the flush=True in the print statement. Without that, your terminal might buffer the text, and you'll see nothing for ten seconds and then a massive block of text all at once. You want that smooth, continuous flow.

Comparing Local LLMs vs. Cloud APIs

Deciding whether to run a model locally or use an API like OpenAI's depends entirely on your use case. Below is a breakdown of how they stack up against each other.

Feature Local LLM (Ollama) Cloud API (OpenAI/Anthropic)
Data Privacy High (Data never leaves your machine) Lower (Data is sent to a third party)
Cost Free (Uses your electricity/hardware) Pay-per-token
Setup Complexity Moderate (Requires local hardware) Low (Just an API key)
Internet Dependency None (Works offline) Required
Performance Dependent on your GPU/CPU Extremely fast/Scalable

If you're building a tool that handles sensitive user data—like a private document analyzer—running things locally is the only way to go. If you're building a massive-scale web app that needs to handle millions of requests, you'll likely need the scale of a cloud provider. It's not an either/or situation; many developers use both.

If you're already managing complex deployments, you might find yourself looking into how to containerize these environments. While Ollama runs great on a desktop, you'll eventually want to look into Mastering Docker Multi-Stage Builds if you intend to move these models into a production-ready containerized pipeline.

Optimizing for Performance

Running a model locally can be taxing on your resources. If your Python script feels sluggish, it's likely because the model is fighting for memory with your IDE or browser. To keep things running smoothly, keep an eye on your hardware usage.

One way to speed things up is to use a smaller quantization level. Models are often "quantized," which means the precision of the weights is reduced to save space and increase speed. A 4-bit quantized model will run significantly faster than an 8-bit or 16-bit model, though it might lose a tiny bit of nuance in its reasoning. For most development tasks, the trade-off is well worth it.

Check your RAM frequently. If you have 16GB of RAM and you're running a 13B parameter model, you're going to hit a wall. The OS will start swapping to the disk, and your "AI" will suddenly become a very slow typewriter. It's better to stick to models that fit comfortably within your available memory. If you find your local environment is getting too heavy, you might want to look into optimizing your dependencies or perhaps implementing a caching layer to avoid hitting the model for the same questions repeatedly.

One thing to keep in mind: don't expect a MacBook Air with no M-series chip to run a 70B model. It just won't happen. Be realistic about your hardware. A dedicated GPU is a massive advantage, but even a modern Mac with Unified Memory can handle 7B or 8B models with ease. Just watch your background processes.

Steps

  1. 1

    Install Ollama on your operating system

  2. 2

    Download and run a model like Llama 3 or Mistral

  3. 3

    Install the Ollama Python library via pip

  4. 4

    Write a script to interact with the local API endpoint