我用上了最好的Linux发行版!

缘起

wsl在大一的时候一定是入不了我的法眼的,因为在当时,我秉持着Windows yyds的心态,并无使用Linux的需求与需要

wsl在大二的时候,我开始接触到了Linux(服务器,nas,工控机),但是我并没有在自己的电脑上安装wsl,而是在服务器上安装了Linux,又给电脑装了双系统,Linux充斥着玩乐的气息

在今年,终于有需要跑的项目代码在Linux上环境更适合,Linux系统走向了学术,变得不可或缺。

今年小学期的程序设计实验,nlp和cv最后的大作业,都需要Linux开发环境,同时联系的老师布置的任务也需要更强大的算力。好巧不巧该说不说,老师给的服务器居然是windows的,我不得不在自己的电脑上搭建Linux环境(鉴于老师的电脑我没法装双系统,也没法装虚拟机–众所周知vmware等虚拟机不能很好的连显卡)

在这样的大背景下,wsl又进入了我的眼帘。

准备步骤

根据以下链接,确认你的电脑能够启用wsl功能

windows-10-tip-find-out-if-your-pc-can-run-hyper-v/

重点关注以下部分

type msinfo32 in the search box to the right of the Start button
Scroll to the very end and look for the four items that begin with Hyper-V. If you see a Yes next to each one

确认你的显卡驱动是可用的版本

详见cuda官方文档

nvidia-compute-software-support-on-wsl-2

step-1-install-nvidia-driver-for-gpu-support
中特别强调This is the only driver you need to install. Do not install any Linux display driver in WSL.
其中还提到
From this point you should be able to run any existing Linux application which requires CUDA. Do not install any driver within the WSL environment. For building a CUDA application, you will need CUDA Toolkit. Read the next section for further information.

