win11安装WSL2详细教程

一、WSL2安装

使用图形界面

  • 打开 控制面版
  • 访问 程序和功能 子菜单 打开或关闭Windows功能
  • 选择“适用于Linux的Windows子系统”与 “虚拟机平台”与“Hyper-V"
  • 点击“确定”
  • 重启
    在这里插入图片描述

搜索power shell ,右键“以管理员方式运行”,执行下面的两行命令

wsl --update
#将 WSL 默认版本设置为 WSL 2
wsl --set-default-version 2

在这里插入图片描述

二、下载Ubuntu系统

这里直接下载ubuntu镜像文件
https://learn.microsoft.com/en-us/windows/wsl/install-manual#downloading-distributions

在这里插入图片描述
用迅雷下载,速度飞快

下载完成后,会得到文件Ubuntuxxxx.AppxBundle
xxxxx根据由你下载的版本决定

将文件名,重命名为Ubuntuxxxx.zip,不用担心文件会损坏,改成zip就是为了方便解压而已。

解压之后,得到文件夹Ubuntuxxx

进入文件夹,找到文件Ubuntu_xxx_x64.appx,重命名为:Ubuntu_xxxx_x64.zip,然后解压。

得到文件夹Ubuntu_xxxx_x64

然后这个文件夹Ubuntu_xxxx_x64中有:
在这里插入图片描述
双击运行,让你输入用户名和密码

在这里插入图片描述

三、cuda安装

cuda版本安装cuda11.7(和windows系统上安装一样的版本)

https://developer.nvidia.com/cuda-toolkit-archive?login=from_csdn

在这里插入图片描述
在这里插入图片描述
然后它会生成安装命令:
在这里插入图片描述

这些命令需要在ubuntu命令行中运行,搜索power shell ,输入命令

wsl

即可进入ubuntu命令行

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
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-7-local_11.7.0-1_amd64.deb
sudo cp /var/cuda-repo-wsl-ubuntu-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

在执行完上述命令后,执行下面的系统环境配置:

echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

验证是否安装成功:

nvcc -V

显示下面的信息,表示成功
在这里插入图片描述

安装好的cuda在路径\wsl.localhost\Ubuntu\usr\local中:
在这里插入图片描述

四、cudnn安装

https://developer.nvidia.com/rdp/cudnn-archive

cuda11.7对应的cudnn版本是8.8.1.3,因此下载:
在这里插入图片描述
在这里插入图片描述
下载后的tar包在win系统中

在\home\syh新建cudnn文件夹:
将下载后的tar包通过复制粘贴到cudnn文件夹中:

然后打开ubuntu命令行,执行下面的命令:

tar -xvf cudnn-linux-x86_64-8.8.1.3_cuda11-archive.tar.xz
sudo cp cudnn-*-archive/include/cudnn*.h /usr/local/cuda/include
sudo cp -P cudnn-*-archive/lib/libcudnn* /usr/local/cuda/lib64 
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

验证是否安装成功,输入如下命令,显示如下图即安装成功

cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2

在这里插入图片描述

安装Anaconda

https://repo.anaconda.com/archive/
在这里插入图片描述
如果这样直接下载会下载到win中,我们直接在ubuntu的命令行中执行下面的命令:

wget -P /tmp https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh

会下载到tmp文件夹中

然后安装Anaconda3:

bash /tmp/Anaconda3-2023.03-1-Linux-x86_64.sh

按回车阅读用户协议

在这里插入图片描述

输入yes。

在这里插入图片描述输入安装位置,默认为/home/用户名/anaconda3。

在这里插入图片描述
输入yes,运行conda初始化。
在这里插入图片描述

重新打开一个conda命令行,前面会自动进入base环境,证明安装成功:
在这里插入图片描述

五、Ubuntu换源

步骤一、apt换源
打开清华镜像官网
https://mirrors.tuna.tsinghua.edu.cn/

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
使用如下命令进入配置文件:

sudo gedit /etc/apt/sources.list

将里面的文本替换为下面的代码,然后保存

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-backports main restricted universe multiverse

# 以下安全更新软件源包含了官方源与镜像站配置,如有需要可自行修改注释切换
deb http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ noble-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse
# # deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ noble-proposed main restricted universe multiverse

ps:清华源镜像会不定期更新,所以最好按作者所说步骤去复制此镜像源

然后更新源:

sudo apt update # apt更新

步骤二、conda换源
使用如下代码即可换源:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64/
conda config --set show_channel_urls yes

步骤三、pip换源
创建pip.conf文件

