
A comprehensive guide to running any GGUF model from HuggingFace on your local machine using Ollama and the Local AI Translator
Table of Contents
- Understanding the Formats
- Prerequisites
- Method 1: Direct Run (Easiest – No Conversion!)
- Method 2: Download and Import with Modelfile
- Method 3: Converting PyTorch to GGUF (Advanced)
- Customization Options
- Troubleshooting
- Real-World Examples
Understanding the Formats
What is GGUF?
GGUF (GPT-Generated Unified Format) is a binary format optimized for:
- Fast loading on CPUs and GPUs
- Efficient inference with quantization
- Self-contained packaging (includes model weights + metadata)
Why GGUF for Ollama?
Ollama is built on llama.cpp (created by Georgi Gerganov, who also created GGUF), making GGUF the native and most efficient format for Ollama.
HuggingFace Format Ecosystem
- PyTorch (
.bin,.pt) – Original training format - SafeTensors (
.safetensors) – Safer alternative to PyTorch - GGUF (
.gguf) – Optimized for inference
Prerequisites
1. Install Ollama
# macOS/Linux
curl -fsSL https://ollama.com/install.sh | sh
# Windows
# Download from https://ollama.com/download
Verify installation:
ollama --version
2. Install Git LFS (for downloading large files)
# Ubuntu/Debian
sudo apt-get install git-lfs
# macOS
brew install git-lfs
# Initialize
git lfs install
3. Optional: Install HuggingFace CLI
pip install huggingface-hub
Method 1: Direct Run (Easiest – No Conversion!)
🎉 NEW FEATURE: As of October 2024, Ollama can run GGUF models directly from HuggingFace without any manual download or conversion!
Step 1: Find a GGUF Model on HuggingFace
Visit HuggingFace Models and:
- Use the “gguf” tag filter, OR
- Search for popular quantizers:
bartowski,MaziyarPanahi,TheBloke
Popular GGUF repositories:
bartowski/Llama-3.2-3B-Instruct-GGUFbartowski/Qwen2.5-7B-Instruct-GGUFMaziyarPanahi/Meta-Llama-3.1-8B-Instruct-GGUF
Step 2: Run Directly with Ollama
# Basic syntax
ollama run hf.co/{username}/{repository}
# Example: Run Llama 3.2 3B
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF
# Specify quantization level
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:IQ3_M
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q8_0
# Use full filename as tag
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Llama-3.2-3B-Instruct-Q4_K_M.gguf
Step 3: Chat with Your Model
After running the command above, you’ll enter an interactive chat:
>>> Hello! Tell me about yourself.
>>> /bye # to exit
Understanding Quantization Levels
| Quantization | Size | Quality | Speed | Use Case |
|---|---|---|---|---|
| IQ3_M | Smallest | Lower | Fastest | Very limited hardware |
| Q4_0 | Small | Good | Fast | Balanced for most users |
| Q4_K_M | Medium | Better | Moderate | Recommended default |
| Q5_K_M | Larger | High | Slower | High quality needed |
| Q8_0 | Largest | Highest | Slowest | Maximum accuracy |
Method 2: Download and Import with Modelfile
For more control over model behavior, download the GGUF file and create a custom Modelfile.
Step 1: Download GGUF File from HuggingFace
Option A: Using Git Clone
# Clone entire repository
git clone https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF
# Or download specific file only
git clone --depth=1 --no-checkout https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF
cd Llama-3.2-3B-Instruct-GGUF
git lfs pull --include="*Q4_K_M.gguf"
Option B: Using HuggingFace CLI
# Download specific quantization
huggingface-cli download bartowski/Llama-3.2-3B-Instruct-GGUF \
Llama-3.2-3B-Instruct-Q4_K_M.gguf \
--local-dir ./models \
--local-dir-use-symlinks False
Option C: Direct wget Download
# Create directory
mkdir -p ~/ollama-models/llama3.2-3b
# Download specific file
wget -O ~/ollama-models/llama3.2-3b/model.gguf \
"https://huggingface.co/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf"
Step 2: Create a Modelfile
Create a file named Modelfile in the same directory as your GGUF file:
cd ~/ollama-models/llama3.2-3b
nano Modelfile
Basic Modelfile:
# Specify the GGUF file
FROM ./model.gguf
# Define the chat template
TEMPLATE """<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|><|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
# Set parameters
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER top_k 40
PARAMETER num_ctx 4096
PARAMETER stop "<|eot_id|>"
PARAMETER stop "<|end_of_text|>"
# System prompt
SYSTEM """You are a helpful AI assistant. Respond clearly and concisely."""
Advanced Modelfile with Custom Behavior:
FROM ./model.gguf
TEMPLATE """<|im_start|>system
{{ .System }}<|im_end|>
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
"""
# Creativity settings
PARAMETER temperature 0.8
PARAMETER top_p 0.95
PARAMETER top_k 50
# Context window
PARAMETER num_ctx 8192
# Repetition control
PARAMETER repeat_penalty 1.1
PARAMETER repeat_last_n 64
# Stop sequences
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"
# Custom system prompt
SYSTEM """You are a coding assistant specialized in Python.
Provide clean, well-documented code with explanations."""
Step 3: Import into Ollama
# Navigate to model directory
cd ~/ollama-models/llama3.2-3b
# Create the model in Ollama
ollama create llama3.2-3b -f Modelfile
# Verify it was created
ollama list
# Run your model
ollama run llama3.2-3b
Method 3: Converting PyTorch to GGUF (Advanced)
If you have a PyTorch or SafeTensors model that’s NOT in GGUF format, you’ll need to convert it.
Step 1: Install llama.cpp
# Clone llama.cpp
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
# Build
make
# Install Python requirements
pip install -r requirements.txt
Step 2: Download HuggingFace Model
# Download a PyTorch model
huggingface-cli download meta-llama/Llama-2-7b-chat-hf \
--local-dir ./models/llama2-7b-chat \
--local-dir-use-symlinks False
Step 3: Convert to GGUF
# Convert PyTorch/SafeTensors to GGUF F16
python convert_hf_to_gguf.py ./models/llama2-7b-chat \
--outfile ./models/llama2-7b-chat.gguf \
--outtype f16
Output type options:
f16– 16-bit floating point (high quality)f32– 32-bit floating point (highest quality, largest size)q8_0– 8-bit quantization
Step 4: Quantize (Optional but Recommended)
# Quantize to Q4_K_M (recommended)
./llama-quantize ./models/llama2-7b-chat.gguf \
./models/llama2-7b-chat-Q4_K_M.gguf \
Q4_K_M
# Other quantization options:
# Q4_0, Q4_K_S, Q4_K_M, Q5_0, Q5_K_S, Q5_K_M, Q8_0
Step 5: Import to Ollama
Create a Modelfile:
FROM ./models/llama2-7b-chat-Q4_K_M.gguf
TEMPLATE """[INST] {{ .Prompt }} [/INST]"""
PARAMETER temperature 0.7
PARAMETER num_ctx 4096
SYSTEM """You are a helpful assistant."""
Import:
ollama create llama2-7b-chat -f Modelfile
ollama run llama2-7b-chat
Customization Options
Modelfile Parameters
| Parameter | Description | Example |
|---|---|---|
temperature | Randomness (0.0-2.0) | 0.7 (balanced) |
top_p | Nucleus sampling | 0.9 |
top_k | Top-k sampling | 40 |
num_ctx | Context window size | 4096 |
num_predict | Max tokens to generate | 128 |
repeat_penalty | Penalize repetition | 1.1 |
repeat_last_n | Look-back for repetition | 64 |
stop | Stop sequences | "<|end|>" |
Template Formats
Llama 3 Format:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
...
ChatML Format:
<|im_start|>system
...
<|im_end|>
Vicuna/Alpaca Format:
### Instruction:
...
### Response:
To find the correct template, check the model’s HuggingFace page for tokenizer_config.json.
Troubleshooting
Issue: “Model not found”
# Make sure Ollama is running
ollama serve
# In another terminal
ollama run hf.co/...
Issue: “Out of memory”
Try a smaller quantization:
# Instead of Q8_0, use Q4_K_M or IQ3_M
ollama run hf.co/bartowski/model:Q4_K_M
Issue: “Invalid template”
Check the model’s documentation for the correct chat template format. Many models include this in their model card.
Issue: Slow response times
# Check if model is using GPU
ollama ps
# Force GPU usage (if available)
OLLAMA_NUM_GPU=1 ollama run model-name
Issue: Model gives nonsensical responses
- Wrong template format
- Incorrect stop sequences
- Try adjusting temperature (lower = more deterministic)
Real-World Examples
Example 1: Running Llama 3.2 3B
# Direct run (easiest)
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
# Chat
>>> Write a Python function to calculate fibonacci numbers
Example 2: Code-Specialized Model
# Download CodeLlama
ollama run hf.co/bartowski/CodeLlama-7B-Instruct-GGUF:Q4_K_M
# Use for coding
>>> Explain this Python code: def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)
Example 3: Custom Medical Model
Download and customize:
# Download medical model
huggingface-cli download biotouchpoints/BioMistral-7B-GGUF \
BioMistral-7B.Q4_K_M.gguf \
--local-dir ./medical-model
# Create Modelfile
cat > Modelfile << 'EOF'
FROM ./BioMistral-7B.Q4_K_M.gguf
TEMPLATE """<s>[INST] {{ .Prompt }} [/INST]"""
PARAMETER temperature 0.3
PARAMETER num_ctx 4096
SYSTEM """You are a medical AI assistant. Provide accurate, evidence-based information.
Always remind users to consult healthcare professionals for medical advice."""
EOF
# Import
ollama create medical-assistant -f Modelfile
# Run
ollama run medical-assistant
Example 4: Running Private Models
# Copy your Ollama SSH key
cat ~/.ollama/id_ed25519.pub | pbcopy
# Add to HuggingFace account:
# Settings → SSH Keys → Add new SSH key
# Run private model
ollama run hf.co/your-username/private-model
Example 5: Multi-Language Model
# Qwen model (excellent for multilingual)
ollama run hf.co/bartowski/Qwen2.5-7B-Instruct-GGUF:Q4_K_M
>>> 用中文告诉我关于人工智能的历史
>>> Explícame la historia de la inteligencia artificial en español
Quick Reference Commands
# Direct run from HuggingFace
ollama run hf.co/{username}/{repo}:{quantization}
# List downloaded models
ollama list
# Remove a model
ollama rm model-name
# Show model info
ollama show model-name
# Create from Modelfile
ollama create model-name -f Modelfile
# Copy/rename model
ollama cp source-model new-name
# Pull official Ollama model
ollama pull llama3.2
# Check running models
ollama ps
# Stop Ollama service
ollama stop
Best Practices
✅ DO:
- Start with Q4_K_M quantization (best balance)
- Use direct HuggingFace integration when possible
- Check model card for correct template format
- Test with small quantizations first on limited hardware
- Keep models organized in dedicated directories
❌ DON’T:
- Download all quantizations (pick one that fits your hardware)
- Skip reading the model card (contains crucial template info)
- Use Q8_0 on systems with < 16GB RAM
- Forget to specify stop sequences in Modelfile
Summary
Easiest Method (Recommended for Most Users)
ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:Q4_K_M
Most Control
- Download GGUF file
- Create custom Modelfile
ollama createand run
Converting from PyTorch
- Use llama.cpp’s
convert_hf_to_gguf.py - Quantize with
llama-quantize - Import to Ollama
Additional Resources
- Ollama Documentation: https://github.com/ollama/ollama/tree/main/docs
- HuggingFace GGUF Models: https://huggingface.co/models?library=gguf
- llama.cpp: https://github.com/ggerganov/llama.cpp
- Quantization Guide: https://github.com/ggerganov/llama.cpp/blob/master/examples/quantize/README.md
Happy model running! 🚀
For questions or issues, check the Ollama GitHub Issues or HuggingFace Forums.