在 Windows 中配置 WSL2 与 Debian 的全流程,希望对困扰于WSL相


在 Windows 中配置 WSL2 与 Debian 的全流程

ref:
How to Install WSL2 on Windows 10 (Updated) - visited on 2024-01-09
Microsoft - 如何使用 WSL 在 Windows 上安装 Linux - visited on 2024-01-09
清华大学开源软件镜像站 - Debian 软件源 - visited on 2024-01-09
Arch Wiki: Font configuration - visited on 2024-01-15
Arch Wiki: Input Method - visited on 2024-01-16


这篇文章记录了我在 Windows 10 上配置 WSL2 与 Debian 的全部流程与参考资料。希望对困扰于 WSL 相关问题的读者提供些许帮助。

谢绝转载。

Step1: 确认 Windows 版本

确认 Windows 版本为 Windows 10 版本 大于等于 2004 (内部版本 大于等于 19041) 或 Windows 11。

  1. 以管理员运行 PowerShell

  2. 输入 [System.Environment]::OSVersion.Version

    Windows PowerShell
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Try the new cross-platform PowerShell https://aka.ms/pscore6
    
    PS C:\Windows\system32> [System.Environment]::OSVersion.Version
    
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    10     0      19045  0
    
  3. 确认版本与 Build 符合要求。

Step2: 开启与 WSL 相关的 Windows Features

接下来,需要开启 Windows Subsystem Linux 和 Virtual Machine Platform 并重启 Windows。

  1. 以管理员运行 PowerShell

  2. 输入下述指令启动 WSL:

    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    
    PS C:\Windows\system32> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    
    Deployment Image Servicing and Management tool
    Version: 10.0.19041.3636
    
    Image Version: 10.0.19045.3803
    
    Enabling feature(s)
    [==========================100.0%==========================]
    The operation completed successfully.
    
  3. 输入下述指令启动 VM Platform:

    Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
    
    PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
    WARNING: Restart is suppressed because NoRestart is specified.
    
    
    Path          :
    Online        : True
    RestartNeeded : True
    
  4. 重启 Windows。

Step3: 安装 WSL 并设置版本

这一步里,安装 WSL 并设置版本.

  1. 以管理员运行 PowerShell

  2. 输入下述指令安装 WSL:

    wsl --install
    

    wsl --install --no-distribution
    

    在我的 Windows 版本下,wsl --install 不会安装 Ubuntu, 但根据 Microsoft 的文档,某些版本下默认会安装 Ubuntu。

  3. 重启 Windows

  4. 安装完成后设置 WSL 版本为 WSL2

    wsl --set-default-version 2
    
  5. 输入下述指令确认 wsl 版本:

    wsl --version
    
    PS C:\Windows\system32> wsl --version
    WSL version: 2.0.9.0
    Kernel version: 5.15.133.1-1
    WSLg version: 1.0.59
    MSRDC version: 1.2.4677
    Direct3D version: 1.611.1-81528511
    DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
    Windows version: 10.0.19045.3803
    

Step4: 下载并安装 Debian

在 Microsoft Store 中安装 Debian。

  1. 打开 Microsoft Store
  2. 安装 Debian
  3. 安装完成后打开 Debian
  4. 第一次进入 Debian 会要求设置用户名和密码。

小结

至此,Debian已经安装完成,打开即可使用 CLI 来操作了。不过,想要使用带GUI的软件,或是中文,我们还需要完成几件事:

  1. 配置 国内镜像源
  2. 配置 GUI 用来打开软件
  3. 配置 中文字体 与 输入法

Step5: 配置 国内镜像源

国内镜像源很多,这里选择清华的镜像源。

  1. 打开并登录 WSL - Debian

  2. 将原有文件设置备份

    sudo mv ~/etc/apt/sources.list ~/etc/apt/sources.list.backup
    
  3. 创建新的 ~/etc/apt/sources.list

    sudo touch ~/etc/apt/sources.list
    
  4. 在新的 sources.list 中输入:

    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    
    deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
    deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
    
  5. Debian CLI 中输入下述代码更新 Debian 软件

    sudo apt update -y && sudo apt upgrade -y
    
  6. Debian CLI 中输入下述代码安装 镜像站 https 证书

    sudo apt install apt-transport-https ca-certificates
    
  7. ~/etc/apt/sources.list 中,将所有的 http:// 改成 https://

至此,国内镜像站配置完成。

Step6: 配置 GUI

