Zum Hauptinhalt springen
Zurück zur Kategorie

Ollama Installation: Lokale LLM-Ausführungsanleitung

Ollama-Installation, lokale LLM-Modelle ausführen (Llama 3, Mistral, Gemma), API-Nutzung, Modellverwaltung, GPU-Beschleunigung und Docker-Deployment.

Lesezeit: 15 Min. KI & ML Self-Hosting
ollamallmkünstliche intelligenzself-hostingdockergpullama

Inhaltsverzeichnis

Ollama Installation: Lokale LLM-Ausführungsanleitung

Ollama ist ein Open-Source-Tool, mit dem Sie große Sprachmodelle (LLMs) einfach auf Ihrem lokalen Server ausführen können. Sie können Llama 3, Mistral, Gemma, Phi-3 und viele weitere Modelle mit einem einzigen Befehl herunterladen und verwenden. Diese Anleitung behandelt die Ollama-Installation, Modellverwaltung, API-Nutzung und GPU-Beschleunigung im Detail.

Was ist Ollama?

Ollama ist eine leichtgewichtige Laufzeitumgebung zum lokalen Ausführen von LLM-Modellen. Es ermöglicht das Herunterladen und Verwalten von Modellen mit einem Docker-ähnlichen Ansatz. Hauptmerkmale:

  • Einfache Installation: Einrichtung und Modell-Download mit einem Befehl
  • Breite Modellunterstützung: Llama 3, Mistral, Gemma, Phi-3, CodeLlama, Qwen und mehr
  • REST-API: OpenAI-kompatibler API-Endpunkt
  • GPU-Beschleunigung: NVIDIA- und AMD-GPU-Unterstützung
  • Modelfile: Benutzerdefinierte Modellkonfigurationen erstellen
  • Geringer Ressourcenverbrauch: Niedriger Speicherverbrauch mit quantisierten Modellen

Systemanforderungen

KomponenteMinimumEmpfohlen
RAM8 GB16 GB+
Festplatte20 GB50 GB+
GPU (optional)NVIDIA 4 GB VRAMNVIDIA 8 GB+ VRAM
BetriebssystemLinux, macOS, WindowsUbuntu 22.04 LTS

Installation unter Linux

Verwenden Sie das offizielle Installationsskript:

hljs bash
curl -fsSL https://ollama.com/install.sh | sh

Nach der Installation startet der Ollama-Dienst automatisch. Überprüfen Sie den Status:

hljs bash
systemctl status ollama

Manuelle Installation

Falls das Skript nicht funktioniert:

hljs bash
# Binary herunterladen
curl -L https://ollama.com/download/ollama-linux-amd64 -o /usr/local/bin/ollama
chmod +x /usr/local/bin/ollama

# Ollama-Benutzer erstellen
useradd -r -s /bin/false -m -d /usr/share/ollama ollama

# Systemd-Servicedatei
cat > /etc/systemd/system/ollama.service << 'EOF'
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="HOME=/usr/share/ollama"

[Install]
WantedBy=default.target
EOF

systemctl daemon-reload
systemctl enable --now ollama

Docker-Installation

CPU-Modus

hljs bash
docker run -d \
  --name ollama \
  -p 11434:11434 \
  -v ollama_data:/root/.ollama \
  ollama/ollama

GPU-Modus (NVIDIA)

hljs bash
docker run -d \
  --name ollama \
  --gpus all \
  -p 11434:11434 \
  -v ollama_data:/root/.ollama \
  ollama/ollama

Docker Compose

hljs yaml
version: '3.8'
services:
  ollama:
    image: ollama/ollama
    container_name: ollama
    ports:
      - "11434:11434"
    volumes:
      - ollama_data:/root/.ollama
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: all
              capabilities: [gpu]
    restart: unless-stopped

volumes:
  ollama_data:

Modelle herunterladen und ausführen

hljs bash
# Llama 3.1 8B Modell
ollama pull llama3.1

