WSL配置

近期由于需要使用特定版本的cuda,在window上配置不便,有随时重开的风险,遂在WSL上进行配置

WSL安装与对应linux发行版下载

WSL实际是WSL2,按照Microsoft官方教程操作即可

linux发行版如Ubuntu在Microsoft Store中下载即可

常用操作

查看 WSL 的运行状态

1
wsl -l -v

关闭wsl

1
wsl --shutdown

导出当前的 Linux 系统镜像:

1
wsl --export Ubuntu d:\image_ubuntu.tar

移除之前注册的 WSL,此操作可以不需要重新在Microsoft Store中重新下载linux发行版

1
wsl --unregister Ubuntu

换源

Ubuntu的系统自带的配置文件在/etc/apt/source.list中

首先进入配置文件目录,如下命令:

1
cd /etc/apt

备份source.list

1
sudo cp sources.list sources.list.bak

根据Ubuntu版本修改source.list为如下的版本,可用ggdG快速删除内容

阿里云Ubuntu20.04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

阿里云Ubuntu22.04

1
2
3
4
5
6
7
8
9
10
11
12
13
14
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse

# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse

安装ssh server

需要安装ssh server,否则git clone等依赖ssh的操作无法进行

1
sudo apt-get install openssh-server

并启动ssh服务

1
sudo systemctl start ssh

如果报如下的错误,需修改/etc/ssh/sshd_config中的Port为除22以外的其他端口,报错中是sshd出的错误,所以修改这个配置文件

安装AnaConda

首先回到根目录下,下载安装包:在此地址 https://www.anaconda.com/download/success 中找到安装包的链接

1
wget https://repo.anaconda.com/archive/Anaconda3-2024.02-1-Linux-x86_64.sh

安装 anaconda,按照官方配置不需要设置~/.bashrc

1
bash Anaconda3-2024.02-1-Linux-x86_64.sh

在~/.bashrc中(可选)设置 Anaconda 快捷键,需要通过source ~/.bashrc激活

1
2
alias act='conda activate'
alias deact='conda deactivate'

配置cuda

Nvidia官方CUDA on WSL (nvidia.com)已经有了一定的支持,不过还需要增加一些额外配置

首先,按照官方guide,remove the old GPG key

1
sudo apt-key del 7fa2af80

接下来,在CUDA Toolkit Archive | NVIDIA Developer中选择需要的Cuda版本进行下载,有WSL-Ubuntu的官方支持

按照提供的Base Installer的指导安装即可

最后在~/.bashrc 中添加环境变量,以安装的cuda版本为11.3为例,需要添加如下的变量,终端执行 source ~/.bashrc即可生效

1
2
3
4
export CUDA_HOME="/usr/local/cuda-11.3"
export CUDA_INSTALL_PATH="/usr/local/cuda-11.3"
export PATH=$CUDA_INSTALL_PATH/bin:$PATH
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.3/lib64

配置Docker

在wsl上没有原生支持的docker,需要要Docker Desktop结合使用,在Windows上安装docker,借助WSL的内核进行驱动

首先,按照MS提供的官方教程安装Docker Destop,并且根据里面的要求设置Docker

WSL 上的 Docker 容器入门 | Microsoft Learn

开始在 Visual Studio Code 中使用 Docker 应用 | Microsoft Learn

最好依照它的建议安装vscode相关依赖

接下来,如果在执行docker run hello-world进行测试时出现如下错误,说明docker的镜像有问题,需要进行换源

1
2
3
4
5
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Retrying in 10 seconds
docker: error pulling image configuration: download failed after attempts=6: dial tcp 128.242.245.93:443: connect: connection refused.
See 'docker run --help'.

/etc/docker/daemon.json中加入如下内容,如果没有这个文件或文件夹就创建新的

1
2
3
4
5
6
7
8
9
10
{
"registry-mirrors": [
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://docker.m.daocloud.io",
"https://dockerproxy.com",
"https://docker.mirrors.ustc.edu.cn",
"https://docker.nju.edu.cn"
]
}

接下来执行如下指令重启docker,可能第二条命令在WSL中报错,对于Docker Desktop来说,直接在Windows中重启Docker即可

1
2
3
sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl status docker

最后,终端中执行docker info,可以看到Registry Mirror中有刚刚配置的源,说明配置成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
Client: Docker Engine - Community
Version: 26.1.4
Context: default
Debug Mode: false
Plugins:
buildx: Docker Buildx (Docker Inc.)
Version: v0.14.1
Path: /usr/libexec/docker/cli-plugins/docker-buildx
compose: Docker Compose (Docker Inc.)
Version: v2.27.1
Path: /usr/libexec/docker/cli-plugins/docker-compose

Server:
Containers: 1
Running: 0
Paused: 0
Stopped: 1
Images: 1
Server Version: 26.1.4
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Using metacopy: false
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d2d58213f83a351ca8f528a95fbd145f5654e957
runc version: v1.1.12-0-g51d5e94
init version: de40ad0
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-1160.119.1.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 2.761GiB
Name: wzy1303
ID: 74efae68-ef43-45a9-b547-ffa2c3805423
Docker Root Dir: /var/lib/docker
Debug Mode: false
Username: inkling1303
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
https://do.nark.eu.org/
https://dc.j8.work/
https://docker.m.daocloud.io/
https://dockerproxy.com/
https://docker.mirrors.ustc.edu.cn/
https://docker.nju.edu.cn/
Live Restore Enabled: false

参考

WSL2的安装与配置(创建Anaconda虚拟环境、更新软件包、安装PyTorch、VSCode)-CSDN博客

CUDA on WSL (nvidia.com)

CUDA on WSL2安装记录 - 知乎 (zhihu.com)

WSL不同版本的Ubuntu更换清华镜像,加速Ubuntu软件下载速度_wsl下载ubuntu速度慢-CSDN博客

Ubuntu配置CUDA环境变量究极解读_ubuntu中cuda环境变量设置-CSDN博客

CUDA与CUDNN在Windows下的安装与配置(超级详细版)_cudnn安装windows-CSDN博客

Docker运行hello-world镜像失败或超时_unable to find image ‘hello-world:latest’ locally -CSDN博客