【WSL】window10 安装WSL2配置conda环境及深度学习环境搭建

WSL安装CentOS7及深度学习环境配置



前言

写这篇文章之前自己安装过两次环境,可能每次都会产生碰到同样的问题,而每次上网查询答案总是需要多个贴共同协作,为了搭建适合自己的虚拟环境保证每一步的正确性,按顺序记录一下


一、安装WSL前置条件及步骤

写内容前先放两个帖子
链接:
WSL2的安装详细过程
基于Windwos11的WSL安装CentOS
里面包含以下要点:

1.1 Windows 10(要求

(已更新到版本 2004 的内部版本 19041 或更高版本,win11无此需求)最好是22H2版本以上,内部版本19045以上。如果未达到请按上述链接升级
. 在这里插入图片描述

1.2.检查是否满足潜在条件

2.1控制面板 -> 程序 -> 程序和功能 -> 启用或关闭Windows功能->适用于window的linux子系统
2.2 任务管理器->性能->CPU->虚拟化(虚拟化未开启需要进入BIOS打开虚拟化)
2.3 BIOS开启虚拟化的方法

二、安装WSL及CentOS7镜像

2.1启动powershell分别输入以下命令

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
#重启电脑
#更改为wsl2
wsl --set-default-version 2
安装CentOS

这里有一个地址是github上的Centwsl提供的镜像,我安装的是CentOS7,但不是这里来的,可以自己尝试一下

https://github.com/wsldl-pg/CentWSL

下载安装CentOS7参照以下的链接
基于Windwos11的WSL安装CentOS

在安装wsl中遇到各种报错可以参考这个帖子:
关于几种报错的解决办法

2.2 如何查看存在的子系统,以及重置子系统

当遇到问题需要重置系统时也很简单
powershell打开

wsl.exe --list --all   #查看存在子系统
wsl.exe --unregister CentOS7

选择默认的linux版本
wslconfig /setdefault  linuxname
#切换版本
wsl --set-version linuxname
wsl --list --verbose

#关闭某一个发行版
wsl -t linuxname

然后按照上述链接中重新安装一遍CentOS7即可

可能需要安装的必要的东西
更新系统中已有的软件包 yum -y update
安装gcc、gcc+、wget等 yum -y install gcc gcc-c++ make wget
安装ssh相关 yum -y install openssh-clients openssh-server
安装vim编辑器 yum -y install vim
安装压缩解压缩工具 yum -y install tar
安装网络工具 yum -y install net-tools
字符终端处理库 yum -y install ncurses

2.3 安装环境设置普通用户

CentOS7默认是root用户,设置普通用户及密码是有必要的
#这部分还没做,暂时用root用户试试
进入后输入重置密码

[root@DESKTOP-JUJ5EQK ~]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password fails the dictionary check - it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.

接下来设置了window下VScode配置WSL(liunx环境下)
配置CentOS7的SSH

2.4 ssh 连接

yum list installed | grep openssh-server
#修改ssh_config文件
vi /etc/ssh/sshd_config

找到修改成以下样子
Port 22
#AddressFamily any
ListenAddress 0.0.0.0
ListenAddress ::

PermitRootLogin yes

SSH启动参考这个链接启动SSH

[root@localhost ~]$ ps -e | grep sshd  #检查是否已开启ssh
[wangl@DESKTOP-JUJ5EQK:]$netstat -ta |grep ssh #检查是否已开启ssh   ubuntu系统下
[root@localhost ~]$systemctl enable sshd.service #设置ssh service自启动
[root@localhost ~]ifconfig    #查看linux系统的ip地址

VScode与wsl互联配置参照这个
vscode(windows) 如何连接虚拟机中的linux

Host Swei
    HostName 192.168.86.10   #linux的ip地址
    User swei  #linux的用户名
    Port 22    #端口
    IdentityFile "C:\Users\SweiJ\.ssh\id_rsa"   #id_rsa的路径

连接之后的情况
在这里插入图片描述


三、Conda配置与pytorch cuda环境安装

3.1 CentOS7 miniconda安装

wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.7.12.1-Linux-x86_64.sh
chmod 777 Miniconda3-4.7.12.1-Linux-x86_64.sh #给执行权限
bash Miniconda3-latest-Linux-x86_64.sh

安装屏幕上的提示安装,完成!!!

(base) [root@DESKTOP-JUJ5EQK ~]#

3.2配置环境

创建新环境,添加新镜像

conda create -n mllab python=3.10.6

vi ~/.pip/pip.conf   #如果没有就创造路径创造文件

#填写内容
[global]
timeout=6000
index-url = http://pypi.douban.com/simple/
trusted-host=pypi.douban.com/simple 
vi ~/.condarc
#如果没有就创造文件
#填写内容
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
  - defaults
show_channel_urls: true
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

3.3 CUDA、cudnn安装

首先你要有一块英伟达显卡,其次使用 NVIDIA GeForce Experience安装最新的驱动,如果你本身已经是最新则不需要重新安装

右键->Nvidia 控制面板->系统信息->驱动程序版本
在这里插入图片描述
这个链接查看CUDA匹配的cuda toolkit

安装CUDA toolkit
网站提示的安装指令安装


第一次安装的电脑可能会显示 rpm not found
输入命令 sudo apt-get install rpm(此时需要这台电脑能够上网),等待自行下载

wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-rhel7-11-8-local-11.8.0_520.61.05-1.x86_64.rpm
sudo rpm -i cuda-repo-rhel7-11-8-local-11.8.0_520.61.05-1.x86_64.rpm
sudo yum clean all
sudo yum -y install nvidia-driver-latest-dkms
sudo yum -y install cuda

#安装遇到了报错,
https://www.nvidia.com/Download/index.aspx?lang=en-us
发现wsl中可能需要其他的nvdia的支持,此链接安装nvdia的驱动

如何在wsl安装CUDA的doc

#根据doc提供的信息,wsl暂时只有wsl-Ubuntu版本,CentOS暂时搁置,下面使用Ubuntu20.04进行部署

部署ubuntu20.04并配置

WSL GPU driver安装参考

有了前面的铺垫后面就变得轻车熟路,也没有遇到什么报错
在这里插入图片描述
ubuntu下的ssh配置参考2.4节
安装完成CUDA和cudnn也配置完成
接下来激动人心的时刻!!

3.4 pytorch 安装

根据环境选择安装pytorch的版本
pytorch 1.13.0 conda CUDA 11.6 linux python
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia

安装依赖库
python -m pip install requirments.txt

3.5 pytorch 测试

cuda版本比非cuda版本大约加速10倍左右,具体内容后续附上

import torch
from torch import nn  # 常用网络
from torch import optim  # 优化工具包
import torchvision  # 视觉数据集
from matplotlib import pyplot as plt
import time

timestart=time.time()
if torch.cuda.is_available():
    device='cuda'
else:
    device='cpu'

## 加载数据
batch_size=512
train_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data',train=True,download=True,
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize((0.1307,), (0.3081,))  # 做一个标准化
                                   ])),
    batch_size=batch_size,shuffle=True)
