win11 安装配置 wsl2
utils
本文字数:1.7k 字 | 阅读时长 ≈ 9 min

win11 安装配置 wsl2

utils
本文字数:1.7k 字 | 阅读时长 ≈ 9 min

1. 安装

windows 的安装非常简单,管理员运行 cmd,然后输入以下命令即可 wsl --install,注意需要设置一下 username 和 password

C:\Users\harryyhwang>wsl --install
正在安装: Ubuntu
已安装 Ubuntu。
正在启动 Ubuntu...
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: harry
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 24.04.1 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

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

 System information as of Fri Oct 11 13:14:09 CST 2024

  System load:  0.09                Processes:             59
  Usage of /:   0.1% of 1006.85GB   Users logged in:       0
  Memory usage: 4%                  IPv4 address for eth0: 172.30.255.187
  Swap usage:   0%


This message is shown once a day. To disable it please create the
/home/harry/.hushlogin file.
harry@HARRYYHWAN-PCHT:~$ sudo passwd root
[sudo] password for harry:
New password:
Retype new password:
passwd: password updated successfully
harry@HARRYYHWAN-PCHT:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.1 LTS
Release:        24.04
Codename:       noble

上面安装的的 ubuntu24.04,如果需要安装别的版本例如 ubuntu20.04,在微软商店搜索安装即可,也可以根据官网指引安装 https://learn.microsoft.com/zh-cn/windows/wsl/install-manual

C:\Users\harryyhwang>wsl --list --online
以下是可安装的有效分发的列表。
使用 'wsl.exe --install <Distro>' 安装。

NAME                            FRIENDLY NAME
Ubuntu                          Ubuntu
Debian                          Debian GNU/Linux
kali-linux                      Kali Linux Rolling
Ubuntu-18.04                    Ubuntu 18.04 LTS
Ubuntu-20.04                    Ubuntu 20.04 LTS
Ubuntu-22.04                    Ubuntu 22.04 LTS
Ubuntu-24.04                    Ubuntu 24.04 LTS
OracleLinux_7_9                 Oracle Linux 7.9
OracleLinux_8_7                 Oracle Linux 8.7
OracleLinux_9_1                 Oracle Linux 9.1
openSUSE-Leap-15.6              openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP5    SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6    SUSE Linux Enterprise 15 SP6
openSUSE-Tumbleweed             openSUSE Tumbleweed

C:\Users\harryyhwang>wsl --list --verbose
  NAME              STATE           VERSION
* docker-desktop    Running         2
  Ubuntu-20.04      Stopped         2
  Ubuntu            Stopped         2

C:\Users\harryyhwang>wsl -d Ubuntu-20.04
root@HARRYYHWAN-PCHT:/mnt/c/Users/harryyhwang#

2. WSL 访问 Windows 文件

Windows 系统的硬盘挂载在 WSL 的 /mnt 路径下,用户可以在 WSL 终端中输入 cd /mnt/d 命令进入 Windows 系统的 D 盘,然后便可编辑和运行 D 盘中的文件

3. 安装 docker

第一种方法

安装 docker desktop,然后按照下面步骤来即可

When Docker Desktop starts, go to Settings > Resources > WSL Integration.

如下图,把需要的打开即可

第二种方法

这种方法就是手动安装了,执行以下命令即可

curl -fsSL https://test.docker.com -o test-docker.sh
sudo sh test-docker.sh

4. 一些问题

1. docker pull 报错

error response from daemon: head "https://registry-1.docker.io/v2/harrytea/paddleocr/manifests/v3": get "https://auth.docker.io/token?scope=repository%3aharrytea%2fpaddleocr%3apull&service=registry.docker.io": http: server gave http response to https client

这是由于网络问题,改成国内镜像即可,修改以下文件 vim /etc/docker/daemon.json

加入以下内容

{
    "registry-mirrors": [
        "https://registry.docker-cn.com",
        "http://hub-mirror.c.163.com",
        "https://docker.mirrors.ustc.edu.cn",
        "https://cr.console.aliyun.com",
        "https://mirror.ccs.tencentyun.com"
        ]
}

重启 docker 就可以看到 mirrors 有源了

➜ docker sudo systemctl restart docker
➜ docker docker info             
Client: Docker Engine - Community
 Version:    27.3.1
 ...
 Registry Mirrors:
  https://registry.docker-cn.com/
  http://hub-mirror.c.163.com/
  https://docker.mirrors.ustc.edu.cn/
  https://cr.console.aliyun.com/
  https://mirror.ccs.tencentyun.com/
  ...

2. gpu 挂载失败

以下命令挂载失败 docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].

