You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下如何查找CUDA路径?安装Libtorch遇CUDA路径错误

问题解决:Libtorch编译时CMake找不到CUDA_TOOLKIT_ROOT_DIR

问题背景

已安装PyTorch且可正常调用GPU,但编译Libtorch时CMake持续报错:

CUDA_TOOLKIT_ROOT_DIR not found or specified

相关报错信息

CMake错误输出:

-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
CUDA_TOOLKIT_ROOT_DIR not found or specified
-- Could NOT find CUDA (missing: CUDA_TOOLKIT_ROOT_DIR CUDA_NVCC_EXECUTABLE CUDA_INCLUDE_DIRS CUDA_CUDART_LIBRARY) 

Caffe2: CUDA cannot be found.  Depending on whether you are building Caffe2
  or a Caffe2 dependent library, the next warning / error will give you more
  info

sudo find / |grep nvcc命令输出:

/usr/local/share/cmake-3.24/Modules/FindCUDA/run_nvcc.cmake
/usr/local/boost_1_80_0/boost/predef/compiler/nvcc.h
/usr/local/boost_1_80_0/boost/config/compiler/nvcc.hpp
/usr/share/cmake-3.16/Modules/FindCUDA/run_nvcc.cmake
/usr/include/boost/predef/compiler/nvcc.h
/usr/include/boost/config/compiler/nvcc.hpp
find: ‘/run/user/1000/doc’: Permission denied
find: ‘/run/user/1000/gvfs’: Permission denied
find: ‘/run/user/125/gvfs’: Permission denied
/home/xxx/.local/share/Trash/files/libtorch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/.local/share/xmake/modules/core/tools/nvcc.lua
/home/xxx/.local/share/xmake/modules/detect/tools/find_nvcc.lua
/home/xxx/.local/share/xmake/modules/detect/tools/nvcc
/home/xxx/.local/share/xmake/modules/detect/tools/nvcc/has_flags.lua
/home/xxx/Documents/xxx/mongo/env/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/Documents/pytorch/libtorch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/Documents/mongo/env/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/Documents/xxx/xx/env/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
find: ‘/home/midas/mydrive’: Permission denied
/home/xxx/git/xxx-projects/xx/Script/env/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/git/midas-engine/cicd-venv/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/home/xxx/git/xxx-xx/venv/lib/python3.8/site-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/var/lib/docker/overlay2/<large-string>/diff/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake
/var/lib/docker/overlay2/<large-string>/diff/usr/local/lib/python3.8/dist-packages/torch/share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/run_nvcc.cmake

解决方案

1. 确认系统是否安装完整CUDA Toolkit

从find输出看,未找到nvcc可执行文件(通常路径为/usr/local/cuda/bin/nvcc),说明系统可能仅通过PyTorch包获得了CUDA Runtime,但缺少完整的CUDA Toolkit(Libtorch编译需要Toolkit提供的nvcc、头文件等)。

  • 运行nvcc --version,若提示命令不存在,需安装对应版本的CUDA Toolkit:
    • 版本需与PyTorch依赖的CUDA版本匹配(可通过torch.version.cuda查看PyTorch的CUDA版本)。

2. 手动指定CUDA路径给CMake

若已安装CUDA Toolkit但CMake无法自动识别,运行CMake时添加参数指定路径:

cmake -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..

将/usr/local/cuda替换为你的实际CUDA安装路径。

3. 配置环境变量

将CUDA的可执行文件和库路径加入环境变量,确保系统能找到nvcc:

export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

若需永久生效,将上述两行添加到~/.bashrc(或~/.zshrc,取决于你的shell)文件末尾,然后执行source ~/.bashrc生效。

4. 匹配Libtorch与PyTorch、CUDA版本

确保下载的Libtorch版本与PyTorch版本完全一致,且CUDA版本对应(例如PyTorch为1.13.1+cu117,则下载Libtorch的cu117版本)。

内容的提问来源于stack exchange,提问作者user20289089

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:25:22