Overview
LibTorch is the C++ distribution of PyTorch. It contains all headers, libraries, and CMake configuration files required to depend on PyTorch from C++.System Requirements
Before installing LibTorch, ensure your system meets these requirements:Linux
- GLIBC: Version 2.29 or newer (for cxx11 ABI version)
- GCC: Version 9 or newer (for cxx11)
- CMake: Version 3.18 or higher
- Disk Space: At least 2GB for LibTorch binaries
Windows
- Visual Studio: 2017, 2019, or 2022
- CMake: Version 3.18 or higher
- Disk Space: At least 2GB for LibTorch binaries
macOS
- macOS: 10.13 (High Sierra) or later
- Xcode: Latest version recommended
- CMake: Version 3.18 or higher
Download LibTorch
Binary Distribution
Download pre-built LibTorch from the PyTorch website.Download the Archive
Download the appropriate LibTorch distribution for your system.CPU-only (Linux):GPU-enabled (Linux with CUDA 12.4):
For GPU support, select the LibTorch distribution matching your CUDA version. Find the right link using the selector on pytorch.org.
From PyTorch Python Installation
If you have PyTorch installed via pip, you can query the LibTorch path:Building from Source
Prerequisites
To build PyTorch from source, you need:- Python: 3.10 or later
- Compiler: Full C++20 support (GCC 11.3.0+ on Linux, clang on macOS)
- Disk Space: At least 10GB free
- Build Time: 30-60 minutes for initial build
Clone the Repository
Install Dependencies
Build PyTorch
For CUDA support on Linux, ensure CUDA is properly installed and
USE_CUDA=1 is set (default). To disable CUDA, set USE_CUDA=0.Creating Your First Application
Project Structure
Create a minimal LibTorch application with this structure:CMakeLists.txt
Create aCMakeLists.txt file:
Application Code
Createexample-app.cpp:
Including
<torch/torch.h> is the most reliable way to access the PyTorch C++ API. For more fine-grained control, you can include specific headers.Build the Application
Platform-Specific Notes
Visual Studio Extension (Windows)
For Windows developers not using CMake, the LibTorch Project Template Visual Studio extension can automatically configure project settings and link options. Features:- Automatic setup for debug and release configurations
- Pre-configured include paths and library links
- Easy project creation
CUDA Support
For GPU-enabled builds:- Install NVIDIA CUDA: Download from NVIDIA
- Install cuDNN: Version 8.5 or above from NVIDIA
- Compatible Compiler: Check CUDA compatibility
ROCm Support (AMD GPUs)
For AMD GPU support on Linux:- Install ROCm: Version 4.0+ from AMD
- Set ROCm Path (if not in
/opt/rocm):
Troubleshooting
CMake Cannot Find Torch
Problem:CMake Error: Could not find a package configuration file provided by "Torch"
Solution: Ensure CMAKE_PREFIX_PATH points to the absolute path of your LibTorch installation:
Linking Errors on Windows
Problem: Unresolved external symbols or DLL not found errors Solution:- Verify debug/release configuration matches LibTorch build
- Ensure DLL copy step in CMakeLists.txt is present
- Check that Visual Studio version is compatible
Missing OpenMP (macOS)
Problem: OpenMP-related errors during build Solution: Install OpenMP via Homebrew:Getting Help
If you encounter installation issues:- Forum: PyTorch Discussion Forum
- GitHub: PyTorch Issues
- Documentation: Official C++ Docs
Next Steps
Now that you have LibTorch installed, learn about:- Tensor Operations - Working with tensors in C++
- Autograd - Automatic differentiation
- Modules - Building neural networks