cd ~/.pip

如果提示目录不存在的话,我们要自行创建一个,再进入目录

mkdir ~/.pip
cd ~/.pip

在.pip目录下创建一个pip.conf文件

touch pip.conf

编辑pip.conf文件

sudo gedit ~/.pip/pip.conf

打开pip.conf文件窗口,将以下内容复制到文件中:

[global] 
index-url = http://pypi.douban.com/simple 
[install] 
trusted-host=pypi.douban.com

ctrl+s保存退出

六、创建虚拟环境,安装pytorch和torchvision

创建虚拟环境:

conda create -n your_env_name python=x.x
例如:conda create -n ghrp python=3.8

查看虚拟环境:

conda env list

切换虚拟环境:

conda activate your_env_name

在切换到你虚拟环境后,安装Pytorch,对应版本下载命令在官网中查找
https://pytorch.org/get-started/previous-versions/

我安装这个版本的:
在这里插入图片描述

pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117

安装完成之后可以在命令行中输入python,依次输出以下命令,输出如下图所示则表明安装成功:

import torch
torch.__version__
torch.cuda.is_available()
torch.version.cuda

在这里插入图片描述

七、Pycharm远程WSL2

我的配置是代码和**解释器(conda环境)**都在WSL中

点击 File > Open
在这里插入图片描述

选择位于 WSL 里面的项目
在这里插入图片描述

点击Pycharm 右下角的 Python Interpreter selector,选择 On WSL…
在这里插入图片描述
点击 Next
在这里插入图片描述
使用conda环境
在这里插入图片描述

选择创建的conda虚拟环境
在这里插入图片描述

八、vscode远程wsl

点击左下方图标【Open a Remote Windows】
在这里插入图片描述
弹出【远程连接】浮窗 ,选择【Connect to WSL】,等待几分钟即可连接到wsl的ubuntu系统
在这里插入图片描述
点击左下方的WSL的ubuntu子系统,弹出【远程连接】浮窗,点击【Close Remote Connection】选项就可以退出WSL的ubuntu子系统。
在这里插入图片描述
直接在设置(ctrl + shift + p)里面搜索,点击“Python:Select Interpreter”选项,然后选择自己对应的虚拟环境即可

### Windows 11 WSL 安装指南 #### 启用 WSL 功能 为了在 Windows 11安装 WSL,首先需要启用必要的功能。这可以通过 PowerShell 来完成: ```powershell dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` 上述命令会启动两个重要的组件:Windows Subsystem for Linux (WSL) 和 Virtual Machine Platform[^2]。 #### 更新到 WSL 2 确保系统已经更新至最新的 WSL 2 内核版本。通过以下命令下载并安装最新内核包: ```powershell wsl --update ``` 此命令将自动获取适用于当前系统的最佳 WSL 2 内核版本。 #### 设置默认版本为 WSL 2 为了让新安装的任何 Linux 发行版都使用 WSL 2 而不是旧版本,默认设置需调整如下: ```powershell wsl --set-default-version 2 ``` 这条指令设定了之后所有的 Linux 发行版都将基于更高效的 WSL 2 技术运行。 #### 安装 Ubuntu 或其他 Linux 发行版 微软商店提供了多种官方支持的 Linux 发行版供选择。对于希望体验 GNOME 桌面环境的情况,推荐安装带有图形界面支持的 Ubuntu 版本。可通过 Microsoft Store 应用程序搜索 "Ubuntu" 并点击安装按钮来轻松实现这一点[^1]。 #### 创建 .wslconfig 文件优化性能 为了进一步提升用户体验,在用户的个人资料目录下创建 `.wslconfig` 文件能够帮助自定义一些全局参数。例如,可以指定内存分配量、CPU 使用率等选项以适应不同的应用场景需求。具体做法是在 `%UserProfile%` 目录中新建名为`.wslconfig` 的纯文本文件,并编辑其内容如下所示[^4]: ```ini [wsl2] memory=8GB # 分配给 WSL 2 VM 的最大 RAM 数量 processors=4 # 可由 WSL 2 VM 使用的最大处理器核心数 swap=0 # 不使用交换空间;可根据实际硬件情况适当增加大小 ``` 保存更改后重启计算机使新的配置生效。 #### 测试安装成果 最后一步是验证整个过程是否成功。打开命令提示符或 PowerShell 输入 `wsl` 命令进入刚刚安装好的 Linux 环境。如果一切正常,则意味着现在已经可以在 Windows 11 中流畅地运行 Linux 工具和服务了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值