发布于 2026-08-20 22:24:11

CUDA nvcc环境配置问题与迷思

主要讲在Linux中怎么配。

以Debian为例,虽然源里也有nvidia-cuda-toolkit,但是其版本太低,Debian 13的nvcc居然是24年的,还不支持Debian 13源里的默认的gcc 14。所以安装nvcc主要有如下两种方式。

安装

通过pip安装

直接uv pip install nvidia-cuda-nvcc即可,头文件什么都是齐全的,是最简单的方式。

在VSCode中,应当这样配置c_cpp_properties.json(主要是compilerPath),相关的语法和标识符才不会飘红:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "compilerPath": "/mnt/d/work/python-work/cuda-learn/.venv/lib/python3.14/site-packages/nvidia/cu13/bin/nvcc"
        }
    ],
    "version": 4
}

然后将nvcc的bin路径加到PATH即可。

通过Nvidia私有源安装

参考官方文档的说明,步骤如下:

# wget https://developer.download.nvidia.com/compute/cuda/repos/<distro>/<arch>/cuda-keyring_1.1-1_all.deb
# Debian 13 x86_64
wget https://developer.download.nvidia.cn/compute/cuda/repos/debian13/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt install cuda-toolkit

然后把/usr/local/cuda/bin加到PATH里即可。

在VSCode的c_cpp_properties.json中,compilerPath要配置成/usr/local/cuda/bin/nvcc

测试

可以跑一下向量加的cuda-samples

编译并运行:

nvcc add.cu -arch=sm_86 -o add && ./add

-arch=sm_86是因为我装的nvcc V13.3.73默认是sm_75的(CUDA 13.X最低支持硬件就是sm_75),而我的3070Ti是sm_86。

CUDA Tile C++ API Reference, Release 13.3文档上有写:
Note: NVCC targets sm_75 as the default architecture if no architecture is provided. In this configuration, no tile code will be generated and a tile kernel launch will fail at runtime.

欢迎留言