# Mistral 7B
ollama pull mistral

# Google Gemma 2 9B
ollama pull gemma2

# Interaktiver Chat
ollama run llama3.1

# Einzelne Abfrage
ollama run llama3.1 "Wie überprüfe ich die Festplattennutzung unter Linux?"

Modellverwaltung

hljs bash
# Installierte Modelle auflisten
ollama list

# Modellinformationen
ollama show llama3.1

# Modell löschen
ollama rm mistral

# Laufende Modelle anzeigen
ollama ps

REST-API-Nutzung

Ollama bietet eine REST-API auf Port 11434:

Chat-Vervollständigung

hljs bash
curl http://localhost:11434/api/chat -d '{
  "model": "llama3.1",
  "messages": [
    {"role": "system", "content": "Du bist ein hilfreicher Assistent."},
    {"role": "user", "content": "Was ist Docker?"}
  ],
  "stream": false
}'

Textgenerierung

hljs bash
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.1",
  "prompt": "Schreibe eine Nginx Reverse-Proxy-Konfiguration",
  "stream": false
}'

Python-API-Nutzung

hljs python
import requests

def chat_with_ollama(prompt, model="llama3.1"):
    response = requests.post(
        "http://localhost:11434/api/chat",
        json={
            "model": model,
            "messages": [{"role": "user", "content": prompt}],
            "stream": False
        }
    )
    return response.json()["message"]["content"]

result = chat_with_ollama("Wie sortiere ich eine Liste in Python?")
print(result)

Benutzerdefinierte Modelle mit Modelfile

hljs dockerfile
# Modelfile
FROM llama3.1

SYSTEM """Du bist ein REXE Technology technischer Support-Assistent.
Du hilfst bei Serververwaltung, Docker, Linux und Netzwerkthemen."""

PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER num_ctx 4096

Modell erstellen:

hljs bash
ollama create rexe-assistant -f Modelfile
ollama run rexe-assistant

GPU-Beschleunigung

NVIDIA GPU

hljs bash
# NVIDIA-Treiber prüfen
nvidia-smi

# CUDA-Version
nvcc --version

AMD GPU (ROCm)

hljs bash
curl -fsSL https://ollama.com/install.sh | OLLAMA_ACCELERATION=rocm sh

# Docker mit AMD GPU
docker run -d \
  --name ollama \
  --device /dev/kfd \
  --device /dev/dri \
  -p 11434:11434 \
  -v ollama_data:/root/.ollama \
  ollama/ollama:rocm

GPU-Speicherverwaltung

hljs bash
export OLLAMA_NUM_GPU=1
export OLLAMA_GPU_MEMORY=6144
export OLLAMA_MAX_LOADED_MODELS=2
export CUDA_VISIBLE_DEVICES=0

Umgebungsvariablen

hljs bash
# /etc/systemd/system/ollama.service.d/override.conf
[Service]
Environment="OLLAMA_HOST=0.0.0.0:11434"
Environment="OLLAMA_MODELS=/data/ollama"
Environment="OLLAMA_NUM_PARALLEL=4"
Environment="OLLAMA_MAX_LOADED_MODELS=2"
Environment="OLLAMA_KEEP_ALIVE=5m"

Änderungen anwenden:

hljs bash
systemctl daemon-reload
systemctl restart ollama

Leistungstipps

hljs bash
# RAM sparen mit quantisierten Modellen
ollama pull llama3.1:8b-q4_0    # 4-Bit (~4,7 GB)
ollama pull llama3.1:8b-q5_1    # 5-Bit (~5,7 GB)
ollama pull llama3.1:8b-q8_0    # 8-Bit (~8,5 GB)

# Kontextfenster-Einstellung
ollama run llama3.1 --num-ctx 2048

Mit REXE GPU-Servern können Sie große Sprachmodelle mit hoher Leistung über Ollama ausführen. NVIDIA A100/H100 GPUs liefern schnelle Antworten selbst bei 70B-Parameter-Modellen.