翼度科技»论坛 云主机 LINUX 查看内容

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

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
在 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。

  • 以管理员运行 PowerShell
  • 输入 [System.Environment]::OSVersion.Version
    1. Windows PowerShell
    2. Copyright (C) Microsoft Corporation. All rights reserved.
    3. Try the new cross-platform PowerShell https://aka.ms/pscore6
    4. PS C:\Windows\system32> [System.Environment]::OSVersion.Version
    5. Major  Minor  Build  Revision
    6. -----  -----  -----  --------
    7. 10     0      19045  0
    复制代码
  • 确认版本与 Build 符合要求。
Step2: 开启与 WSL 相关的 Windows Features

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

  • 以管理员运行 PowerShell
  • 输入下述指令启动 WSL:
    1. dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    复制代码
    1. PS C:\Windows\system32> dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
    2. Deployment Image Servicing and Management tool
    3. Version: 10.0.19041.3636
    4. Image Version: 10.0.19045.3803
    5. Enabling feature(s)
    6. [==========================100.0%==========================]
    7. The operation completed successfully.
    复制代码
  • 输入下述指令启动 VM Platform:
    1. Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
    复制代码
    1. PS C:\Windows\system32> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart
    2. WARNING: Restart is suppressed because NoRestart is specified.
    3. Path          :
    4. Online        : True
    5. RestartNeeded : True
    复制代码
  • 重启 Windows。
Step3: 安装 WSL 并设置版本

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

  • 以管理员运行 PowerShell
  • 输入下述指令安装 WSL:
    1. wsl --install
    复制代码
    1. wsl --install --no-distribution
    复制代码
    在我的 Windows 版本下,wsl --install 不会安装 Ubuntu, 但根据 Microsoft 的文档,某些版本下默认会安装 Ubuntu。
  • 重启 Windows
  • 安装完成后设置 WSL 版本为 WSL2
    1. wsl --set-default-version 2
    复制代码
  • 输入下述指令确认 wsl 版本:
    1. wsl --version
    复制代码
    1. PS C:\Windows\system32> wsl --version
    2. WSL version: 2.0.9.0
    3. Kernel version: 5.15.133.1-1
    4. WSLg version: 1.0.59
    5. MSRDC version: 1.2.4677
    6. Direct3D version: 1.611.1-81528511
    7. DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
    8. Windows version: 10.0.19045.3803
    复制代码
Step4: 下载并安装 Debian

在 Microsoft Store 中安装 Debian。

  • 打开 Microsoft Store
  • 安装 Debian
  • 安装完成后打开 Debian
  • 第一次进入 Debian 会要求设置用户名和密码。
小结

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

  • 配置 国内镜像源
  • 配置 GUI 用来打开软件
  • 配置 中文字体 与 输入法
Step5: 配置 国内镜像源

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

  • 打开并登录 WSL - Debian
  • 将原有文件设置备份
    1. sudo mv ~/etc/apt/sources.list ~/etc/apt/sources.list.backup
    复制代码
  • 创建新的 ~/etc/apt/sources.list
    1. sudo touch ~/etc/apt/sources.list
    复制代码
  • 在新的 sources.list 中输入:
    1. deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    2. deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
    3. deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    4. deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
    5. deb http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    6. deb-src http://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
    7. deb http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
    8. deb-src http://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware
    复制代码
  • Debian CLI 中输入下述代码更新 Debian 软件
    1. sudo apt update -y && sudo apt upgrade -y
    复制代码
  • Debian CLI 中输入下述代码安装 镜像站 https 证书
    1. sudo apt install apt-transport-https ca-certificates
    复制代码
  • 在 ~/etc/apt/sources.list 中,将所有的 http:// 改成 https://
至此,国内镜像站配置完成。
Step6: 配置 GUI

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

  • source forge 上下载 VcXsrv Windows X Server
  • 安装 VcXsrv 并运行 XLaunch
  • 在 XLaunch 的 Select display settings 中,将 Display number 改为 0
  • 在 XLaunch 的 Extra Settings 中,勾选 Disable access control 选项
  • 在 XLaunch 的 Configuration complete 界面,点击 Save configuration 来保存设置,下次通过保存的文件启动即可。
  • 打开 WSL2 Debian (以后每次启动WSL2 Debian之前都需要打开 XLaunch,否则GUI软件无法运作。)
  • 在 ~/.bashrc 中加入下述代码并保存后退出:
    1. # Setting DISPLAY pointing to the X server on Windows
    2. export DISPLAY=:0
    复制代码
  • 在 Debian CLI 中 source ~/.bashrc
