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

这篇具有很好参考价值的文章主要介绍了在 Windows 中配置 WSL2 与 Debian 的全流程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

谢绝转载。

整体流程如下:

  1. 确认 Windows 是否支持 WSL2

  2. 安装 Debian

  3. 完整 Debian 的基础配置,起码得能用。

确认 Windows 版本

确认 Windows 版本为 Windows 10 版本 $$\geq$$ 2004 (内部版本 $$\geq$$ 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 符合要求。

开启与 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。

安装 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
    

下载并安装 Debian

在 Microsoft Store 中安装 Debian。

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

小结

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

  1. 配置 基本的 WSL - Win10 剪切板互通
  2. 配置 国内镜像源
  3. 配置 GUI 用来打开软件
  4. 配置 中文字体
  5. 配置 中文输入法

配置 基本的 WSL - Win10 剪切板互通

  1. 打开 WSL - Debian 界面,右键点击界面的标题栏,选中属性

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

  2. 在选项中勾选 ”将 Ctrl + Shift + C/V“ 用作复制/粘贴的快捷键

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

想要更加完美的实现剪切板互通,需要让 Debian 调用 Win10 的 clip.exe 实现。在一些软件里会有这样的需求,笔者后续会实现 WSL-Debian-vim 读写 Win10 剪切板

配置 国内镜像源

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

  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 中输入下述代码安装 镜像站 https 证书

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

  7. Debian CLI 中输入下述代码更新 Debian 软件

    sudo apt update -y && sudo apt upgrade -y
    

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

配置 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

  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。

配置中文字体

默认的 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            
    
  3. 配置 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)

  4. 安装可直接通过 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 的软件,中文可以正常显示。

配置中文输入法

  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上做自己其他想做的事情了。

祝好。


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
文章来源地址https://www.toymoban.com/news/detail-825197.html

到了这里,关于在 Windows 中配置 WSL2 与 Debian 的全流程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 在Windows运行完整的Linux桌面GUI!Kali on WSL2安装与简单配置指南

    Windows在更新到2004版本之后正式开启了WSL2的支持,这次是完整的运行在HyperV虚拟机中的Linux系统了,可以称之为真正的Linux了,难不成Windows即将成为最好的Linux发行版? 实际体验下来还是可以的,不过小问题不少,爱折腾的同学安排一手,对提高Windows平台下的开发体验还是有

    2024年04月12日
    浏览(29)
  • 【番外】【Airsim in Windows & ROS in WSL2-Ubuntu20.04】环境配置大全

    AirSim的使用着实是一件麻烦事,我的配置过程可谓是坎坷重重。原因在于AirSim对于电脑要求较高,所以即使我手头有一个Interl mini PC,但是由于没有独立显卡,也没有办法实现在Ubuntu环境下使用AirSim。而电脑又不太想装双系统,并且在VMware虚拟机里面没有办法使用电脑的独立

    2024年02月19日
    浏览(33)
  • 【Docker Desktop】Windows11家庭版安装docker desktop和WSl2(Ubuntu22.04)并完成迁移,配置国内镜像

    目录 一、准备工作 ① 安装Hyper-V ②开启电脑的虚拟化技术,如何确定是否开启了虚拟化技术 ③ 启动Windows功能 二、安装WSl 三、移动Ubuntu 22.04.2 LTS 四、迁移 WSL2 安装位置 五、安装Docker Desktop 六、配置Docker Desktop  七、测试 因为win11家庭版本身是不支持hyper-V的,需要手动安装

    2024年01月20日
    浏览(45)
  • 【2023最新版】Win11: WSL(Ubuntu22.04)使用docker远程容器教程(Windows的Docker Desktop下载安装、迁移到非系统盘、配置国内镜像源、设置 WSL2)

    目录 一、准备工作 1. 安装WSL(适用于 Linux 的 Windows 子系统) 2. docker简介——来源chatGPT 二、Windows安装 Docker Desktop 1. 官网链接 2. 安装过程 3. 迁移到非系统盘 4. 配置国内镜像源 国内镜像 方法1——通过Docker-Desktop配置 方法2——找打daemon.json文件进行配置 三、在适用于 Wind

    2024年02月12日
    浏览(58)
  • WSL2+windows terminal

    WSL2的安装与使用-Windows与Linux双系统的新选择 既要使用Windows系统满足日常生活,协作办公,又要使用Linux系统满足开发工作,双系统来回重启比较麻烦,双主机成本高,远程服务器又有很多限制,WSL也许是更好的解决方案,可以摆脱以上困扰。 1. WSL是什么 WSL全称 Windows Subs

    2024年02月06日
    浏览(38)
  • windows10安装wsl2

    注意: 在安装wsl_update.msi前要重启一次 https://zhuanlan.zhihu.com/p/355276815

    2024年02月11日
    浏览(31)
  • Windows wsl2安装Ubuntu

    wsl(Windows Subsystem for Linux)即适用于Windows的Linux子系统,是一个实现在Windows 10 / 11上运行原生Linux的技术。 wsl2 为其迭代版本,可以更好的在Windows上运行Linux子系统。 这里以 Windows 11 安装Ubuntu作为示例。 按 Win 键,点击 设置 。 搜索 控制面板 。 点击 程序 。 点击启用或关闭

    2024年02月09日
    浏览(32)
  • windows10安装wsl2,centos内核

    检查系统环境 必须运行 Windows 10 版本 2004 及更高版本(内部版本 19041 及更高版本)或 Windows 11 才能使用以下命令。 一、开启WSL2特性 【控制面板】【程序】【程序和功能】【启用或关闭Windows功能】 二、下载Linux内核更新包 下载 双击安装 三、设置WSL2为默认版本 四、下载安

    2024年02月12日
    浏览(28)
  • Windows+WSL2+SSH实现远程办公

    在这里分享一下我如何实现用MacBook连接主机服务器远程办公,我目前有一台主机在寝室,但是在教室或者图书馆我手边是一台MacBook便不能跑一些算力较大的项目,这时我只需要远程打开我的主机服务器,然后用MacBook的终端或者vs code等连接到我的主机服务器遍可以进行一些大

    2024年01月21日
    浏览(26)
  • 【WSL2】在Windows和wsl2中互相网络访问的优雅做法,不用再手动写死IP了

    目前在windows与windows的wsl系统之间,还无法通过相对较固定的方式来互相网络访问,通常需要使用各自的ip来互相访问,而ip可能不是很直观并且可能会有变更,当前教程解决了这个问题,通过hosts映射的方式打通wsl与windows宿主机之间的访问。 脚本的原理:在linux启动时或she

    2024年04月17日
    浏览(21)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包