继 [Windows 11安装Linux子系统WSL2](环境:Windows安装Linux子系统) 之后,接下来我们为`Ubuntu 22.04`配置`CUDA`编程环境。 # Windows安装NVIDIA显卡驱动 这一步很重要,一定要确保Windows正确安装显卡驱动,否则后续步骤总是会出现奇奇怪怪的问题。 Windows安装`NVIDIA`显卡驱动,以`GeForce MX350`显卡为例。 访问`NVIDIA`官网,搜索适合的显卡驱动: 中文:[https://www.nvidia.cn/geforce/drivers/](https://www.nvidia.cn/geforce/drivers/) 英文:[https://www.nvidia.com/Download/index.aspx](https://www.nvidia.com/Download/index.aspx) ![image](../../../ff_internal_upload/img/2024/image-20240710212044958.png) 这里下载最新版驱动:556.12 ![image](../../../ff_internal_upload/img/2024/image-20240710212414688.png) 安装完成后,在`Windows command`中查询`CUDA`信息: ``` C:\Users\yz> nvidia-smi Wed Jul 10 21:25:48 2024 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 556.12 Driver Version: 556.12 CUDA Version: 12.5 | |-----------------------------------------+------------------------+----------------------+ | GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |=========================================+========================+======================| | 0 NVIDIA GeForce MX350 WDDM | 00000000:01:00.0 Off | N/A | | N/A 51C P0 N/A / ERR! | 0MiB / 2048MiB | 2% Default | | | | N/A | +-----------------------------------------+------------------------+----------------------+ +-----------------------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=========================================================================================| | No running processes found | +-----------------------------------------------------------------------------------------+ ``` # Ubuntu安装CUDA Toolkit 注意,这里我们安装的是“CUDA Toolkit”,WSL使用的CUDA驱动实际上是Windows的,所以子系统的Ubuntu不需安装显卡驱动。 访问`NVIDIA`官网,选择合适的安装方式。 [CUDA Toolkit官网](https://developer.nvidia.com/cuda-toolkit-archive) ![image](../../../ff_internal_upload/img/2024/image-20240710213150600.png) 我们选择:`CUDA Toolkit 12.5.0` 然后,选择“Operating System”,“Architecture”,“Distribution”,“Version”,“Installer Type”。 本文推荐使用“Installer Type”使用“runfile (local)”方式。 ![image](../../../ff_internal_upload/img/2024/image-20240710213931814.png) 选择安装方式后,按照页面给出的步骤执行安装步骤。 ![image](../../../ff_internal_upload/img/2024/image-20240710214330223.png) 执行`.run`程序之前需要先安装`gcc`: ``` apt install gcc apt install gcc++ ``` 安装gcc后,执行上述`.run`程序,然后在以下步骤输入“accept”并回车继续。 ![image](../../../ff_internal_upload/img/2024/image-20240710215912772.png) 接下来选择需要安装的组件。这里不用选择“Kernel Object”。然后选择“Install”,并回车继续安装。 ![image](../../../ff_internal_upload/img/2024/image-20240710220140714.png) 安装成功,可以看到以下提示信息: ![image](../../../ff_internal_upload/img/2024/image-20240710220608086.png) 安装成功后,按照提示在用户的`.bashrc`文件中添加环境变量: ```bash export CUDA_HOME=/usr/local/cuda-12.5 export PATH=$PATH:$CUDA_HOME/bin export LD_LIBRARY_PATH=$CUDA_HOME/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} ``` 检查CUDA是否正确安装: ```bash root@FF:~/cuda# nvidia-smi Wed Jul 10 21:49:44 2024 +-----------------------------------------------------------------------------------------+ | NVIDIA-SMI 555.58.02 Driver Version: 556.12 CUDA Version: 12.5 | |-----------------------------------------+------------------------+----------------------+ | 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. | |=========================================+========================+======================| segmentation fault root@FF:~/cuda# nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2024 NVIDIA Corporation Built on Wed_Apr_17_19:19:55_PDT_2024 Cuda compilation tools, release 12.5, V12.5.40 Build cuda_12.5.r12.5/compiler.34177558_0 ``` 很不幸!上面`nvidia-smi`命令执行报错了,仅输出了一部分信息,报`segmentation fault`错误。有关该问题的解决方法见: [环境:CUDA开发环境问题汇总](环境:CUDA开发环境问题汇总#nvidia-smi报segmentation fault的错误) # 参考 - [全网最详细搭建Win10+WSL2+Ubuntu-22.04LTS+CUDA+Xfce4+noVNC个人工作站](https://blog.csdn.net/weixin_47145054/article/details/129865298)