Running Open-Source LLMs Locally with Ollama: From Selection to Verification
This guide explores the benefits and trade-offs of deploying open-source large language models locally. It covers how to evaluate hardware requirements, install Ollama, pull models like Qwen2.5, verify local API services using Python, and optimize performance through troubleshooting common issues such as insufficient VRAM and slow generation speeds.
1. Why Deploy Large Language Models Locally?
Compared to calling cloud APIs directly, deploying open-source LLMs locally offers three practical benefits. First, data stays on your machine, which is ideal for scenarios with high privacy and compliance requirements. Second, inference does not rely on the network and has no API call limits, facilitating high-frequency interaction and offline usage. Third, it allows for secondary development, prompt experiments, or fine-tuning based on your own business needs.
Local deployment also comes with trade-offs. You need to invest in certain hardware, and inference speed is highly dependent on the GPU, RAM, and disk. Therefore, evaluating your hardware beforehand can prevent downloading a model only to find it cannot run smoothly.
Nowadays, many open-source models offer small parameter versions around 3B. Combined with mature local inference tools, a standard computer with a dedicated GPU can run them relatively smoothly. This significantly lowers the barrier to entry.
2. Hardware Evaluation First
2.1 Estimation Benchmarks
The table below provides rough estimates based on Q4_K_M quantization and default context length, serving as a reference for model selection. The model file size is based on the labels on the model library page. A longer context or higher precision quantization versions like Q5 or Q8 will require more VRAM and RAM accordingly.
| Model Size | Quantization | Approx. File Size | Recommended VRAM (GPU Inference) | Recommended System RAM | Recommended Disk Space |
|---|---|---|---|---|---|
| 3B | Q4_K_M | ~ 2 GB | 6 GB or above | 16 GB | 10 GB |
| 7B | Q4_K_M | ~ 4.7 GB | 8 GB or above | 32 GB | 15 GB |
| 14B | Q4_K_M | ~ 9 GB | 12 GB or above | 32 GB | 20 GB |
Without a dedicated GPU, models can run entirely on the CPU and RAM, but generation speed will be noticeably slower. A 3B model is suitable as an entry-level choice, while models 7B and above are better suited for machines with larger VRAM.
2.2 Performance Measurement Method
Speeds vary greatly across different hardware, so fixed numbers are not provided here. You can measure the actual performance of your own machine using the following method:
ollama run qwen2.5:3b --verbose
After the command finishes, the terminal will output the eval rate, measured in tokens per second. This represents the speed during the generation phase.
Additionally, run the following command after running for a while to check where the model is actually running:
ollama ps
If the PROCESSOR column shows 100% GPU, the model is running entirely on the graphics card. If it shows a ratio like 48%/52% CPU/GPU, it means the VRAM is insufficient, and part of the computation falls on the CPU, which will decrease speed.
3. Installing the Runtime
3.1 Installing Ollama
Windows users can install it using the built-in package manager:
winget install Ollama.Ollama
macOS and Linux users can execute the official installation script:
curl -fsSL https://ollama.com/install.sh | sh
Before executing a remote script, it is recommended to open the URL in a browser to review its content. On Linux, the script registers a system service, which usually requires sudo privileges. macOS users can also directly download the installation package from the official website.
3.2 Verifying the Installation
Open a terminal and execute the following command:
ollama --version
If a version number is output, the installation is successful. If it prompts that the command is not found, reopen the terminal or check if the installation path has been added to the system environment variables.
3.3 Modifying the Model Storage Path (Optional)
By default, Ollama saves models in the user directory. If space on your system drive is tight, you can specify another directory via the OLLAMA_MODELS environment variable. This variable only applies to Ollama.
Windows users execute in PowerShell:
setx OLLAMA_MODELS "D:\models"
Then exit Ollama from the system tray and restart it for the settings to take effect.
For Linux users managing services with systemd, execute the following command to add the environment variable:
sudo systemctl edit ollama
Write the following content in the opened editor and save:
[Service]Environment="OLLAMA_MODELS=/data/models"
Next, ensure that the directory belongs to the ollama user running the service, then restart the service:
sudo mkdir -p /data/modelssudo chown -R ollama:ollama /data/modelssudo systemctl daemon-reloadsudo systemctl restart ollama
4. Pulling and Verifying the Model
4.1 Pulling the Model
This article uses qwen2.5:3b as an example. The following command only downloads the model and does not start a conversation:
ollama pull qwen2.5:3b
The model file is about 2 GB, and download time depends on network speed. After the download is complete, execute the following command to confirm the model is local:
ollama list
4.2 Performing an Interactive Test
ollama run qwen2.5:3b --verbose
After entering the conversation interface, input a question to see the answer and speed statistics. Enter /bye to exit.
4.3 Verifying the Local Service Interface
Ollama starts an HTTP service locally, with the default port 11434, and only listens to 127.0.0.1. First, execute the following command to check if the service is normal:
curl http://localhost:11434/api/tags
If the returned JSON contains the list of downloaded models, the service is running normally.
Next, make a minimal call using Python. In the example, stream is set to False, which is only used to verify connectivity. In practical applications, if you need character-by-character output, you should use a streaming request.
import requestsBASE_URL = "http://localhost:11434"payload = {"model": "qwen2.5:3b","prompt": "Briefly describe the advantages of deploying LLMs locally in one sentence.","stream": False,}try:resp = requests.post(f"{BASE_URL}/api/generate", json=payload, timeout=120)resp.raise_for_status()print(resp.json()["response"])except requests.HTTPError as e:print("HTTP Error:", e.response.status_code, e.response.text)except requests.RequestException as e:print("Request failed, please ensure the Ollama service is running:", e)
If a 404 is returned, it is usually because the requested model name has not been pulled. First, check the name with ollama list; the name must exactly match the model field. On the first request, the model needs to be loaded into memory, which takes longer, so the timeout in the example is set to 120 seconds.
5. Common Troubleshooting and Optimization
Slow Loading: Confirm that the model directory is on an SSD. On the first request, weights need to be read from disk into VRAM or RAM, and this delay is normal. Once the model is loaded, subsequent requests will be significantly faster.
Insufficient VRAM: First run
ollama psto check thePROCESSORcolumn. If CPU usage appears, switch to a smaller model, such as the 3B version, or reduce the context length. In API requests, the context length can be set viaoptions:
payload = {"model": "qwen2.5:3b","prompt": "Hello","stream": False,"options": {"num_ctx": 2048},}
Slow Generation Speed: First use
ollama psto confirm whether the model is running entirely on the GPU. Then check the service logs. Log locations vary by system: on Linux runjournalctl -u ollama --no-pager | tail -n 100; on Windows logs are at%LOCALAPPDATA%\Ollama\server.log; on macOS they are at~/.ollama/logs/server.log. If CPU inference is confirmed, you can setnum_threadinoptions, generally taking a value around the number of physical cores.Port Conflict: If port 11434 is already occupied by another program, you can set the
OLLAMA_HOSTenvironment variable to change to a different port, such as127.0.0.1:11435. Windows users set it withsetxand restart Ollama. Linux users follow the method in Section 3 to add the variable in the systemd configuration and restart the service. After modifying the port, the client’sBASE_URLmust also be updated accordingly.Model Management: First run
ollama listto find the name of the model to delete, then runollama rmto delete it. Before deleting, useollama psto confirm that the model is not currently in use. Here is an example of deletingqwen2.5:3b:
ollama listollama rm qwen2.5:3b
After deletion, the model files will be removed and need to be re-downloaded for future use. The deletion command cannot be undone, so verify the name before executing.
6. Security Tips
By default, Ollama only listens on the local loopback address, making it inaccessible to other devices. If you set OLLAMA_HOST=0.0.0.0, the service will be exposed to the local network or even the public internet. Ollama itself does not provide authentication; anyone who can access that port can consume your computing power. Therefore, if you need to provide services externally, you should first add authentication via a reverse proxy and restrict access sources with a firewall.
Additionally, each model has its own license. Before commercial use, please review the licensing terms on the model library page to confirm if your usage is permitted.
7. Summary
The workflow for deploying open-source LLMs locally can be summarized as: evaluate hardware, install Ollama, pull the model, verify the service, and integrate into applications. For 3B-level models, a computer with 16GB of RAM and 6GB of VRAM is usually enough for a usable experience. If you want better output quality, you should invest appropriately in VRAM and RAM. Once you master this workflow, you can reuse the same steps for deployment when switching to other open-source models or inference tools.
