How to properly install a project that depends on LibTorch?

I followed the official tutorial to build the example LibTorch app. It works just fine from the build directory, but for a real project, I would like my users to install the app after building it. So I added installation instructions to my CMakeLists.txt:

# Code from the official tutorial:
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(example-app)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 17)

# New: Install the app:
set(CMAKE_INSTALL_PREFIX "$ENV{HOME}/.local" CACHE PATH "Default install path" FORCE)
include(GNUInstallDirs)
install(
  TARGETS example-app
  LIBRARY
  DESTINATION "${CMAKE_INSTALL_BINDIR}"
)

With this, make install installs the example app to ~/.local/bin/example-app, but from there, it does not work anymore:

$ example-app 
example-app: error while loading shared libraries: libtorch.so: cannot open shared object file: No such file or directory

Using ldd shows that the example in in the build directory finds the shared libraries that I originally unzipped, while the installed example app does not find them anymore. Lacking experience in C++ deployment, I am not sure how to proceed. Here are my thoughts:

  1. Can I tell CMake to also install the shared LibTorch libraries? If so, how? Would that be a good practice considering users may have installed LibTorch already via some other means?
  2. Should I require my users to have LibTorch libraries already installed? If so, by what means should they be installed preferably? Could this cause mismatches between library versions, and should I adjust the CMakeLists.txt to link against already present LibTorch instead of the files from the zip archive?
  3. Can and should I statically link to LibTorch so that my app is self-contained?

I realize these question may be more C++ than PyTorch related, but any clarifying input would be very appreciated. :sweat_smile:

For now, I added the following to my CMake script:

install(
  DIRECTORY ${torch_SOURCE_DIR}/lib/
  DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)

Where torch_SOURCE_DIR holds the path to the extracted LibTorch archive. This works, because it simply installs all the shared libraries that LibTorch provides. I am not sure if this is a good practice though.