WSL搭建CUDA环境

WSL搭建CUDA环境,第1张

WSL搭建CUDA环境

文章目录
  • WSL搭建CUDA环境
    • 打开虚拟机功能
    • 安装cuda-wsl驱动
    • 安装WSL2
    • WSL2中安装CUDA
    • WSL更换源,加速软件下载
    • 安装Miniconda
    • 搭建Pytorch环境验证
    • 彩蛋

打开虚拟机功能

打开控制面板—>程序与功能—>启用或关闭Windows功能,勾选以下功能

  • Hyper-V
  • 适用于Linux的Windows子系统
  • 虚拟机平台

重启

安装cuda-wsl驱动

驱动下载地址:https://developer.nvidia.com/cuda/wsl
根据你自己显卡型号选择对应驱动


下载并安装

安装WSL2

到应用商店安装WSL,我这里选择Ubuntu-18.04进行安装

WSL安装完成后查看WSL版本

wsl -l -v


如果是WSL1,需要升级到WSL2

  1. 下载Linux 内核更新包并安装,下载地址

  2. 将Ubuntu18.04 设置为WSL2

    wsl --set-version Ubuntu-18.04 2
    
  3. 查看wsl版本,看是否变成版本2

    wsl -l -v
    
WSL2中安装CUDA
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub

sudo sh -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'

sudo apt-get update

sudo apt-get install -y cuda-toolkit-11-0

如果缺少依赖报错就安装下面依赖,安装前记得先按下面步骤换源,否则下载速度贼慢

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
WSL更换源,加速软件下载
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
安装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
conda init
搭建Pytorch环境验证

创建环境

conda create --name torch python=3.8

激活环境

conda activate torch

安装pytorch,Pytorch官网地址

conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch


验证cuda环境

conda activate torch

python

import torch
torch.cuda.is_available()


恭喜你,环境搭建成功

彩蛋

WSL2可以直接使用图形界面打开软件,使用VScode打开当前目录

code .

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/589792.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-13
下一篇 2022-04-13

发表评论

登录后才能评论

评论列表(0条)

保存