Skip to content

Latest commit

 

History

History
395 lines (277 loc) · 14.4 KB

OfflineDeployment.md

File metadata and controls

395 lines (277 loc) · 14.4 KB

LightRAG Offline Deployment Guide

This guide provides comprehensive instructions for deploying LightRAG in offline environments where internet access is limited or unavailable.

If you deploy LightRAG using Docker, there is no need to refer to this document, as the LightRAG Docker image is pre-configured for offline operation.

Software packages requiring transformers, torch, or cuda will not be included in the offline dependency group. Consequently, document extraction tools such as Docling, as well as local LLM models like Hugging Face and LMDeploy, are outside the scope of offline installation support. These high-compute-resource-demanding services should not be integrated into LightRAG. Docling will be decoupled and deployed as a standalone service.

Table of Contents

Overview

LightRAG uses dynamic package installation (pipmaster) for optional features based on file types and configurations. In offline environments, these dynamic installations will fail. This guide shows you how to pre-install all necessary dependencies and cache files.

What Gets Dynamically Installed?

LightRAG dynamically installs packages for:

  • Storage Backends: redis, neo4j, pymilvus, pymongo, asyncpg, qdrant-client
  • LLM Providers: openai, anthropic, ollama, zai-sdk, aioboto3, voyageai, llama-index, lmdeploy, transformers, torch
  • Tiktoken Models: BPE encoding models downloaded from OpenAI CDN

Note: Document processing dependencies (pypdf, python-docx, python-pptx, openpyxl) are now pre-installed with the api extras group and no longer require dynamic installation.

Quick Start

Option 1: Using pip with Offline Extras

# Online environment: Install all offline dependencies
pip install lightrag-hku[offline]

# Download to ~/.tiktoken_cache (no environment variable needed)
lightrag-download-cache

# Create offline package
pip download lightrag-hku[offline] -d ./offline-packages
tar -czf lightrag-offline.tar.gz ./offline-packages -C ~ .tiktoken_cache

# Transfer to offline server
scp lightrag-offline.tar.gz user@offline-server:/path/to/