test_loader = torch.utils.data.DataLoader(
    torchvision.datasets.MNIST('mnist_data/',train=False,download=True,
                               transform=torchvision.transforms.Compose([
                                   torchvision.transforms.ToTensor(),
                                   torchvision.transforms.Normalize((0.1307,), (0.3081,))
                                   ])),
    batch_size=batch_size,shuffle=True)
x,y=next(iter(train_loader))
x=x.to(device)
y=y.to(device)
print(x.shape,y.shape,x.min(),x.max())


relu = nn.ReLU()  # 如果使用torch.sigmoid作为激活函数的话正确率只有60%
# 创建网络
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()

        # xw+b  这里的256,64使我们人根据自己的感觉指定的
        self.fc1 = nn.Linear(28*28,256)
        self.fc2 = nn.Linear(256,64)
        self.fc3 = nn.Linear(64,10)

    def forward(self,x):
        # 因为找不到relu函数,就换成了激活函数
        # x:[b,1,28,28]
        # h1 = relu(xw1+b1)
        x = relu(self.fc1(x))
        # h2 = relu(h1w2+b2)
        x = relu(self.fc2(x))
        # h3 = h2*w3+b3
        x = self.fc3(x)

        return x

# 因为找不到自带的one_hot函数,就手写了一个
def one_hot(label, depth=10):
    out = torch.zeros(label.size(0), depth)
    idx = torch.LongTensor(label).view(-1, 1)
    out.scatter_(dim=1, index=idx, value=1)
    return out

## 训练模型
net = Net().to(device)
# 返回[w1,b1,w2,b2,w3,b3]  对象,lr是学习过程
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)

