Rate this Page

Installing C++ Distributions of PyTorch#

We provide binary distributions of all headers, libraries and CMakeconfiguration files required to depend on PyTorch. We call this distributionLibTorch, and you can download ZIP archives containing the latest LibTorchdistribution onour website. Belowis a small example of writing a minimal application that depends on LibTorchand uses thetorch::Tensor class which comes with the PyTorch C++ API.

Minimal Example#

The first step is to download the LibTorch ZIP archive via the link above. Forexample:

wgethttps://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zipunziplibtorch-shared-with-deps-latest.zip

Note that the above link has CPU-only libtorch. If you would like to download a GPU-enabledlibtorch, find the right link in the link selector onhttps://pytorch.org

If you’re a Windows developer and wouldn’t like to use CMake, you could jump to the Visual StudioExtension section.

Next, we can write a minimal CMake build configuration to develop a smallapplication that depends on LibTorch. CMake is not a hard requirement for usingLibTorch, but it is the recommended and blessed build system and will be wellsupported into the future. A most basicCMakeLists.txt file could look likethis:

cmake_minimum_required(VERSION3.18FATAL_ERROR)project(example-app)find_package(TorchREQUIRED)set(CMAKE_CXX_FLAGS"${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")add_executable(example-appexample-app.cpp)target_link_libraries(example-app"${TORCH_LIBRARIES}")set_property(TARGETexample-appPROPERTYCXX_STANDARD17)# The following code block is suggested to be used on Windows.# According to https://github.com/pytorch/pytorch/issues/25457,# the DLLs need to be copied to avoid memory errors.if(MSVC)file(GLOBTORCH_DLLS"${TORCH_INSTALL_PREFIX}/lib/*.dll")add_custom_command(TARGETexample-appPOST_BUILDCOMMAND${CMAKE_COMMAND}-Ecopy_if_different${TORCH_DLLS}$<TARGET_FILE_DIR:example-app>)endif(MSVC)

The implementation of our example will simply create a newtorch::Tensor andprint it:

#include<torch/torch.h>#include<iostream>intmain(){torch::Tensortensor=torch::rand({2,3});std::cout<<tensor<<std::endl;}

While there are more fine-grained headers you can include to access only partsof the PyTorch C++ API, includingtorch/torch.h is the most sure-proof way ofincluding most of its functionality.

The last step is to build the application. For this, assume our exampledirectory is laid out like this:

example-app/CMakeLists.txtexample-app.cpp

We can now run the following commands to build the application from within theexample-app/ folder:

mkdirbuildcdbuildcmake-DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch..cmake--build.--configRelease

where/absolute/path/to/libtorch should be the absolute (!) path to the unzipped LibTorchdistribution. If PyTorch was installed via pip,CMAKE_PREFIX_PATH can be queriedusingtorch.utils.cmake_prefix_path variable. In that case CMake configuration step would look something like follows:

cmake-DCMAKE_PREFIX_PATH=`python3-c'import torch;print(torch.utils.cmake_prefix_path)'`..

If all goes well, it will look something like this:

root@4b5a67132e81:/example-app#mkdirbuildroot@4b5a67132e81:/example-app#cdbuildroot@4b5a67132e81:/example-app/build#cmake-DCMAKE_PREFIX_PATH=/path/to/libtorch..--TheCcompileridentificationisGNU5.4.0--TheCXXcompileridentificationisGNU5.4.0--CheckforworkingCcompiler:/usr/bin/cc--CheckforworkingCcompiler:/usr/bin/cc--works--DetectingCcompilerABIinfo--DetectingCcompilerABIinfo-done--DetectingCcompilefeatures--DetectingCcompilefeatures-done--CheckforworkingCXXcompiler:/usr/bin/c++--CheckforworkingCXXcompiler:/usr/bin/c++--works--DetectingCXXcompilerABIinfo--DetectingCXXcompilerABIinfo-done--DetectingCXXcompilefeatures--DetectingCXXcompilefeatures-done--Lookingforpthread.h--Lookingforpthread.h-found--Lookingforpthread_create--Lookingforpthread_create-notfound--Lookingforpthread_createinpthreads--Lookingforpthread_createinpthreads-notfound--Lookingforpthread_createinpthread--Lookingforpthread_createinpthread-found--FoundThreads:TRUE--Configuringdone--Generatingdone--Buildfileshavebeenwrittento:/example-app/buildroot@4b5a67132e81:/example-app/build#cmake--build.--configReleaseScanningdependenciesoftargetexample-app[50%]BuildingCXXobjectCMakeFiles/example-app.dir/example-app.cpp.o[100%]LinkingCXXexecutableexample-app[100%]Builttargetexample-app

Executing the resultingexample-app binary found in thebuild foldershould now merrily print the tensor (exact output subject to randomness):

root@4b5a67132e81:/example-app/build#./example-app0.20630.65930.08660.07960.58410.1569[Variable[CPUFloatType]{2,3}]

Tip

On Windows, debug and release builds are not ABI-compatible. If you plan tobuild your project in debug mode, please try the debug version of LibTorch.Also, make sure you specify the correct configuration in thecmake--build.line above.

System Requirements#

To ensure smooth installation and usage of LibTorch, please ensure your systemmeets the following requirements:

  1. GLIBC Version:

  • GLIBC 2.29 or newer for cxx11 ABI version

  1. GCC Version:

  • GCC 9 or newer for cxx11

Visual Studio Extension#

LibTorch Project Template can help Windows developersset all libtorch project settings and link options for debug and release.It’s easy to use and you could check out thedemo video.The only prerequisite is to download the libtorch onhttps://pytorch.org

Support#

If you run into any troubles with this installation and minimal usage guide,please use ourforum orGitHub issues to get in touch.