Back to Blog
From the foundersGLMLocal AIDeveloper Guide

How to Setup GLM on Your PC

Run a powerful open-source language model fully offline — no API bills, no data leaks, no cloud dependency. Here's the complete setup from zero.

PPratik Khanapurkar· Co-founderAugust 202610 min read

Every developer building with LLMs eventually hits the same wall: API costs scale with usage, data privacy is someone else's promise, and latency is at the mercy of network hops you cannot control. GLM — the General Language Model series from Tsinghua University's KEG Lab — offers a credible local alternative. The GLM-4 family sits in the same capability tier as similarly sized open models, it runs on consumer hardware, and the setup is surprisingly clean once you understand the moving parts.

This guide walks you through the full local setup on a Windows or Linux PC. By the end you will have a working model server, a Python client, and an optional OpenAI-compatible endpoint you can drop straight into your existing tooling.

What is GLM?

GLM stands for General Language Model, a family of transformer-based language models developed by Tsinghua University. The key architectural difference from GPT-style models is the autoregressive blank infilling objective used in pretraining — instead of pure next-token prediction, GLM learns to fill masked spans of arbitrary length in any order. In practice, this gives the model strong performance on both generation and understanding tasks from a single checkpoint.

GLM-4 (the current public series) is available in two key sizes for local use: GLM-4-9B and the lighter GLM-4-9B-Chat (instruction-tuned). There's also GLM-4V for multimodal tasks. For most developer setups, GLM-4-9B-Chat is the right starting point.

Why GLM over Llama or Mistral?. GLM-4's bilingual (English + Chinese) pretraining corpus makes it particularly strong for multilingual products. If your users or data include non-English text, the accuracy gap versus English-only models is meaningful.

Hardware Requirements

ScenarioVRAMRAMStorageSpeed (tok/s)
GLM-4-9B FP16 (GPU)18 GB+16 GB20 GB~30–50
GLM-4-9B INT8 (GPU)10 GB16 GB12 GB~20–35
GLM-4-9B Q4 (CPU only)None24 GB8 GB~4–8
GLM-4-9B INT4 (GPU)6 GB12 GB8 GB~15–25

Before you start. For GPU inference, you need NVIDIA CUDA 11.8+ or 12.x. AMD cards can run via CPU offload through llama.cpp but performance degrades significantly. Make sure your CUDA toolkit and driver versions match before installing PyTorch.

Step-by-Step Setup

Installation Commands

# 1. Create virtual environment
python -m venv glm-env
source glm-env/bin/activate   # Windows: glm-env\Scripts\activate

# 2. Install PyTorch (CUDA 12.1 example — check pytorch.org for yours)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

# 3. Install Hugging Face stack
pip install transformers==4.43.0 accelerate bitsandbytes huggingface_hub

# 4. Download model weights
huggingface-cli download THUDM/glm-4-9b-chat --local-dir ./models/glm-4-9b-chat
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

MODEL_PATH = "./models/glm-4-9b-chat"

tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_PATH,
    trust_remote_code=True,
    torch_dtype=torch.float16,   # use torch.int8 for INT8 quantization
    device_map="auto"            # spreads across GPUs / falls back to CPU
)
model.eval()

history = []
response, history = model.chat(
    tokenizer,
    query="Explain transformer attention in two sentences.",
    history=history
)
print(response)

Running on Low-VRAM GPUs (INT4 Quantization)

If you're on a 6–8 GB GPU (RTX 3060, RTX 4060, etc.), INT4 quantization via bitsandbytes makes GLM-4-9B runnable with acceptable quality loss for most text tasks.

OpenAI-Compatible Local Server

Wrapping GLM in a FastAPI server lets you point any OpenAI SDK call at http://localhost:8000/v1 without changing your application code. The minimal pattern:

from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
import uvicorn, torch
from transformers import AutoTokenizer, AutoModelForCausalLM

app = FastAPI()
tokenizer = AutoTokenizer.from_pretrained("./models/glm-4-9b-chat", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    "./models/glm-4-9b-chat", trust_remote_code=True,
    torch_dtype=torch.float16, device_map="auto"
).eval()

class Message(BaseModel):
    role: str
    content: str

class ChatRequest(BaseModel):
    messages: List[Message]
    max_new_tokens: int = 512

@app.post("/v1/chat/completions")
async def chat(req: ChatRequest):
    history = [(m.content, "") for m in req.messages if m.role == "user"]
    query = req.messages[-1].content
    response, _ = model.chat(tokenizer, query=query, history=[])
    return {"choices": [{"message": {"role": "assistant", "content": response}}]}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

# Run: python server.py
# Use with: openai.base_url = "http://localhost:8000/v1"

Common Issues & Fixes

CUDA out of memory. Switch to INT4 quantization or reduce max_new_tokens. If using FP16, ensure no other GPU processes are running. On Linux, nvidia-smi shows current VRAM consumption per process.

trust_remote_code error. GLM uses custom attention kernels that require trust_remote_code=True in both the tokenizer and model load calls. This is expected — the code runs from the downloaded checkpoint, not from the transformers library itself.

Slow on CPU — what to expect. CPU-only inference on a modern 8-core machine produces roughly 4–8 tokens/second for the 9B model. For real-time chat, this is borderline; for batch processing or background jobs, it's acceptable. Consider llama.cpp GGUF format for faster CPU inference.

What to Build Next

Once GLM is running locally, the obvious next steps are: connect it to a vector database (Chroma, Qdrant, or Weaviate) to build a private RAG pipeline over your own documents; add a system prompt layer so your local server behaves like a domain-specific assistant; or use it as the backbone for a local agent loop with tool-use.

The OpenAI-compatible server pattern means you can drop GLM into LangChain, LlamaIndex, or Open WebUI immediately — no SDK changes needed. The entire stack — model + inference server + frontend — costs you nothing per query after the initial setup.

Building with local LLMs at DestinPQ

We help teams ship AI-powered products without cloud vendor lock-in. From local model deployment to full AI agent systems — talk to us.

From the DestinPQ founders — practical writing on AI, engineering, and building for real businesses.

All posts