(py) ➜ /data docker run -it --gpus all -v /data:/data harrytea/paddleocr:v3 /bin/bash
docker: Error response from daemon: could not select device driver "" with capabilities: [[gpu]].

这是因为缺少依赖,将以下脚本保存为 nvidia-container-runtime-script.sh 并执行

# nvidia-container-runtime-script.sh

sudo curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
  sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
sudo curl -s -L https://nvidia.github.io/nvidia-container-runtime/$distribution/nvidia-container-runtime.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update

随后执行下面命令就可以了

sudo apt-get install nvidia-container-runtime
systemctl restart  docker
(py) ➜ /data sudo sh nvidia-container-runtime-script.sh

OK
deb https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/$(ARCH) /
#deb https://nvidia.github.io/libnvidia-container/experimental/ubuntu18.04/$(ARCH) /
deb https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/$(ARCH) /
#deb https://nvidia.github.io/nvidia-container-runtime/experimental/ubuntu18.04/$(ARCH) /
Get:1 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  InRelease [1484 B]
Get:2 https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64  InRelease [1481 B]                                          
Get:3 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  Packages [29.2 kB]                                               
Get:4 https://nvidia.github.io/nvidia-container-runtime/stable/ubuntu18.04/amd64  Packages [7416 B]                                                       
Hit:5 https://download.docker.com/linux/ubuntu focal InRelease                                                                     
Hit:6 http://security.ubuntu.com/ubuntu focal-security InRelease                                
Hit:7 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:8 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:9 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Fetched 39.6 kB in 2s (26.4 kB/s)
Reading package lists... Done
(py) ➜ /data sudo apt-get install nvidia-container-runtime

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libnvidia-container-tools libnvidia-container1 nvidia-container-toolkit nvidia-container-toolkit-base
The following NEW packages will be installed:
  libnvidia-container-tools libnvidia-container1 nvidia-container-runtime nvidia-container-toolkit nvidia-container-toolkit-base
0 upgraded, 5 newly installed, 0 to remove and 156 not upgraded.
Need to get 4055 kB of archives.
After this operation, 15.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  libnvidia-container1 1.13.5-1 [928 kB]
Get:2 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  libnvidia-container-tools 1.13.5-1 [24.9 kB]
Get:3 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  nvidia-container-toolkit-base 1.13.5-1 [2244 kB]
Get:4 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  nvidia-container-toolkit 1.13.5-1 [853 kB]
Get:5 https://nvidia.github.io/libnvidia-container/stable/ubuntu18.04/amd64  nvidia-container-runtime 3.13.0-1 [4988 B]
Fetched 4055 kB in 2s (2049 kB/s)             
Selecting previously unselected package libnvidia-container1:amd64.
(Reading database ... 34452 files and directories currently installed.)
Preparing to unpack .../libnvidia-container1_1.13.5-1_amd64.deb ...
Unpacking libnvidia-container1:amd64 (1.13.5-1) ...
Selecting previously unselected package libnvidia-container-tools.
Preparing to unpack .../libnvidia-container-tools_1.13.5-1_amd64.deb ...
Unpacking libnvidia-container-tools (1.13.5-1) ...
Selecting previously unselected package nvidia-container-toolkit-base.
Preparing to unpack .../nvidia-container-toolkit-base_1.13.5-1_amd64.deb ...
Unpacking nvidia-container-toolkit-base (1.13.5-1) ...
Selecting previously unselected package nvidia-container-toolkit.
Preparing to unpack .../nvidia-container-toolkit_1.13.5-1_amd64.deb ...
Unpacking nvidia-container-toolkit (1.13.5-1) ...
Selecting previously unselected package nvidia-container-runtime.
Preparing to unpack .../nvidia-container-runtime_3.13.0-1_all.deb ...
Unpacking nvidia-container-runtime (3.13.0-1) ...
Setting up nvidia-container-toolkit-base (1.13.5-1) ...
Setting up libnvidia-container1:amd64 (1.13.5-1) ...
Setting up libnvidia-container-tools (1.13.5-1) ...
Setting up nvidia-container-toolkit (1.13.5-1) ...
Setting up nvidia-container-runtime (3.13.0-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
(py) ➜ /data  systemctl restart  docker                                              
(py) ➜ /data docker run -it --gpus all -v /data:/data harrytea/paddleocr:v3 /bin/bash

==========
== CUDA ==
==========

CUDA Version 11.8.0

Container image Copyright (c) 2016-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.

This container image and its contents are governed by the NVIDIA Deep Learning Container License.
By pulling and using the container, you accept the terms and conditions of this license:
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license

A copy of this license is made available in this container at /NGC-DL-CONTAINER-LICENSE for your convenience.

root@1a231cfac644:/# 
4月 06, 2025
3月 10, 2025
12月 31, 2024