# Offline environment: Install (the tiktoken cache lands in ./.tiktoken_cache)
tar -xzf lightrag-offline.tar.gz
pip install --no-index --find-links=./offline-packages lightrag-hku[offline]
mkdir -p ~/.tiktoken_cache && cp ./.tiktoken_cache/* ~/.tiktoken_cache/

Option 2: Using Requirements Files

# Online environment: Download packages
pip download -r requirements-offline.txt -d ./packages

# Transfer to offline server
tar -czf packages.tar.gz ./packages
scp packages.tar.gz user@offline-server:/path/to/

# Offline environment: Install
tar -xzf packages.tar.gz
pip install --no-index --find-links=./packages -r requirements-offline.txt

Layered Dependencies

LightRAG provides flexible dependency groups for different use cases:

Available Dependency Groups

Group Description Use Case
api API server + document processing FastAPI server with PDF, DOCX, PPTX, XLSX support
offline-storage Storage backends Redis, Neo4j, MongoDB, PostgreSQL, etc.
offline-llm LLM providers OpenAI, Anthropic, Ollama, etc.
offline Complete offline package API + Storage + LLM (all features)

Note: Document processing (PDF, DOCX, PPTX, XLSX) is included in the api extras group. The previous offline-docs group has been merged into api for better integration.

Software packages requiring transformers, torch, or cuda will not be included in the offline dependency group.

Installation Examples

# Install API with document processing
pip install lightrag-hku[api]

# Install API and storage backends
pip install lightrag-hku[api,offline-storage]

# Install all offline dependencies (recommended for offline deployment)
pip install lightrag-hku[offline]

Using Individual Requirements Files

# Storage backends only
pip install -r requirements-offline-storage.txt

# LLM providers only
pip install -r requirements-offline-llm.txt

# All offline dependencies
pip install -r requirements-offline.txt

Tiktoken Cache Management

Tiktoken downloads BPE encoding models on first use. In offline environments, you must pre-download these models.

Where the Cache Lives

For a standard LightRAG installation, run lightrag-download-cache with no arguments. It creates ~/.tiktoken_cache by default, and LightRAG uses that directory automatically. Leave TIKTOKEN_CACHE_DIR unset when using this default. For an offline install, copy the cache into ~/.tiktoken_cache under the account that runs LightRAG.

The cache directory is resolved in this order:

  1. TIKTOKEN_CACHE_DIR (or tiktoken's legacy DATA_GYM_CACHE_DIR) when set. An empty TIKTOKEN_CACHE_DIR disables caching.
  2. ~/.tiktoken_cache, if the directory exists.
  3. Otherwise tiktoken's own fallback, <system temp dir>/data-gym-cache (for example /tmp/data-gym-cache).

Do not keep an offline cache under the system temp directory: /tmp is often a tmpfs or periodically cleaned (systemd-tmpfiles), so the cache disappears on reboot or after a few idle days, and it is writable by every local user. tiktoken verifies each file against a pinned SHA-256, so a tampered file is rejected rather than used — but on an offline host that rejection is an outage.

LightRAG never creates ~/.tiktoken_cache itself at runtime: with a cache directory configured, tiktoken treats a failed cache write as an error, so a missing or read-only home must not be adopted implicitly. Set TIKTOKEN_CACHE_DIR explicitly when the service account's home is not the right place (the Docker image uses /app/data/tiktoken).

An adopted ~/.tiktoken_cache may be read-only: encodings already in it are read from it. If an encoding is missing and cannot be cached there, LightRAG logs a warning and falls back to tiktoken's own default location for the rest of the process (which needs network access to download). A directory you set through TIKTOKEN_CACHE_DIR gets no such fallback: a failed cache write there is an error, as in tiktoken itself.

Using the CLI Command

After installing LightRAG, populate the default cache:

lightrag-download-cache

To prefetch only the encodings for specific models, use --models:

lightrag-download-cache --models gpt-4o-mini gpt-4

Default Models Downloaded

  • gpt-4o-mini (LightRAG default)
  • gpt-4o
  • gpt-4
  • gpt-3.5-turbo
  • text-embedding-ada-002
  • text-embedding-3-small
  • text-embedding-3-large

Setting Cache Location in Offline Environment

# Copy to the default location (no environment variable needed
# for LightRAG; run as the user the service runs as)
mkdir -p ~/.tiktoken_cache
cp /path/to/tiktoken_cache/* ~/.tiktoken_cache/

Using a Custom Cache Directory

Download to the custom path, then set TIKTOKEN_CACHE_DIR for the process that runs LightRAG. To keep the setting across server restarts, put it in the server's .env.

lightrag-download-cache --cache-dir /path/to/tiktoken_cache
export TIKTOKEN_CACHE_DIR=/path/to/tiktoken_cache

spaCy Models for Native docx smart_heading (Optional)

The native docx parser's opt-in smart_heading engine parameter uses spaCy for sentence/NER heuristics. Deployments that never enable smart_heading do NOT need any of this — the dependency is loaded lazily only when a document is parsed with smart_heading=true, and a missing model raises a hard error at that point (never silently degrades). Deployments that enable it globally (DOCX_SMART_HEADING=true) or via a LIGHTRAG_PARSER rule with native(smart_heading=true) get the check earlier: the server verifies the models at startup and fails fast with install guidance.

Docker deployments need none of this section: the main LightRAG image bundles the spaCy runtime and both pinned models (the lite image ships the runtime only, so enabling smart_heading there still requires installing the models). The steps below apply to non-Docker pip deployments.

The two language models are pinned to an exact version (zh_core_web_sm / en_core_web_sm 3.8.0) because smart_heading promises deterministic re-parse results; a model version drift would silently change its decisions. The zh model's tokenizer backend spacy-pkuseg is pinned exactly for the same reason. Unlike the models it is a PyPI package, so the api extra declares it and pip install lightrag-hku[api] / uv sync --extra api install it; it is also shipped with the model wheels so the offline model install can resolve it.

Prepare in Online Environment

# spaCy runtime (already included in the api extra)
pip install lightrag-hku[api]
# or: pip install -r requirements-offline-smart-heading.txt (runtime + models)

# For offline transfer: download runtime + model wheels to ./packages
pip download -r requirements-offline-smart-heading.txt -d ./packages

# Or download only the pinned model wheels (to ./spacy_models by default) —
# --spacy is independent of the tiktoken cache download, so this never
# touches tiktoken
lightrag-download-cache --spacy-dir ./spacy_models

# Or install them straight into the current environment (uses pip, or
# `uv pip install` in a uv-created environment that has no pip)
lightrag-download-cache --spacy-install

The models are not in uv.lock: in a uv sync environment, a later plain uv sync removes them. Use uv sync --inexact to keep them.

Install in Offline Environment

# Install by name from the transferred wheel directory. Do NOT use
# `-r requirements-offline-smart-heading.txt` here: its model pins are direct
# GitHub URLs, which pip fetches from the network even under --no-index.
pip install --no-index --find-links=./packages spacy zh_core_web_sm en_core_web_sm
# or, with only the model wheels from lightrag-download-cache --spacy:
pip install --no-index --find-links=./spacy_models zh_core_web_sm en_core_web_sm

Complete Offline Deployment Workflow

Step 1: Prepare in Online Environment

# 1. Install LightRAG with offline dependencies
pip install lightrag-hku[offline]

# 2. Download tiktoken cache
lightrag-download-cache --cache-dir ./offline_cache/tiktoken

# 3. Download all Python packages
pip download lightrag-hku[offline] -d ./offline_cache/packages

# 4. Create archive for transfer
tar -czf lightrag-offline-complete.tar.gz ./offline_cache

# 5. Verify contents
tar -tzf lightrag-offline-complete.tar.gz | head -20

Step 2: Transfer to Offline Environment

# Using scp
scp lightrag-offline-complete.tar.gz user@offline-server:/tmp/

# Or using USB/physical media
# Copy lightrag-offline-complete.tar.gz to USB drive

Step 3: Install in Offline Environment

# 1. Extract archive
cd /tmp
tar -xzf lightrag-offline-complete.tar.gz

# 2. Install Python packages
pip install --no-index \
    --find-links=/tmp/offline_cache/packages \
    lightrag-hku[offline]

# 3. Set up tiktoken cache in the default location (LightRAG finds it
#    without TIKTOKEN_CACHE_DIR; set the variable to use another path)
mkdir -p ~/.tiktoken_cache
cp /tmp/offline_cache/tiktoken/* ~/.tiktoken_cache/

# 4. (Optional) Remove the extracted archive; the cache no longer lives in /tmp
rm -rf /tmp/offline_cache /tmp/lightrag-offline-complete.tar.gz

Step 4: Verify Installation

# Test Python import
python -c "from lightrag import LightRAG; print('✓ LightRAG imported')"

# Test tiktoken
python -c "from lightrag.utils import TiktokenTokenizer; t = TiktokenTokenizer(); print('✓ Tiktoken working')"

# Test optional dependencies (if installed)
python -c "import redis; print('✓ Redis available')"

Troubleshooting

Issue: Tiktoken fails with network error

Problem: Unable to load tokenizer for model gpt-4o-mini

Solution:

# Check which directory is in use: $TIKTOKEN_CACHE_DIR if set,
# else ~/.tiktoken_cache if it exists (for the service's user)
echo $TIKTOKEN_CACHE_DIR

# Verify cache files exist
ls -la ${TIKTOKEN_CACHE_DIR:-~/.tiktoken_cache}/

# If empty, you need to download cache in online environment first

Issue: Dynamic package installation fails

Problem: Error installing package xxx

Solution:

# Pre-install the specific package you need
# For API with document processing:
pip install lightrag-hku[api]

# For storage backends:
pip install lightrag-hku[offline-storage]

# For LLM providers:
pip install lightrag-hku[offline-llm]

Issue: Missing dependencies at runtime

Problem: ModuleNotFoundError: No module named 'xxx'

Solution:

# Check what you have installed
pip list | grep -i xxx

# Install missing component
pip install lightrag-hku[offline]  # Install all offline deps

Issue: Permission denied on tiktoken cache

Problem: PermissionError: [Errno 13] Permission denied

Solution:

# Ensure cache directory has correct permissions
chmod 755 ~/.tiktoken_cache
chmod 644 ~/.tiktoken_cache/*

# Or use a user-writable directory
export TIKTOKEN_CACHE_DIR=~/my_tiktoken_cache
mkdir -p ~/my_tiktoken_cache

Best Practices

  1. Test in Online Environment First: Always test your complete setup in an online environment before going offline.

  2. Keep Cache Updated: Periodically update your offline cache when new models are released.

  3. Document Your Setup: Keep notes on which optional dependencies you actually need.

  4. Version Pinning: Consider pinning specific versions in production:

    pip freeze > requirements-production.txt
  5. Minimal Installation: Only install what you need:

    # If you only need API with document processing
    pip install lightrag-hku[api]
    # Then manually add specific LLM: pip install openai

Additional Resources

Support

If you encounter issues not covered in this guide:

  1. Check the GitHub Issues
  2. Review the project documentation
  3. Create a new issue with your offline deployment details