这里的这”step one”是指windows系统安装成功正确驱动 (稍后会讲我的安装实践

如果要用Linux构建cuda程序,才需要在wsl中安装toolkit

因此不要轻易在wsl中运行任何cuda安装的相关命令(官方说很容易覆盖掉正确的驱动)

安装过程流水账

1
wsl --install --distribution Ubuntu-20.04

这一步我应该是没开命令行代理,同时是在管理员的cmd中安装的

然后运行完他说重启之前不会做任何改动

然后我发现
wsl -l -v也不起作用,
重启之后也不起作用

报错如下:
适用于 Linux 的 Windows 子系统没有已安装的分发。
使用‘wsl.exe –list –online’列出可用的分发
和 ‘wsl.exe –install ’进行安装。
也可以通过访问 Microsoft Store 安装分发:
https://aka.ms/wslstore
Error code: Wsl/Service/CreateInstance/GetDefaultDistro/WSL_E_DEFAULT_DISTRO_NOT_FOUND

以为没装上,就重新执行了
$wsl.exe –install –web-download Ubuntu-20.04$

结果显示:

Ubuntu 20.04 LTS 已安装。
正在启动 Ubuntu 20.04 LTS...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: mayu
New password:
Retype new password:
passwd: password updated successfully

安装操作误打误撞成功完成。

Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04.6 LTS (GNU/Linux 5.15.133.1-microsoft-standard-WSL2 x86_64)

* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage

System information as of Sat Dec 23 00:32:12 CST 2023

System load:  0.81                Processes:             43
Usage of /:   0.1% of 1006.85GB   Users logged in:       0
Memory usage: 0%                  IPv4 address for eth0: 172.30.137.32
Swap usage:   0%


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update


This message is shown once a day. To disable it please create the
/home/mayu/.hushlogin file.
mayu@DESKTOP-68DFFP1:~$ ls

我应该是跳过了正确安装的第一次正常启动的过程

正常的第一次启动应该是这样的:使用 WSL 安装 Linux 发行版的过程完成后,使用“开始”菜单打开该发行版(默认情况下为 Ubuntu)。 系统将要求你为 Linux 发行版创建“用户名”和“密码”。参看

经过了上述步骤之后,你在新的cmd中应该会有以下效果

1
2
3
C:\Windows\system32>wsl -l -v
NAME STATE VERSION
* Ubuntu-20.04 Running 2

然后在wsl中运行nvidia-smi可以直接显示显卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
mayu@DESKTOP-68DFFP1:~$ nvidia-smi
Sat Dec 23 00:59:44 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.54.04 Driver Version: 536.23 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 2080 Ti On | 00000000:73:00.0 On | N/A |
| 17% 34C P8 27W / 250W | 446MiB / 22528MiB | 4% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 23 G /Xwayland N/A |
+---------------------------------------------------------------------------------------+

截止目前,我没有在wsl中安装任何cuda相关驱动。因此根据我的验证,网上没缘由安装cuda的博客全都在扯淡。

当然如果需要nvcc(使用toolkit编译cuda程序),则需要安装cuda,但是是特别版的。

(如有需要)安装cuda

我是在配置一个项目的环境的时候遇到了如下的报错:

1
2
3
4
5
6
7
8
9
10
11
× python setup.py egg_info did not run successfully.
exit code: 1
╰─> [13 lines of output]
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "<pip-setuptools-caller>", line 34, in <module>
File "/tmp/pip-install-2fg0sy0m/flash-attn_c0c61b96808f437eab0096f319bdd46e/setup.py", line 105, in <module>
raise_if_cuda_home_none("flash_attn")
File "/tmp/pip-install-2fg0sy0m/flash-attn_c0c61b96808f437eab0096f319bdd46e/setup.py", line 55, in raise_if_cuda_home_none
raise RuntimeError(
RuntimeError: flash_attn was requested, but nvcc was not found. Are you sure your environment has nvcc available? If you're installing within a container from https://hub.docker.com/r/pytorch/pytorch, only images whose names contain 'devel' will provide nvcc.

可以看到,它尝试使用nvcc编译cuda程序,但是nvcc并没有找到。

遇到了不得不安装工具包的情况
还是跟着官网来cuda官方的wsl安装教程

1
2
sudo apt-key del 7fa2af80

从历史archive里找到对应版本的cuda,这里有几点注意事项,首先cuda版本不得高于你的显卡驱动支持的版本,其次,cuda的版本要和你的环境中安装的pytorch的cuda版本对应。注意要选择wsl版本的cuda,不要选择linux版本的cuda。
历史archive链接点击这里

以下是我的踩坑记录,可以不看(错在了版本不匹配,以及官方的安装脚本有小问题)。
wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-keyring_1.0-1_all.deb
sudo dpkg -i cuda-keyring_1.0-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda

我的驱动支持到12.2,所以这一步我选了12.1版本,在线安装

但是不知道怎么回事安装上的nvcc版本是12.3

nvcc -V结果如下:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Wed_Nov_22_10:17:15_PST_2023
Cuda compilation tools, release 12.3, V12.3.107
Build cuda_12.3.r12.3/compiler.33567101_0

不过看起来能正常运行(编译flash-attn)不知道这个版本高低有没有影响,先用着吧

实际上不能正常运行
The detected CUDA version (12.3) mismatches the version that was used to compile
    PyTorch (11.3). Please make sure to use the same CUDA versions.

不得不卸载旧的cuda

卸载cuda

安装新的,要和torch的cuda版本对应

前几句脚本就是官方的命令,只不过在最后一句安装的时候,需要指定安装的版本

1
2
3
4
5
6
7
8
9
10
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.3.0/local_installers/cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-3-local_11.3.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-3-local/7fa2af80.pub
sudo apt-get update
# 官方的错误命令,执行了就会安装最新版本
#sudo apt-get -y install cuda #(不管用,版本错误的罪魁祸首)
#正确指令如下:
sudo apt-get install cuda-toolkit-11-3
重要经验教训

-y不能随便运行

如果我没有随意执行-y,那么可能在一开始安装的时候就会发现版本不对,然后就不会有后面的一系列问题了

最后参考这个链接,在终端中激活nvcc

proxy部分(直接用win的代理)

https://zhuanlan.zhihu.com/p/640242841
按照这个配置客户端
https://zhuanlan.zhihu.com/p/153124468
按照这个配置代理,记得放开防火墙,我用的是xray内核,所以选的xray

网络的官方说明:https://learn.microsoft.com/zh-cn/windows/wsl/networking

里面提到了自动代理功能,todo

配置之前

1
2
3
4
5
6
mayu@DESKTOP-68DFFP1:~$ curl google.com
curl: (56) Recv failure: Connection reset by peer
mayu@DESKTOP-68DFFP1:~$ curl baidu.com
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>

配置之后

1
2
3
4
5
6
7
8
9
10
export https_proxy="http://172.30.128.1:10809"
export http_proxy="http://172.30.128.1:10809"
mayu@DESKTOP-68DFFP1:~$ export https_proxy="http://172.30.128.1:10809"
mayu@DESKTOP-68DFFP1:~$ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

todo写bashrc云云

访问win文件

在/mnt/e等
如果想从 WSL 发行版命令行访问 Windows 文件目录,而不是使用 C:\Users\username,则需使用 /mnt/c/Users/username 访问该目录,因为 Linux 发行版将 Windows 文件系统视为已装载的驱动器。

打开win中的vscode:

1
code .

安装anaconda和torch

更新软件源

正常按照anaconda安装
https://zhuanlan.zhihu.com/p/166102942
这个写的很好

新建环境
conda create -n name python=3.9
然后直接pip install 对应版本的torch
pip install torch==1.10.0+cu111 torchvision==0.11.0+cu111 torchaudio==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html
网速感人(终端甚至没挂代理,安装anaconda重启后忘记配了)
更正,只有pytorch速度够,剩下的就几k每秒
挂上代理就快了

多显卡

网上说还不支持多显卡:
https://blog.csdn.net/yul13579/article/details/134280724

但经过我的实测,不需要任何额外配置就可以调用两张魔改版本的2080ti(两张一模一样的卡)

未来可能的参考

迁移wsl

https://zhuanlan.zhihu.com/p/621873601

后记

这次安装工作步骤我均严格参考各个官方文档,仍然踩了不少的坑,但总比直接无脑copy网上的教程强。

我也不希望后人直接照抄我这篇博客,因为时代在发展,代码在变化。博客仅供参看流程和思想实验

在此隆重附上一切的一切的开始,也就是微软官方的wsl安装教程(点击这里)

希望你的wsl之旅顺利。

参考的过时博客,用来思想实验

列举几个示众
https://blog.csdn.net/iwanvan/article/details/122119595

https://zhuanlan.zhihu.com/p/515621724#%E5%AE%89%E8%A3%85wsl2

https://zhuanlan.zhihu.com/p/506477744


我用上了最好的Linux发行版!
http://blog.1314171.xyz/post/240131wsl2.html
作者
TT2TER
发布于
2024年1月31日
许可协议