WSL2 安装 CUDA(Win11)
1.安装WSL的CUDA驱动
驱动下载地址:https://developer.nvidia.com/cuda/wsl
下载完成后直接默认安装就行
2.安装WSL2(推荐使用Ubuntu-18.04版本,其他版本坑多)
如果要卸载以前版本
wsl --list
wsl --unregister Ubuntu-18.04
下载,换源
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo chmod 777 /etc/apt/sources.list
sudo vim /etc/apt/sources.list
在vim的命令模式下按d,可以删除内容
按 i 变成编辑模式,把下面内容复制粘贴进去
先按ESC退回到命令模式,按 :wq 进行保存
阿里源:
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
清华源
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
更新一下包
sudo apt update
sudo apt upgrade
3.安装cuda-toolkit(10.版本用不了)
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/3bf863cc.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
toolkit的版本一定要选择cuda版本对应的
sudo apt-get install -y cuda-toolkit-11-3
添加变量到bashrc中
vim ~/.bashrc
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PAT
source ~/.bashrc
验证是否安装成功
nvcc -V
4.安装Miniconda
# 切换到家目录
cd ~
# 下载miniconda
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# 修改运行权限
chmod 777 Miniconda3-latest-Linux-x86_64.sh
# 运行安装脚本
./Miniconda3-latest-Linux-x86_64.sh
一直按回车,在需要输入yes的时候输入yes
添加环境变量
vim ~/.bashrc
export PATH=/home/你的用户名/miniconda3/bin:$PATH
关闭WSL重新打开就能看见base环境
如果使用的是zsh
vim ~/.zshrc
export PATH=/home/你的用户名/miniconda3/bin:$PATH
cd ~/miniconda3/bin
./conda init zsh
conda换源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes
pip换源
pip install pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
5.搭建Pytorch环境
创建环境
conda create --name torch python=3.8
激活环境
conda activate torch
安装pytorch
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
6.验证是否能使用GPU
import torch
torch.cuda.is_available()
返回True即搭建成功