这一步,在 Windows 上安装并运行 X server,并配置 WSL2 中的 Debian 使用 X server 运行需要 GUI 的软件。

  1. 在 source forge 上下载 VcXsrv Windows X Server

  2. 安装 VcXsrv 并运行 XLaunch

  3. 在 XLaunch 的 Select display settings 中,将 Display number 改为 0

  4. 在 XLaunch 的 Extra Settings 中,勾选 Disable access control 选项

  5. 在 XLaunch 的 Configuration complete 界面,点击 Save configuration 来保存设置,下次通过保存的文件启动即可。

  6. 打开 WSL2 Debian (以后每次启动WSL2 Debian之前都需要打开 XLaunch,否则GUI软件无法运作。)

  7. 在 ~/.bashrc 中加入下述代码并保存后退出:

    # Setting DISPLAY pointing to the X server on Windows
    export DISPLAY=:0
    
  8. 在 Debian CLI 中 source ~/.bashrc

至此,如果 Debian 安装了 firefox-esr,X Server 会在 Windows 新开一个窗口并运行 firefox-esr。

**Note: ** WSL2 更新速度跟不上一些软件,导致WSL不能完美支持一些软件的最新版本,如 firefox-esr。

Step7: 配置中文字体

默认的 Debian 为英文环境,我们需要配置中文环境与相关的管理软件。

  1. 检查是否安装语言环境管理包 locales、字体管理包 fontconfigs

    apt list --installed | grep locales
    apt list --installed | grep fontconfigs
    
  2. 若没有locales,则安装 :

    sudo apt-get install locales
    

    在 Debian CLI 中配置 locales

    sudo dpkg-reconfigure locales
    

    配置界面中,上下左右为移动键,空格为选择键,回车为确认键

    需要选择 en_US.utf8zh_CN.utf8

    默认 localesen_US.utf8 (默认自行选择即可)

    .bashrc 中加入下列代码使 CLI 支持中文字符:

    export LC_ALL=en_US.UTF-8
    export LANGUAGE=en_US.UTF-8
    

    在 CLI 中 source .bashrc 后,输入 locale 确认 LC_ALLLANGUAGELC_ALL 都设置成 en_US.UTF-8:

 $ locale
LANG=en_US.UTF-8
LANGUAGE=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8            
  1. 配置 fontconfigs,启用bitmap (可略过此步骤):

    sudo dpkg-reconfigure fontconfig-config
    

    在 preferred method for tuning fonts 中,选择 Autohinter (Native/Autohinter/None)

    在 preferred automatic font hinting styple 中,选择 Medium (None/Slight/Medium/Full)

    在 subpixel rendering 中,选择 Automatic (Automatic/Always/Never)

    在 enable bitmapped fonts by default 中,选择 Yes (Yes/No)

  2. 安装可直接通过 apt 管理的常用字体:

     sudo apt-get install <FONTPACKAGE_NAME>
    
    # Common monospace font
    fonts-jetbrains-mono 
    fonts-hack
    # Common english fonts
    fonts-liberation2 
    # Common Chinese fonts
    *fonts-wqy*
    *fonts-arphic*
    
    sudo apt-get install xfonts-intl-chinese
    

至此,打开其他带有中文 GUI 的软件,中文可以正常显示。

Step8:配置中文输入法

  1. 检查常见的 IMF(Input-Method Framework) 是否存在。

    apt list --installed | grep ibus
    apt list --installed | grep fcitx
    

    若不存在则安装喜欢的IMF。我选择了 iBus。

    sudo apt-get install ibus
    
  2. 确认 dbus (desktop bus) 是否安装

    apt list --installed | grep dbus
    

    若不存在则安装 dbus-x11

    sudo apt-get install dbus-x11
    
  3. 安装基于对应 IMF 的 IME(Input-Method Editor),我选择了 ibus-pinyin

    sudo apt-get install ibus-pinyin
    
  4. 配置iBus,自己喜欢就好。

    ibus-setup
    
  5. 修改 ~/.bashrc, 添加下列变量:

    export GTK_IM_MODULE=ibus
    export QT_IM_MODULE=ibus
    export XMODIFIERS=@im=ibus
    
  6. 之后在 CLI 或者 firefox 等GUI页面中就可以使用中文输入法输入中文了。

    值得注意的是,WSL2打开的CLI terminal是无法显示中文(可以输入,但无法显示)的,想在 terminal 中使用中文,需要用支持 UTF-8 的 terminal emulator 才行。

    我自己测试时发现每次进入 debian 时 ibus 都已经启动了,可能在 ibus-setup 中自动设置了吧。

总结

至此,在windows中配置 WSL2 与 Debian 的全部流程就结束了。现在的 Debian 有了国内的镜像源、GUI、中文字体与输入法,可以的在Debian上做自己其他想做的事情了。

祝好。

相关内容