Download Models from Hugging Face
Hugging Face hosts thousands of pre-trained machine learning models, but downloading them isn't always straightforward if you're new to the platform. This guide covers multiple methods to download models from Hugging Face, whether you prefer programmatic approaches, command-line tools, or manual downloads.
Problem Overview
Hugging Face models are stored in Git repositories with Large File Storage (LFS) for handling large model files. Unlike typical file hosting services, there's no simple "Download" button for the entire model package, which can be confusing for newcomers.
Recommended Download Methods
1. Using Hugging Face Hub Library (Python)
The most reliable method is using the official huggingface_hub
library:
from huggingface_hub import snapshot_download
# Download entire model repository
model_path = snapshot_download(repo_id="bert-base-uncased")
For downloading specific files:
from huggingface_hub import hf_hub_download
# Download a specific file
model_path = hf_hub_download(
repo_id="CompVis/stable-diffusion-v-1-4-original",
filename="sd-v1-4.ckpt"
)
2. Using Hugging Face CLI
Install the CLI tool and download models directly:
pip install huggingface_hub
# Download a model
huggingface-cli download bert-base-uncased
For better performance with large downloads:
pip install huggingface_hub[hf_transfer]
huggingface-cli download xai-org/grok-1 --repo-type model --include ckpt-0/* --local-dir checkpoints
3. Git with LFS Method
For complete repository cloning (requires Git LFS):
# Install Git LFS first (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/google-bert/bert-base-uncased
WARNING
git lfs clone
is deprecated. Use regular git clone
after installing Git LFS.
Alternative Download Methods
Manual Download via Browser
- Navigate to the model's page on Hugging Face
- Click on the "Files and versions" tab
- Click the download icon next to individual files
- For large files, you may need to click the "LFS" indicator
Using wget or curl
For direct file downloads:
# Using wget
wget https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0/resolve/main/sd_xl_base_1.0.safetensors
# Using curl
curl -L -O https://huggingface.co/TheBloke/falcon-7b-instruct-GGML/resolve/main/falcon-7b-instruct.ggccv1.q4_1.bin
For multi-part downloads:
#!/bin/bash
for i in {1..15}; do
wget --show-progress -c "https://huggingface.co/unsloth/DeepSeek-R1-GGUF/resolve/main/DeepSeek-R1-Q8_0/DeepSeek-R1.Q8_0-0000${i}-of-00015.gguf?download=true" \
-O "DeepSeek-R1.Q8_0-0000${i}-of-00015.gguf"
done
Model Caching Behavior
When using Transformers library, models are automatically cached locally on first use:
from transformers import AutoTokenizer, AutoModelForMaskedLM
# First run downloads and caches the model
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForMaskedLM.from_pretrained("bert-base-uncased")
The default cache location is:
- Linux:
~/.cache/huggingface/
- Windows:
C:\Users\username\.cache\huggingface\
Special Considerations
Slow Internet Connections
For unstable or slow connections, use the resume capability of wget
or curl
:
# curl with continue-at functionality
curl -L -C - -O https://huggingface.co/path/to/large/file
Enterprise Environments
In restricted corporate environments where direct downloads might be blocked:
- Use GitHub repositories when available
- Download during off-peak hours
- Use approved corporate download tools
Best Practices
- Always verify downloads - Check hashes when provided
- Respect license terms - Some models have specific usage restrictions
- Manage storage - Models can be large (several GB)
- Use specific revisions - Pin to specific commits for reproducibility:
snapshot_download(repo_id="bert-base-uncased", revision="a1b2c3d4")
Troubleshooting
Common issues and solutions:
"Git LFS not installed"
# Install on Ubuntu/Debian
sudo apt-get install git-lfs
git lfs install
# On Windows/macOS, download from https://git-lfs.com
"Incomplete downloads"
Use tools with resume capability (wget -c
or curl -C -
)
"SSL certificate errors"
Try using the --insecure
flag with curl or configure your system's certificate store
Conclusion
The easiest method for most users is the huggingface_hub
Python library or CLI tool. For complete repository access, use Git with LFS. For individual files, manual downloading or direct HTTP requests work well. Choose the method that best fits your workflow and technical requirements.
For advanced usage, refer to the official Hugging Face documentation.