Install HF Transformers & Datasets
Install Hugging Face Transformers and datasets — LLM Finetuning lesson. Hands-on setup for fine-tuning pipelines.
Focus: install hugging face transformers and datasets
You've built a solid foundation in Python, PyTorch, and the fundamentals of LLMs, but now comes the moment where theory meets practice: you want to download a pretrained model and fine-tune it on your own data. Without the right libraries, you'll find yourself wrestling with model architectures, tokenizers, and dataset formats that Hugging Face has already solved for you. This lesson is your hands-on guide to install Hugging Face Transformers and datasets, the two essential libraries that unlock the entire LLM fine-tuning ecosystem.
The problem this lesson solves
Every serious LLM project — whether you're fine-tuning a small model like distilbert or a 7B-parameter giant like Llama-2 — relies on the same core infrastructure: the Transformers library for model architectures, tokenizers, and training utilities, and the Datasets library for efficient data loading and preprocessing. Without these, you'd have to implement attention mechanisms from scratch, write your own tokenizers, and manually handle batching and shuffling — a massive waste of time that the community has long since automated.
Trying to skip the installation step leads to a cascade of failures: ModuleNotFoundError, GPU out-of-memory errors from inefficient data loading, and version conflicts between libraries. The pain is real, and it hits exactly when you're most excited to start fine-tuning. This lesson removes those roadblocks so your next step — actually fine-tuning a model — goes smoothly.
Core concept / mental model
Think of Hugging Face's ecosystem as a well-organized toolkit for machine learning. The Transformers library is like a universal adapter that lets you load any pretrained model — be it a BERT, GPT, or T5 — with just a few lines of code. The Datasets library is your data warehouse, designed to handle massive datasets that don't fit in memory by streaming them from disk. Together, they form the backbone of virtually every modern LLM project.
A useful analogy: installing these libraries is like setting up a professional kitchen before cooking a gourmet meal. The Transformers library provides the high-end appliances (model architectures, training loops), and the Datasets library is your pantry organization system (efficient data loading, caching, and preprocessing). With the kitchen ready, you can focus on the recipe — your fine-tuning script — instead of worrying about the tools.
The installation process itself is straightforward: you use pip, Python's package installer, to fetch and install the packages from the Python Package Index (PyPI). The key is doing it correctly the first time, with the right environment and dependencies, to avoid headaches later.
How it works step by step
Here's the logical sequence you'll follow to install Hugging Face Transformers and datasets successfully.
Step 1: Create a virtual environment
Before installing anything, isolate your project dependencies. Virtual environments prevent version conflicts between different projects and keep your global Python installation clean. Use venv (built into Python 3.3+) or conda if you're already in the Anaconda ecosystem.
# Create and activate a virtual environment (Linux/macOS)
python3 -m venv hf-env
source hf-env/bin/activate
# Windows (PowerShell)
python -m venv hf-env
hf-env\Scripts\activate
Step 2: Install PyTorch (or TensorFlow)
Transformers requires a deep learning framework. PyTorch is the default choice for most LLM work, but TensorFlow is also supported. Install PyTorch according to your hardware:
- CPU-only:
pip install torch - CUDA (NVIDIA GPU): Follow the official instructions at pytorch.org to get the correct version for your CUDA version.
# Example for CUDA 12.1
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Step 3: Install Transformers and Datasets
Once PyTorch is ready, install the core libraries. Use pip install with the latest versions, and optionally include sentencepiece for tokenization support.
pip install transformers datasets
pip install sentencepiece # Often needed for tokenizers
Step 4: Verify the installation
Import the libraries and check their versions to confirm everything works.
import transformers
import datasets
print(f"Transformers version: {transformers.__version__}")
print(f"Datasets version: {datasets.__version__}")
Expected output (versions may differ):
Transformers version: 4.40.0
Datasets version: 2.19.0
Step 5: Test loading a small model and dataset
A quick smoke test ensures the installation is functional. Load a tiny model and a small dataset to confirm the libraries interact correctly.
from transformers import pipeline
from datasets import load_dataset
# Load a tiny sentiment-analysis pipeline
test_pipeline = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
print(test_pipeline("I love this tutorial!"))
# Load a small subset of a dataset
small_dataset = load_dataset("imdb", split="train[:10]")
print(small_dataset)
Expected output:
[{'label': 'POSITIVE', 'score': 0.999...}]
Dataset({
features: ['text', 'label'],
num_rows: 10
})
If this works, your installation is solid and ready for fine-tuning.
Hands-on walkthrough
Let's go through a complete, reproducible setup from scratch. This walkthrough assumes you have Python 3.10+ installed.
Full installation script
# 1. Create a project directory
mkdir llm-finetuning-project
cd llm-finetuning-project
# 2. Set up virtual environment
python3 -m venv venv
source venv/bin/activate
# 3. Install PyTorch (CPU or CUDA — adjust as needed)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu # CPU version
# 4. Install Hugging Face libraries
pip install transformers datasets
# 5. Optional: install extra tokenizer support
pip install sentencepiece
# 6. Verify installation
python -c "import transformers; print(transformers.__version__)"
python -c "import datasets; print(datasets.__version__)"
Test with a tokenizer and model
Now that everything is installed, let's load a tokenizer and model together — the core pattern you'll use in every fine-tuning script.
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Load tokenizer and model for a small classification task
model_name = "distilbert-base-uncased"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
# Tokenize a sample input
inputs = tokenizer("Hello, I'm fine-tuning my first LLM!", return_tensors="pt")
print(inputs["input_ids"])
# Get model output (logits)
import torch
with torch.no_grad():
outputs = model(**inputs)
print(outputs.logits)
Expected output:
tensor([[ 101, 7592, 1010, 1045, 1005, 2999, 2025, 2034, 1010, 2123,
999, 102]])
tensor([[-0.1023, 0.0875]])
This confirms the full pipeline — tokenizer and model — is operational.
Compare options / when to choose what
When installing Hugging Face libraries, you have a few choices. Here's a comparison to guide you.
| Approach | Pros | Cons | Best for |
|---|---|---|---|
pip install transformers (latest) |
Simple, always latest | May introduce breaking changes | Most users |
pip install transformers==specific.version |
Reproducible | Older features, security patches lag | Production code |
conda install -c huggingface transformers |
Handles dependencies well | Sometimes outdated, less common | Conda users |
Install from source (git clone + pip install -e .) |
Access development features | Requires Git, unstable | Contributors, bleeding-edge users |
When to choose what:
- For learning and experimentation, use the
piplatest version. It's easy and risks are minimal. - For production or shared projects, pin the version using a
requirements.txtorpyproject.tomlfile to ensure everyone uses the same version. - For GPU-heavy work, ensure you install the CUDA-compatible PyTorch before installing Transformers to avoid dependency mismatches.
Pro tip: Always create a
requirements.txtfile listing your exact versions. This makes your setup reproducible and saves hours of debugging later.
Troubleshooting & edge cases
Here are the most common issues you'll face and how to fix them.
ModuleNotFoundError: No module named 'transformers'
- Cause: The library isn't installed (or installed in a different environment).
- Fix: Check your virtual environment is active (
which pipon Unix,pip --versionon Windows). Install withpip install transformers. If using Jupyter notebooks, restart the kernel or install inside the notebook.
ImportError: libcublas.so.11: cannot open shared object file
- Cause: PyTorch was compiled for a different CUDA version than your system.
- Fix: Reinstall PyTorch with the correct CUDA version. Uninstall first:
pip uninstall torch, then follow the official PyTorch installation guide for your CUDA version.
Version mismatch between Transformers and Datasets
- Cause: Incompatible versions of the two libraries.
- Fix: Upgrade both libraries together using
pip install --upgrade transformers datasets. Or downgrade to a known-good pair (e.g.,transformers==4.40.0 datasets==2.19.0).
Out of memory during dataset loading
- Cause: Dataset is too large to fit in RAM.
- Fix: Use the
streaming=Trueparameter when loading withload_dataset, or load a small split/subset:load_dataset("imdb", split="train[:1000]").
SSL certificate errors when downloading models
- Cause: Corporate firewalls or old
certificertificates. - Fix: Update
certifi:pip install --upgrade certifi. Set environment variableREQUESTS_CA_BUNDLEto your custom CA bundle if needed.
OSError: Can't load tokenizer because no tokenizer.json found
- Cause: The model repository doesn't include a tokenizer file.
- Fix: Use a different model that includes a tokenizer, or manually create a tokenizer from the model's vocabulary. For fine-tuning, it's best to stick with pretrained models that have tokenizers.
What you learned & what's next
You've taken a critical step in your LLM fine-tuning journey: you now have a working environment with Transformers and Datasets installed. You can create a virtual environment, install PyTorch with the right CUDA support, and verify everything works by loading a model and tokenizer. These are the exact tools you'll use in every subsequent lesson — from data preparation to training loops.
Now that your foundation is solid, the next lesson in this track will guide you through choosing a pretrained model and dataset for your fine-tuning task. You'll learn how to select the right architecture, prepare your data in the format that Transformers expects, and avoid common pitfalls when adapting a generic model to your specific use case. With the libraries installed, you're ready to dive into the real work of customization.
Practice recap
Create a fresh virtual environment and install Transformers and Datasets from scratch, then write a short script that loads a small model (e.g., distilbert-base-uncased) and a tiny dataset (e.g., imdb with 10 rows). Run it to confirm everything works, then try loading a model with a tokenizer and print the tokenized input IDs. This exercise cements the setup so you're ready for the next lesson on choosing models and datasets for fine-tuning.
Common mistakes
- Installing Transformers before installing PyTorch, which leads to conflicts or missing dependencies. Always install PyTorch first.
- Using a different Python environment than the one where you installed the libraries — check your active environment with
which python. - Not pinning versions, causing random breaking changes in future installs. Use a
requirements.txt. - Forgetting to activate the virtual environment in a new terminal session, resulting in
ModuleNotFoundError. - Installing CPU-only PyTorch when you have an NVIDIA GPU, missing out on massive speedups and potential CUDA errors.
Variations
- Use
condainstead ofvenvfor environment management if you're already in the Anaconda ecosystem. - Install from source (
git clone+pip install -e .) for bleeding-edge features, but be prepared for daily changes. - Use Docker to create a fully isolated environment with all dependencies pre-installed, great for reproducibility.
Real-world use cases
- Setting up a reproducible environment for a team fine-tuning a sentiment classifier on customer reviews.
- Deploying a fine-tuned model in production where pinned versions ensure consistent behavior across servers.
- Creating a CI/CD pipeline that installs and tests Transformers and Datasets for automated model retraining.
Key takeaways
- Always create a virtual environment to isolate dependencies before installing Transformers and Datasets.
- Install PyTorch with the correct CUDA version before installing Transformers to avoid hard-to-debug errors.
- Use
pip install transformers datasetsas the core command, optionally addingsentencepiecefor tokenizer support. - Verify your installation by checking versions and running a small model/dataset load to catch issues early.
- Pin your library versions in a requirements file to make your setup reproducible and maintainable.
- Troubleshoot common issues like module errors and CUDA mismatches by checking your environment and reinstalling with correct options.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.