Implementation:Huggingface Transformers Pip Install Transformers
| Knowledge Sources | |
|---|---|
| Domains | NLP, Inference, DevOps |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
Concrete tool for installing the HuggingFace Transformers library with PyTorch dependencies via pip, the Python package installer.
Description
The command pip install transformers[torch] installs the Transformers library along with its PyTorch-specific optional dependencies. This resolves the full dependency graph defined in setup.py, including core requirements (huggingface-hub, numpy, tokenizers, safetensors) and the torch extras group (torch, accelerate).
The [torch] extras specifier triggers pip to install the packages listed under extras_require["torch"] in addition to the base install_requires. This is the recommended installation method for users who intend to run inference on PyTorch-backed models.
Usage
Use this command when setting up a new Python environment for Transformers-based inference. It is the first step before any model loading or pipeline usage. Choose the appropriate extras group based on your modality and hardware:
transformers[torch]-- for PyTorch-based text, vision, or audio models.transformers[vision]-- adds TorchVision and Pillow for image-based models.transformers[audio]-- adds torchaudio, librosa, and CTC decoding for speech models.transformers[all]-- installs all optional dependencies.
Code Reference
Source Location
- Repository: transformers
- File:
setup.py(lines 68-274)
Signature
pip install transformers[torch]
Import
# After installation, verify the setup:
import transformers
import torch
import accelerate
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| package specifier | str |
Yes | The pip package specifier, e.g. transformers[torch]. The extras group in brackets selects optional dependencies.
|
| --upgrade | flag | No | Pass -U or --upgrade to upgrade an existing installation to the latest version.
|
| --index-url | str |
No | Custom PyPI index URL for private or alternative package registries. |
Outputs
| Name | Type | Description |
|---|---|---|
| Installed packages | Python packages on disk | Core dependencies: huggingface-hub>=1.3.0,<2.0, numpy>=1.17, packaging>=20.0, pyyaml>=5.1, regex, tokenizers>=0.22.0,<=0.23.0, safetensors>=0.4.3, tqdm>=4.27. Torch extras: torch>=2.4, accelerate>=1.1.0.
|
| Exit code | int |
0 on success, non-zero on dependency resolution failure. |
Usage Examples
Basic Usage
# Install transformers with PyTorch support
pip install transformers[torch]
Upgrade Existing Installation
# Upgrade to latest version
pip install --upgrade transformers[torch]
Install in a Virtual Environment
python -m venv .venv
source .venv/bin/activate
pip install transformers[torch]
Verify Installation
import transformers
print(transformers.__version__)
import torch
print(torch.__version__)
from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")
print(generator("Hello, world", max_new_tokens=20))