Ubuntu 24.04下CUDA 13.0开发环境搭建和测试
在Ubuntu 24.04下安装CUDA 13.0的开发环境,并使用cuda-samples进行测试。
1. 系统准备
在正式安装 CUDA 之前,先确保系统是最新的,并具备编译工具和基础依赖:
sudo apt update sudo apt upgrade -y sudo apt install -y build-essential dkms wget curl git
说明:
build-essential 提供 gcc/g++ 编译环境;
dkms 用于动态编译驱动内核模块;
wget、curl、git 方便后续下载源码和工具。
2. 清理旧版本(可选)
如果系统上曾经装过旧版 NVIDIA 驱动或 CUDA,建议先卸载,避免冲突:
sudo apt remove --purge 'nvidia-*' 'cuda-*' 'libnvidia-*' -y sudo apt autoremove -y sudo apt autoclean sudo reboot
说明:
remove --purge 会彻底清除旧驱动;
autoremove 删除残留依赖;
建议重启后再继续,确保内核加载干净。
3. 添加 NVIDIA 官方仓库(支持 Ubuntu 24.04)
Ubuntu 24 还较新,需要手动添加 NVIDIA 的官方仓库源:
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb sudo dpkg -i cuda-keyring_1.1-1_all.deb sudo apt update
说明:
这一步会让系统能够从 NVIDIA 官方渠道获取最新的 CUDA 13.0 包,而不是 Ubuntu 默认源中的旧版本。
4. 安装 CUDA 13.0 与驱动
安装命令如下:
sudo apt install -y cuda
说明:
这会自动安装驱动(通常为 580.xx 及以上)和 CUDA 13.0 工具链。
包含了 nvcc 编译器、cuBLAS/cuDNN 库、以及运行时依赖。
安装完成后无需手动装驱动。
5. 配置环境变量
安装完成后,需要告诉系统 CUDA 的可执行程序和库路径:
echo 'export PATH=/usr/local/cuda-13.0/bin${PATH:+:${PATH}}' | sudo tee -a /etc/profile.d/cuda.sh echo 'export LD_LIBRARY_PATH=/usr/local/cuda-13.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' | sudo tee -a /etc/profile.d/cuda.sh source /etc/profile.d/cuda.sh
说明:
这两行命令会在系统启动时自动加载 CUDA 环境。
之后在任意目录输入 nvcc可以识别 CUDA 编译器。
6. 重启系统加载驱动
安装驱动后必须重启,否则 GPU 驱动模块不会加载。
sudo reboot
说明:
重启后系统会自动启用 NVIDIA 驱动,此时桌面图形界面或nvidia-smi 都能检测到 GPU。
7. 验证驱动与 CUDA 工具链
确认安装是否成功:
nvidia-smi
nvcc --version
预期输出类似:
Sat Oct 11 08:00:39 2025 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 580.95.05 Driver Version: 580.95.05 CUDA Version: 13.0 | +-----------------------------------------+------------------------+----------------------+ | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| ... +-----------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=========================================================================================| | No running processes found | +-----------------------------------------------------------------------------------------+ nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2025 NVIDIA Corporation Built on Wed_Aug_20_01:58:59_PM_PDT_2025 Cuda compilation tools, release 13.0, V13.0.88 Build cuda_13.0.r13.0/compiler.36424714_0
说明:
nvidia-smi 确认驱动加载正常;
nvcc 确认 CUDA 编译器版本匹配。
8. 编译并运行 cuda-examples(官方 GPU 测试)
NVIDIA 提供的官方样例非常适合验证 CUDA 功能是否可用。
克隆与编译
git clone https://github.com/NVIDIA/cuda-samples.git cd cuda-samples git checkout v13.0
mkdir build && cd build
cmake ../
make -j$(nproc)
说明:
下载对应版本的示例源代码;
使用 make 编译后,样例会自动放到 bin/x86_64/linux/release 目录。
运行测试
~/cuda/cuda-samples/build/Samples/0_Introduction/vectorAddDrv$ ./vectorAddDrv
预期输出:
Vector Addition (Driver API) > Using CUDA Device [0]: NVIDIA GeForce RTX 4090 > findModulePath found file at <./vectorAdd_kernel64.fatbin> > initCUDA loading module: <./vectorAdd_kernel64.fatbin> Result = PASS
说明:
deviceQuery 检查 GPU 信息;
matrixMul 测矩阵乘性能。
若全部通过,说明驱动和 CUDA 工作正常。
9. 测试结果汇总
测试项目 | 命令 | 预期结果 |
---|---|---|
驱动检测 | nvidia-smi |
+-----------------------------------------------------------------------------------------+ |
CUDA 编译器 | nvcc --version |
nvcc: NVIDIA (R) Cuda compiler driver |
样例编译 | make | 成功无错误 |
核心功能 | deviceQuery |
./build/Samples/1_Utilities/deviceQuery/deviceQuery Starting... CUDA Device Query (Runtime API) version (CUDART static linking) Detected 2 CUDA Capable device(s) ... deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 13.0, CUDA Runtime Version = 13.0, NumDevs = 2 Result = PASS |
cuBLAS 测试 | simpleCUBLAS |
GPU Device 0: "Ada" with compute capability 8.9 simpleCUBLAS test running.. |