train_loss = []
mes_loss = nn.MSELoss().to(device)
for epoch in range(10):
    for batch_idx, (x, y) in enumerate(train_loader):
        # x:[b,1,28,28],y:[512]
        # [b,1,28,28]  =>  [b,784]
        x = x.view(x.size(0), 28 * 28)
        x=x.to(device)
        # =>[b,10]
        out = net(x)
        # [b,10]
        y_onehot = one_hot(y)
        y_onehot=y_onehot.to(device)
        # loss = mse(out,y_onehot)
        loss = mes_loss(out, y_onehot)

        # 清零梯度
        optimizer.zero_grad()
        # 计算梯度
        loss.backward()
        # w' = w -lr*grad
        # 更新梯度,得到新的[w1,b1,w2,b2,w3,b3]
        optimizer.step()

        train_loss.append(loss.item())
      #  if batch_idx % 10 == 0:
      #      print(epoch, batch_idx, loss.item())
# plot_curve(train_loss)
# 到现在得到了[w1,b1,w2,b2,w3,b3]

## 准确度测试
total_correct = 0
for x,y in test_loader:
    x = x.view(x.size(0),28*28)
    x=x.to(device)
    y=y.to(device)
    out = net(x)
    # out : [b,10]  =>  pred: [b]
    pred = out.argmax(dim = 1)
    correct = pred.eq(y).sum().float().item()  # .float之后还是tensor类型,要拿到数据需要使用item()
    total_correct += correct
total_num = len(test_loader.dataset)
acc = total_correct/total_num
print('准确率acc:',acc)
timeend=time.time()
usedtime=timeend-timestart
print('打印任务耗时%.2f s'%usedtime)
torch.Size([512, 1, 28, 28]) torch.Size([512]) tensor(-0.4242, device='cuda:0') tensor(2.8215, device='cuda:0')
准确率acc: 0.9316
#有GPU情况下的耗时
打印任务耗时68.10 s

3.6 pytorch-geometric 安装及测试

abstract

PyG (PyTorch Geometric) is a library built upon PyTorch to easily write and train Graph Neural Networks (GNNs) for a wide range of applications related to structured data

安装方法
1.安装依赖库
wheel依赖库
安装依赖库需要选择不同的版本,torch版本+cudann版本以及python版本
torch_cluster, torch_scatter, torch_sparse, torch_spline_conv

$ conda list torch
pytorch                   1.13.0          py3.10_cuda11.7_cudnn8.5.0_0    pytorch
pytorch-cuda              11.7                 h67b0de4_0    pytorch

$ python
Python 3.10.6 (main, Oct 24 2022, 16:07:47) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

安装完成依赖库后直接使用 pip install pytorch-geometric !!
安装完成一个小测试:

import torch
from torch_geometric.data import Data

edge_index = torch.tensor([[0, 1, 1, 2],
                               [1, 0, 2, 1]], dtype=torch.long)
x = torch.tensor([[-1], [0], [1]], dtype=torch.float)

data = Data(x=x, edge_index=edge_index)
print(data)

>>>Data(x=[3, 1], edge_index=[2, 4])

四、遇到的问题与解决方法

4.1 长时间开启wsl导致window内存占用过高

查看发现window进程中vmmen的内存占用过高,以下链接提供了三个解决办法:解决办法
方法1:wsl --shutdown
关闭wsl后重启
方法2:在window中写.wslconfig文本
方法3:定时释放内存的任务

4.2 每次wsl2开启虚拟机IP地址变动

windows中使用openSSH调用并不是很友善,不要使用visual studio code,建议使用git或者其他ssh进行ssh链接


总结

以上就是wsl安装及配置conda深度学习环境的内容,

