# WSL + CUDA + Pytorch ###### tags: `linux` `ml` `python` 套餐:ubuntu22 + CUDA11.7 + cudnn8.7 + torch1.13.0 官方參考資料 (建議優先看): 1. https://docs.nvidia.com/cuda/wsl-user-guide/index.html 2. https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html 3. https://docs.nvidia.com/deeplearning/cudnn/support-matrix/index.html 4. https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html 民間參考資料: * https://hackmd.io/@Kailyn/HkSTXL9xK * https://zhuanlan.zhihu.com/p/350399229 * https://jackfrisht.medium.com/install-nvidia-driver-via-ppa-in-ubuntu-18-04-fc9a8c4658b9 個人安裝 WSL 的筆記:https://hackmd.io/@stb61307/S1ZBa0YEh ## 安裝 GCC WSL 預設不包含 gcc,需要安裝 gcc 後才能安裝 CUDA: ``` sudo apt-get update sudo apt-get install gcc -y ``` 安裝完成後可以透過指令確認版本:`gcc -v` ## 安裝 CUDA Toolkit 到官網自行挑選版本:https://developer.nvidia.com/cuda-toolkit-archive 以 11.7 為例,記得勾選 WSL 版本:  按照官方提供的指令進行安裝,完成後出現的資訊: ``` =========== = Summary = =========== Driver: Not Selected Toolkit: Installed in /usr/local/cuda-11.7/ Please make sure that - PATH includes /usr/local/cuda-11.7/bin - LD_LIBRARY_PATH includes /usr/local/cuda-11.7/lib64, or, add /usr/local/cuda-11.7/lib64 to /etc/ld.so.conf and run ldconfig as root To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.7/bin ***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 515.00 is required for CUDA 11.7 functionality to work. To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file: sudo <CudaInstaller>.run --silent --driver Logfile is /var/log/cuda-installer.log ``` 驗證方法(2擇1): * 直接透過已安裝的檔案進行驗證: ``` cd /usr/local/cuda/bin ./nvcc -V ``` * 修改環境變數: 1. 先開啟系統檔案: ``` sudo vim ~/.bashrc ``` 2. 於底部新增以下內容並儲存: ``` export CUDA_HOME=/usr/local/cuda export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH export PATH=/usr/local/cuda-11.8/bin:$PATH ``` 3. 退出 vim 後讓系統重新讀取設定檔並再次測試讀取 CUDA 版本: ``` source ~/.bashrc nvcc -V ``` 驗證成功可以看到CUDA版本: ``` nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2022 NVIDIA Corporation Built on Tue_May__3_18:49:52_PDT_2022 Cuda compilation tools, release 11.7, V11.7.64 Build cuda_11.7.r11.7/compiler.31294372_0 ``` ## 安裝 cuDNN 需要到官網註冊帳號並下載一些 CUDA 相關檔案,然後複製到先前安裝 CUDA Toolkit 的路徑, 下載網址:https://developer.nvidia.com/rdp/cudnn-download  進入 WSL 開始解壓縮: ``` // 若壓縮檔格式為 tgz tar -zxvf FileName.tar.xz // 若壓縮檔格式為 tar.xz tar Jxvf FileName.tar.xz tar -xvf cudnn-linux-x86_64-8.x.x.x_cudaX.Y-archive.tar.xz ``` 完成後會獲得一個新資料夾:cudnn-linux-XXXXXXX-archive。 後續把解壓縮結果複製到 CUDA 安裝目錄底下的資料夾:lib64、include。 沒有 lib64 的話就放到 lib 。 確認 CUDA 安裝路徑底下的資料夾然後開始複製、調整權限: ``` ls -al /usr/local/cuda-11.7/ sudo cp -P cudnn-linux-x86_64-8.7.0.84_cuda11-archive/lib/libcudnn* /usr/local/cuda/lib64 sudo cp cudnn-linux-x86_64-8.7.0.84_cuda11-archive/include/cudnn*.h /usr/local/cuda/include sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn* ``` ## 驗證結果 可以直接到 github 下載測試專案:https://github.com/li-weihua/cudnn_samples_v8 ``` cd cudnn_samples_v8/mnistCUDNN sudo make clean && make ./mnistCUDNN ``` 測試成功會出現`Test passed!`。 make 過程可能會遇到的錯誤: * g++: No such file or directory ``` sudo apt install g++ ``` * fatal error: FreeImage.h: No such file or directory ``` sudo apt install libfreeimage3 libfreeimage-dev ``` ### 安裝 pytorch 官網預設會提供最新版本的安裝指令:https://pytorch.org/get-started/locally/ 不想使用最新版可以參考:https://pytorch.org/get-started/previous-versions/ 基本上都是一次指定三個套件進行安裝 (torch、torchvision、torchaudio), 若非安裝最新版則建議附加上版本編號,例如: ``` pip install torch==1.13.0+cu117 torchvision==0.14.0+cu117 torchaudio==0.13.0 --extra-index-url https://download.pytorch.org/whl/cu117 ``` 簡易測試: ```python= import torch x = torch.rand(5, 3) print(torch.cuda.is_available()) # True ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up