至此,如果 Debian 安装了 firefox-esr,X Server 会在 Windows 新开一个窗口并运行 firefox-esr。
**Note: ** WSL2 更新速度跟不上一些软件,导致WSL不能完美支持一些软件的最新版本,如 firefox-esr。
Step7: 配置中文字体

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

  • 检查是否安装语言环境管理包 locales、字体管理包 fontconfigs
    1. apt list --installed | grep locales
    2. apt list --installed | grep fontconfigs
    复制代码
  • 若没有locales,则安装 :
    1. sudo apt-get install locales
    复制代码
    在 Debian CLI 中配置 locales
    1. sudo dpkg-reconfigure locales
    复制代码
    配置界面中,上下左右为移动键,空格为选择键,回车为确认键
    需要选择 en_US.utf8 与 zh_CN.utf8
    默认 locales 选 en_US.utf8 (默认自行选择即可)
    在 .bashrc 中加入下列代码使 CLI 支持中文字符:
    1. export LC_ALL=en_US.UTF-8
    2. export LANGUAGE=en_US.UTF-8
    复制代码
    在 CLI 中 source .bashrc 后,输入 locale 确认 LC_ALL 、LANGUAGE 、LC_ALL 都设置成 en_US.UTF-8:
  1. $ locale
  2. LANG=en_US.UTF-8
  3. LANGUAGE=en_US.UTF-8
  4. LC_CTYPE="en_US.UTF-8"
  5. LC_NUMERIC="en_US.UTF-8"
  6. LC_TIME="en_US.UTF-8"
  7. LC_COLLATE="en_US.UTF-8"
  8. LC_MONETARY="en_US.UTF-8"
  9. LC_MESSAGES="en_US.UTF-8"
  10. LC_PAPER="en_US.UTF-8"
  11. LC_NAME="en_US.UTF-8"
  12. LC_ADDRESS="en_US.UTF-8"
  13. LC_TELEPHONE="en_US.UTF-8"
  14. LC_MEASUREMENT="en_US.UTF-8"
  15. LC_IDENTIFICATION="en_US.UTF-8"
  16. LC_ALL=en_US.UTF-8            
复制代码

  • 配置 fontconfigs,启用bitmap (可略过此步骤):
    1. 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)
  • 安装可直接通过 apt 管理的常用字体:
    1. sudo apt-get install <FONTPACKAGE_NAME>
    2. # Common monospace font
    3. fonts-jetbrains-mono
    4. fonts-hack
    5. # Common english fonts
    6. fonts-liberation2
    7. # Common Chinese fonts
    8. *fonts-wqy*
    9. *fonts-arphic*
    10. sudo apt-get install xfonts-intl-chinese
    复制代码
至此,打开其他带有中文 GUI 的软件,中文可以正常显示。
Step8:配置中文输入法


  • 检查常见的 IMF(Input-Method Framework) 是否存在。
    1. apt list --installed | grep ibus
    2. apt list --installed | grep fcitx
    复制代码
    若不存在则安装喜欢的IMF。我选择了 iBus。
    1. sudo apt-get install ibus
    复制代码
  • 确认 dbus (desktop bus) 是否安装
    1. apt list --installed | grep dbus
    复制代码
    若不存在则安装 dbus-x11
    1. sudo apt-get install dbus-x11
    复制代码
  • 安装基于对应 IMF 的 IME(Input-Method Editor),我选择了 ibus-pinyin
    1. sudo apt-get install ibus-pinyin
    复制代码
  • 配置iBus,自己喜欢就好。
    1. ibus-setup
    复制代码
  • 修改 ~/.bashrc, 添加下列变量:
    1. export GTK_IM_MODULE=ibus
    2. export QT_IM_MODULE=ibus
    3. export XMODIFIERS=@im=ibus
    复制代码
  • 之后在 CLI 或者 firefox 等GUI页面中就可以使用中文输入法输入中文了。
    值得注意的是,WSL2打开的CLI terminal是无法显示中文(可以输入,但无法显示)的,想在 terminal 中使用中文,需要用支持 UTF-8 的 terminal emulator 才行。
    我自己测试时发现每次进入 debian 时 ibus 都已经启动了,可能在 ibus-setup 中自动设置了吧。
总结

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

来源:https://www.cnblogs.com/wanderoff-night/p/17988720
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具