WSL2 安装 CUDA(Win11)

1.安装WSL的CUDA驱动

驱动下载地址:

https://developer.nvidia.com/cuda/wsl

2.安装WSL2

下载,换源

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


更新一下包

sudo apt update
sudo apt upgrade

3.安装cuda-toolkit

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环境

5.搭建Pytorch环境

创建环境

conda create --name torch python=3.8

激活环境

conda activate torch

安装pytorch
在线方式

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

离线方式
pip安装自己离线下载的torch和torch-version
下载到本地

http://download.pytorch.org/whl/torch_stable.html
pip install "/home/bcm/torch-1.10.0+cu113-cp37-cp37m-linux_x86_64.whl"
pip install "/home/bcm/torchvision-0.11.0+cu113-cp37-cp37m-linux_x86_64.whl"

6.验证是否能使用GPU

>>> import torch
>>> torch.cuda.is_available()

Tree,成功。
在这里插入图片描述

你可能感兴趣的:(Python,WSl,Linux,ubuntu,vim,linux)