WSL2安装+深度学习环境配置

参考:搭建Windows Linux子系统(WSL2)CUDA环境
参考:深度学习环境配置 Windows+WSL2

1 安装WSL2

WSL 官方安装指南
以管理员身份打开 PowerShell ,运行代码:

wsl --install

安装后打开Ubuntu(我这边安装的是Ubuntu 20.04.6 LTS),如果出现以下报错
Windows11 WSL 打开Ubuntu 报错 WslRegisterDistribution failed with error: 0x800701bc

使用以下代码可以解决

wsl --update

2 配置深度学习环境

1.1 设置用户名、密码

用户名的命名规则:首字母不能大写,组成是子母和数字

1.2 安装cuda

安装地址:https://developer.nvidia.com/cuda-12-2-0-download-archive?target_os=Linux&target_arch=x86_64&Distribution=WSL-Ubuntu&target_version=2.0&target_type=runfile_local
在这里插入图片描述
页面底下有有对应的安装代码

wget https://developer.download.nvidia.com/compute/cuda/12.2.0/local_installers/cuda_12.2.0_535.54.03_linux.run
sudo sh cuda_12.2.0_535.54.03_linux.run

如果运行sudo sh cuda_12.2.0_535.54.03_linux.run报错在这里插入图片描述
报错: Failed to verify gcc version. See log at /var/log/cuda-installer.log for details.
说明没有安装GCC
参考博客:搭建Windows Linux子系统(WSL2)CUDA环境

sudo apt update && sudo apt upgrade -y
sudo apt install build-essential
# 查看安装的gcc版本
gcc --version

在这里插入图片描述

修改WSL安装路径

显然,此时的wsl默认安装在c盘,随着系统的使用,会占用我们C盘的空间,所以我们将其打包放到其它盘去。
1.查看WSL发行版本
在Windows PowerShell中输入命令:

wsl -l --all -v

2.导出分发版为tar文件到d盘
wsl --export Ubuntu-20.04 d:\wsl-ubuntu20.04.tar(Ubuntu修改成你现在的发行版名称)
3.注销当前分发版
wsl --unregister Ubuntu-20.04(Ubuntu修改成你现在的发行版名称)
4.重新导入并安装WSL在d:\wsl-ubuntu20.04(可以修改成你自己想要的目录)

wsl --import Ubuntu d:\wsl-ubuntu20.04 d:\wsl-ubuntu20.04.tar --version 2

5.设置默认登陆用户为安装时用户名

Ubuntu config --default-user Username

6.删除tar文件(可选)

del d:\wsl-ubuntu20.04.tar

在这里插入图片描述

1.3 安装Anaconda

直接去Anaconda官网安装
https://anaconda.com/download#downloads
在这里插入图片描述
下载,运行。
将下载好的文件拖到
在这里插入图片描述
执行

sh <filename>.sh

一路默认,在某个地方它会提示你是否进行conda init(conda初始化),默认是no,我选择了yes,建议yes。

安装完成后,如果你的shell命令路径前面有一个(base),说明你已经进入了conda环境,如果没有,输入

source ~/.bashrc

推荐:修改镜像源,不然有的时候慢得离谱。命令如下:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/

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/pytorch/

conda config --set show_channel_urls yes

推荐:创建虚拟环境,避免包冲突(我下载的是3.9)

conda create -n <YourVirtualEnvironmentName> python=<PythonVersion, e.g. 3.8>

这个虚拟环境名字不建议太长。进入自己创建的虚拟环境:

conda activate <YourVirtualEnvironmentName>

然后在虚拟环境中安装PyTorch:
https://pytorch.org/get-started/locally/
在这里插入图片描述
我安装的

conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

至此,就此安装完成!试一试:
在这里插入图片描述

  • 19
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值