关闭 WSL2
# 完全关闭 WSL
wsl --shutdown
# 关闭某个系统
wsl --terminate Ubuntu
删除 WSL 发行版
# 列出当前安装的 WSL 实例
wsl -l
# 移除某个实例
wsl --unregister Ubuntu-20.04
不使用 Windows 环境变量
否则会在使用 crosstool-ng 的时候出现奇怪的问题。
# vim /etc/wsl.conf
[interop]
appendWindowsPath = false
使用 systemd
使用条件:
wsl --version
查看 WSL 版本,要求 WSL 版本为 0.67.6 或以上
使用命令:
echo -e "[boot]\nsystemd=true" | sudo tee -a /etc/wsl.conf
然后通过 wsl --shutdown
关闭 WSL 并重启。
判断 systemd 是否启动成功:
ps --no-headers -o comm 1
需要返回 systemctl
而不是 init
。
开启 KVM
这一步主要是为了开启嵌套虚拟化
# vim /mnt/c/Users/<username>/.wslconfig
[wsl2]
nestedVirtualization=true
检查 kvm 是否启动:
sudo apt install cpu-checker
kvm-ok
# INFO: /dev/kvm exists
# KVM acceleration can be used
安装一些包
sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager
检查是否安装成功:
sudo systemctl is-active libvirtd
# active
开启 KVM-参考资料
获取 IP 和端口
export hostIP=`grep -oP "(\d+\.)+(\d+)" /etc/resolv.conf`
export hostPort=7890
sed -i "162c http $hostIP $hostPort" /home/user/proxychains4.conf # 因人而异
git config --global http.proxy http://$hostIP:$hostPort
git config --global https.proxy http://$hostIP:$hostPort
使用 CUDA
理论上新版驱动已经帮忙做好所有基础了,但由于我们不使用 Windows 环境变量,因此需要手动添加环境变量:
export PATH="$PATH:/usr/lib/wsl/lib"
然后执行 nvidia-smi
检查 CUDA 版本:
$ nvidia-smi
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.76.01 Driver Version: 552.22 CUDA Version: 12.4 |
|-----------------------------------------+------------------------+----------------------+
| 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 4070 Ti On | 00000000:01:00.0 On | N/A |
| 0% 37C P8 11W / 285W | 924MiB / 12282MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
接着去 https://developer.nvidia.com/cuda-downloads 下载对应的 CUDA,我这里下载了 .run
文件,官方也给出了安装说明:
wget https://developer.download.nvidia.com/compute/cuda/12.4.0/local_installers/cuda_12.4.0_550.54.14_linux.run
sudo sh cuda_12.4.0_550.54.14_linux.run
在安装完成后添加环境变量:
export PATH=/usr/local/cuda-12.4/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.4/lib64:$LD_LIBRARY_PATH
然后输入 nvcc -V
进行检查:
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Tue_Feb_27_16:19:38_PST_2024
Cuda compilation tools, release 12.4, V12.4.99
Build cuda_12.4.r12.4/compiler.33961263_0
接着去 https://developer.nvidia.com/cudnn-downloads 下载 cuDNN,安装过程略过不表。
使用 PyTorch 验证安装
pipenv install torch torchvision torchaudio
测试:
import torch
# PyTorch 版本
torch.__version__
# '2.3.0+cu121'
# CUDA 是否可用
torch.cuda.is_available()
# GPU 数量
torch.cuda.device_count()
# GPU 名称
torch.cuda.get_device_name(0)