Run Llama Offline on Windows PC Without Internet After Setup
In this article
Yes, you can run Llama locally on a Windows PC without internet after the initial setup. The process involves installing a few developer tools, downloading the model once, and running it via llama-cpp-python or a lightweight server that communicates over http://localhost. Once the model is on disk, every inference is performed entirely on the machine, so no network connection is required.
What You’ll Need
- Windows 10 or 11 (64‑bit)
- At least 8 GB of RAM (16 GB recommended for larger models)
- 20 GB of free disk space for the model file
- Python 3.10 or newer (download from python.org)
- Git for Windows (optional but handy for cloning repositories)
- Visual Studio Build Tools with the C++ build tools workload (required to compile the C++ extension)
- CMake (v3.22 or newer)
- GPU drivers and CUDA 11.8+ if you want GPU acceleration (otherwise CPU only will work)
- Internet access for the initial download of the model and the Python packages
Step‑by‑Step
- Install Python. Run the installer, check "Add Python to PATH", and choose "Customize installation". Ensure the "pip" component is selected. After installation, open a command prompt and type
python --versionto confirm the version. - Install Visual Studio Build Tools. Download the installer from the Microsoft website, launch it, and select the "C++ build tools" workload. Include the "Windows 10 SDK" and the "MSVC v143" compiler. Click "Install" and wait for the components to finish.
- Install CMake. Download the binary distribution from the CMake site, run the installer, and add CMake to the system PATH during installation.
- Set up a virtual environment (optional but recommended). In a command prompt, navigate to a folder where you want the project, then run:
python -m venv llama-env llama-env\Scripts\activate - Install the llama‑cpp‑python package. With the virtual environment activated, run:
This pulls the pre‑compiled C++ extension and the Python wrapper.pip install --upgrade pip pip install llama-cpp-python - Download a quantized Llama model. Go to the Hugging Face model hub and locate a GGUF file, such as
llama-2-7b-chat.Q4_K_M.gguf. Click the download button and save the file to a folder likeC:\models\llama-2-7b-chat.Q4_K_M.gguf. The download size is roughly 4 GB. - Test the model with a quick prompt. In the command prompt, run:
If you see a generated response, the installation is successful.python - <<'PY' from llama_cpp import Llama model = Llama(model_path=r'C:\models\llama-2-7b-chat.Q4_K_M.gguf', n_gpu_layers=0) print(model('Hello, who are you?')) PY - Optional: Enable GPU acceleration. If you have a compatible NVIDIA GPU, install CUDA 11.8+ and cuDNN. Then reinstall
llama-cpp-pythonwith GPU support:
When creating the model object, setpip uninstall llama-cpp-python pip install llama-cpp-python[cuda]n_gpu_layersto a value greater than zero, e.g.,n_gpu_layers=30. - Set up a local inference server. Create a file named
server.pywith the following content:
Run it with:from fastapi import FastAPI from pydantic import BaseModel from llama_cpp import Llama app = FastAPI() model = Llama(model_path=r'C:\models\llama-2-7b-chat.Q4_K_M.gguf', n_gpu_layers=0) class Prompt(BaseModel): text: str @app.post("/chat") async def chat(prompt: Prompt): response = model(prompt.text, max_tokens=200) return {"response": response["choices"][0]["text"]}
Now you can send POST requests touvicorn server:app --host 127.0.0.1 --port 8000http://127.0.0.1:8000/chatfrom any local tool. - Use the server from a local client. For example, in PowerShell:
You’ll receive the model’s answer in JSON.Invoke-RestMethod -Method Post -Uri http://127.0.0.1:8000/chat -Body '{"text":"What is the capital of France?"}' -ContentType application/json - Optional: Create a desktop shortcut. Right‑click the
server.pyfile, choose "Send to > Desktop (create shortcut)", then edit the shortcut target to:
Double‑clicking the shortcut will start the server.cmd /k "C:\Users\YourName\llama-env\Scripts\activate && python C:\path\to\server.py" - Verify offline operation. Disconnect the network cable or disable Wi‑Fi. Open a new command prompt and run the server again. The model should load from disk and respond to requests without any error.
Common Mistakes
- Installing Python without adding it to PATH, which causes
pythoncommands to fail. - Using an older Visual Studio build tool set that lacks the MSVC compiler needed for the C++ extension.
- Downloading a 32‑bit Python or model on a 64‑bit system; the model will not load.
- Forgetting to activate the virtual environment before installing packages, leading to version conflicts.
- Running the server with
n_gpu_layers=0on a machine that has no GPU, which still works but is slow; set a higher value only if a GPU is present. - Using the wrong path separator (forward slash vs backslash) in Windows, which can cause the model loader to fail.
Tips
- Keep the model file in a folder that does not contain spaces; Windows sometimes misinterprets paths with spaces when passed to C++ binaries.
- Use the
--no-cache-dirflag withpip installif you have limited disk space; it prevents caching of wheel files. - When running CPU‑only inference, consider using the
--n_threadsparameter to match the number of logical processors for better performance. - For larger models (13 b or 70 b), use a 16‑bit quantized GGUF file and allocate at least 32 GB of RAM.
- Store the model in a location that is backed up; the file is large and can be corrupted if the drive fails.
- Set environment variables such as
CUDA_VISIBLE_DEVICESto control which GPU the server uses.
Troubleshooting
- Model fails to load. Verify the file path is correct and that the file extension is
.gguf. Ensure the file is not corrupted by checking its SHA‑256 hash against the one on Hugging Face. - Python raises "DLL load failed". This usually indicates a missing Visual C++ runtime. Re‑run the Visual Studio Build Tools installer and ensure the Windows 10 SDK is installed.
- Server crashes with "MemoryError". Increase
max_context_lengthor use a smaller model. On CPU, allocate more threads or reducemax_tokensper request. - GPU not recognized. Confirm that CUDA 11.8+ and cuDNN are installed and that the
PATHincludes the CUDA binaries. Reinstallllama-cpp-python[cuda]after installing CUDA. - FastAPI returns 500 error. Check the server logs; missing dependencies like
uvicornorfastapiwill cause failures. Install them withpip install fastapi uvicorn. - Requests time out after a few minutes. The default timeout in the client may be too low. Increase it or use streaming responses with
stream=TrueinrequestsorInvoke‑RestMethod.
Frequently Asked Questions
Can I use Llama on a laptop with limited RAM?
Yes, but you’ll need a very small model (e.g., 7 b quantized to 4 b) and you may have to reduce the context window to fit within the available memory.
Do I need a GPU to run Llama offline?
No. The CPU implementation works fine for small models, though inference will be slower. GPU acceleration is optional and only available on NVIDIA cards with CUDA 11.8+.
Will the model stay offline if I later connect to the internet?
Yes. Once the model file is on disk and the Python packages are installed, no network connection is required for inference.
How do I update the model to a newer version?
Download the new GGUF file from Hugging Face and replace the old file. Restart the server so it loads the updated model.
Is it legal to run Llama offline?
Yes, as long as you comply with the model’s license. Most Llama models are released under the Llama 2 license, which allows personal and commercial use with attribution.