<think>嗯,用户想在WSL配置深度学习开发环境,并且使用VSCode。首先,我得理清楚整个流程的步骤。WSLWindows的子系统,所以用户可能用的是Windows系统,但想用Linux环境来开发深度学习项目。VSCode作为编辑器,确实是个不错的选择,因为它有很好的WSL集成。 首先,用户可能需要先安装配置WSL。我记得官方文档推荐用WSL2,因为它性能更好,特别是对文件系统的支持。可能需要确认用户是否已经启用了WSL功能,如果没有的话,可能需要通过PowerShell命令开启,然后安装Ubuntu之类的发行版。 接下来是安装显卡驱动。因为深度学习通常需要GPU加速,特别是NVIDIA的显卡。用户需要在Windows上安装NVIDIA的驱动,然后在WSL安装CUDA工具包。不过现在NVIDIA对WSL2的支持应该更好了,可能不需要额外安装驱动,但CUDA和cuDNN还是需要的。需要提醒用户检查驱动版本是否兼容他们的CUDA版本。 然后是安装Anaconda或者Miniconda,用来管理Python环境。用户可能需要创建一个新的conda环境安装PyTorch或者TensorFlow之类的框架。这里可能需要指导用户如何根据CUDA版本来选择正确的安装命令,比如使用conda或者pip安装。 接下来是配置VSCode。需要安装Remote - WSL扩展,这样可以在VSCode里直接连接到WSL环境,编辑和运行代码。用户可能需要设置Python解释器路径,指向conda环境里的Python。另外,安装必要的插件,比如Python、Pylance、Jupyter等,以便于开发和调试。 可能还需要配置CUDA和cuDNN的环境变量,确保深度学习框架能正确识别GPU。不过现在很多框架像PyTorch已经简化了这个过程,可能只需要正确安装CUDA版本的包就可以了。需要提醒用户验证GPU是否被框架识别,比如在PyTorch里运行torch.cuda.is_available()。 此外,数据集的存放位置可能需要注意,WSL的文件系统访问Windows的文件可能会有性能问题,所以建议用户将数据集放在WSL的文件系统中,比如/home目录下,或者挂载到特定位置。或者使用符号链接。 还有可能遇到的一些问题,比如权限问题、路径问题,或者GPU驱动不兼容。可能需要指导用户如何排查这些问题,比如检查nvidia-smi在WSL里是否可用,查看日志等。 最后,测试整个环境,运行一个简单的深度学习模型,确认GPU加速是否生效。可能需要提供一个简单的代码示例,比如用PyTorch训练一个MNIST模型,看看是否能正常使用CUDA。 总结一下步骤大概是:安装WSL2 -> 安装NVIDIA驱动和CUDA -> 安装conda -> 创建环境安装框架 -> 配置VSCode远程连接 -> 验证GPU可用性 -> 处理数据集路径 -> 测试运行模型。过程中需要确保每个步骤都正确,特别是驱动和CUDA版本的兼容性,这可能是最容易出问题的地方。</think>以下是在WSLWindows Subsystem for Linux)中配置深度学习开发环境的分步指南,结合VS Code实现高效开发: --- ### 一、系统基础配置 #### 1. 安装WSL 2 ```powershell # Windows PowerShell中执行 wsl --install -d Ubuntu-22.04 wsl --set-default-version 2 ``` #### 2. 配置NVIDIA驱动 - 在Windows中安装[NVIDIA驱动](https://www.nvidia.com/Download/index.aspx) - 验证WSL内GPU识别: ```bash nvidia-smi # 应显示GPU信息 ``` #### 3. 安装CUDA Toolkit ```bash wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600 sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/3bf863cc.pub sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/ /" sudo apt-get install cuda-12-2 ``` --- ### 二、Python环境配置 #### 1. 安装Miniconda ```bash wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh bash Miniconda3-latest-Linux-x86_64.sh ``` #### 2. 创建深度学习环境 ```bash conda create -n dl python=3.10 conda activate dl pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 ``` --- ### 三、VS Code集成配置 #### 1. 安装必要扩展 - Remote - WSL - Python - Jupyter - Pylance #### 2. 配置WSL远程开发 1. 在VS Code中按`F1` → 输入`Remote-WSL: New Window` 2. 打开WSL中的项目目录 3. 选择Python解释器路径(`~/miniconda3/envs/dl/bin/python`) --- ### 四、环境验证 #### 1. GPU可用性测试 ```python import torch print(torch.cuda.is_available()) # 应输出True print(torch.cuda.get_device_name(0)) # 显示GPU型号 ``` #### 2. 性能基准测试 ```bash python -c "import torch; print(torch.randn(1000,1000).cuda() @ torch.randn(1000,1000).cuda())" ``` --- ### 五、优化建议 1. **文件路径优化**:将数据集存储在WSL文件系统内(`/home/username/data`) 2. **内存管理**:在`/etc/wsl.conf`中添加: ``` [automount] options = "metadata" ``` 3. **多环境管理**:使用`conda env export > environment.yml`保存环境配置 --- ### 六、故障排查 1. **CUDA不可用**: - 检查驱动版本与CUDA版本匹配 - 验证`/usr/local/cuda`软链接是否正确 2. **IO性能问题**: - 避免直接访问Windows文件系统 - 使用`rsync`复制数据到WSL内部 --- 通过以上步骤,您将获得一个完整的GPU加速深度学习开发环境。此方案支持PyTorch/TensorFlow等主流框架,结合VS Code的远程开发功能,可实现接近原生Linux环境的开发体验。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangxiancao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值