diff --git a/docs/Ch10/images/ms-store-search-linux.png b/docs/Appendix/images/ms-store-search-linux.png similarity index 100% rename from docs/Ch10/images/ms-store-search-linux.png rename to docs/Appendix/images/ms-store-search-linux.png diff --git a/docs/Appendix/images/vscode-store-remote-wsl.png b/docs/Appendix/images/vscode-store-remote-wsl.png new file mode 100644 index 00000000..abf8e48a Binary files /dev/null and b/docs/Appendix/images/vscode-store-remote-wsl.png differ diff --git a/docs/Appendix/wsl.md b/docs/Appendix/wsl.md new file mode 100644 index 00000000..a25b39d3 --- /dev/null +++ b/docs/Appendix/wsl.md @@ -0,0 +1,110 @@ +# Windows Subsystem for Linux + +!!! Warning "本文仍在编辑中,尚未校对、审阅和修订" + +!!! abstract "导言" + + 本章节将介绍在 Windows 上体验和使用 Linux 的较好的方法之一——Windows Subsystem for Linux。 + + WSL 有着接近原生的性能、GPU 的支持和极易的安装方法,完整的 Linux 内核环境可以在涵盖绝大部分的 Linux 日常开发环境。 + +## 安装 {#install} + +### 自动安装方法 {#install-auto} + +在 Windows 10 Build 19041(Windows 10 2004)版本或以上,可以通过一条命令直接完成安装(需要以管理员身份运行): + +```shell +wsl --install +``` + +这条命令会完成如下几个操作: + +- 启用 WSL 和虚拟化平台 +- 下载并安装 WSL 的 Linux 内核 +- 设定 WSL 2 默认的版本 +- 从微软商店里面下载 Ubuntu 进行安装 + +安装完毕之后可以直接通过 `ubuntu` 命令启动。 + +### 手动安装 {#install-manual} + +如果系统版本在 Windows 10 Build 19041 以下,如 Windows 10 LTSC 2019,需要手动配置系统。 + +#### 启用 WSL 和虚拟化平台 {#enable-windows-features} + +使用管理员身份在 Powershell 中运行: + +```batch +Dism.exe /Online /Enable-feature /Featurename:Microsoft-Windows-Subsystem-Linux /All /NoRestart +Dism.exe /Online /Enable-feature /Featurename:VirtualMachinePlatform /All /NoRestart +``` + +#### 安装 Linux 内核 {#install-linux-kernel} + +从微软下载 Linux 内核用于 Windows 的更新,并根据机器架构选择安装: + +- [x64 内核](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)(绝大部分电脑) +- [ARM64 内核](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi)(使用高通等品牌 CPU 的电脑) + +#### 设置 WSL 2 为默认的 WSL 版本 {#set-default-wsl-version} + +```powershell +wsl --set-default-version 2 +``` + +## 安装发行版 {#install-distro} + +有许多的方法安装一个新的发行版。 + +### 从微软商店下载 {#install-distro-msstore} + +在微软商店中直接搜索对应的发行版进行安装即可。这种方法仅支持一部分发行版,如 Ubuntu 和 Debian 等。 + +![Microsoft Store Search Result](./images/ms-store-search-linux.png) + +图 1. Microsoft Store 搜素结果 +{: .caption } + +### 手动安装 {#install-distro-manual} + +有些发行版并未上架微软商店,但也有办法安装,比如可以参考下面这两个项目: + +- Manjaro: [ManjaroWSL2](https://github.com/sileshn/ManjaroWSL2) +- Gentoo: [GentooWSL2](https://github.com/VPraharsha03/GentooWSL2) + +但需要注意,这种安装方式并非发行版官方支持。 + +## 使用 {#usage} + +### VS Code {#vscode-remote-wsl} + +在 VS Code 中安装插件 "WSL" 后,可以像 "Remote - SSH" 一样使用 WSL,或者也可以直接安装 "Remote Development" 插件,里面包含了 "WSL" 插件。 + +![VS Code Store "WSL"](./images/vscode-store-remote-wsl.png) + +图 2. Visual Studio Code 搜索 "WSL" +{: .caption } + +### JetBrains {#jetbrains-remote-wsl} + +包括 IntelliJ IDEA、PyCharm、WebStorm 等,都有对 WSL 的支持。在打开了 WSL 下的目录后, +会自动地选择 WSL 下的工具链,Git 会使用 WSL 中安装的 Git 而不是 Window 下的 Git。 + +### 在 WSL 中使用资源管理器打开文件夹 {#open-folder-in-wsl} + +在 WSL 的终端中,直接输入 `explorer.exe .`,会在当前目录下打开资源管理器,也可以使用 Windows 上的应用程序打开相应的文件。 + +## 已知的问题 {#known-issues} + +### 运行 32 位应用程序 {#32-bit-exe} + +由于实现原理,WSL 1 并不支持运行 32 位应用程序。WSL 2 由于是完整的 Linux 内核,在安装了对应的运行时后可以运行 32 位应用程序。 + +### WSL 下 `/mnt/` 性能问题 {#mnt-performance} + +WSL 2 由于相当于是通过网络的方式访问 `/mnt/` 下的文件,导致性能相对于 WSL 1 有明显的下降。 + +可以通过将文件放在 `/` 下(不包括 `/mnt/`),来提高性能。 + +相关的 Issue 和评论:[microsoft/WSL#4197](https://github.com/microsoft/WSL/issues/4197#issuecomment-604592340) diff --git a/docs/Ch01/index.md b/docs/Ch01/index.md index 93dd0a3c..9affcfdb 100644 --- a/docs/Ch01/index.md +++ b/docs/Ch01/index.md @@ -213,6 +213,10 @@ Android TV 图标 因此,本书将主要讲解如何为自己搭建一个安全高效的 Linux 虚拟机。如果你有一台远程的 Linux 服务器,可以参考[拓展阅读](./supplement.md#ssh)的内容配置 SSH 连接。 +!!! info "WSL" + + 如今,对于 Windows 用户来说,使用 WSL 安装 Linux 或许会是更加便捷的方法,详情可以参考[附录](../Appendix/wsl.md)。 + ### 获取虚拟机管理软件 {#get-vm-softwares} 现在在 Windows / macOS 上主流的虚拟机管理软件有: diff --git a/docs/Ch10/index.md b/docs/Ch10/index.md deleted file mode 100644 index 945f6f32..00000000 --- a/docs/Ch10/index.md +++ /dev/null @@ -1,160 +0,0 @@ -# Windows Subsystem for Linux - -!!! Warning "本文仍在编辑中,尚未校对、审阅和和修订" - -!!! abstract "导言" - - 本章节将介绍在 Windows 上体验和使用 Linux 的较好的方法之一——Windows Subsystem for Linux。 - - WSL 有着接近原生的性能、GPU 的支持和极易的安装方法,完整的 Linux 内核环境可以在涵盖绝大部分的 Linux 日常开发环境。 - -## 安装 {#install} - -### 自动安装方法 {#install-auto} - -在 Windows 10 Build 19041(Windows 10 2004)版本或以上,可以通过一条命令直接完成安装(需要以管理员身份运行): - -```shell -wsl --install -``` - -这条命令会完成如下几个操作: - -- 启用 WSL 和虚拟化平台 -- 下载并安装 WSL 的 Linux 内核 -- 设定 WSL 2 默认的版本 -- 从微软商店里面下载 Ubuntu 进行安装 - -安装完毕之后可以直接通过 `ubuntu` 命令启动。 - -### 手动安装 {#install-manual} - -如果系统版本在 Windows 10 Build 19041 以下,如 Windows 10 LTSC 2019 ,需要手动配置系统。 - -#### 启用 WSL 和虚拟化平台 {#enable-windows-features} - -使用管理员身份在 Powershell 中运行 - -```batch -Dism.exe /Online /Enable-feature /Featurename:Microsoft-Windows-Subsystem-Linux /All /NoRestart -Dism.exe /Online /Enable-feature /Featurename:VirtualMachinePlatform /All /NoRestart -``` - -#### 安装 Linux 内核 {#install-linux-kernel} - -从微软下载 Linux 内核用于 Windows 的更新,并安装 - -[x64 内核](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi) - -[ARM64 内核](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi) - -#### 设置 WSL 2 为默认的 WSL 版本 {#set-default-wsl-version} - -```powershell -wsl --set-default-version 2 -``` - -## 安装发行版 {#install-distro} - -有许多的方法安装一个新的发行版。 - -### 从微软商店下载 {#install-distro-msstore} - -在微软商店中直接搜索对应的发行版进行安装即可,这种方法可以安装的发行版并不多,如 Arch Linux 和 Fedora 就无法通过这种方法进行安装。 - -![Microsoft Store Search Result](./images/ms-store-search-linux.png) - -### 使用 Appx 文件进行安装 {#install-distro-appx} - -有的打包为 Appx 的分发方式,也有提供了可执行应用程序来进行安装,在仓库里面提供了安装方式 - -- ArchLinux: [ArchWSL](https://github.com/yuk7/ArchWSL) -- Fedora: [FedoraWSL](https://github.com/yosukes-dev/FedoraWSL) -- Manjaro: [ManjaroWSL](https://github.com/sileshn/ManjaroWSL) -- Gentoo: [GentooWSL](https://github.com/imaandrew/GentooWSL) - -### 使用 LxRunOffline 进行安装 {#install-lxrunoffline} - -LxRunOffline 是进行 WSL 管理的一个应用程序,可以帮助管理 WSL 的安装与配置 - -LxRunOffline 仓库:[LxRunOffline](https://github.com/DDoSolitary/LxRunOffline) - -LxRunOffline 是通过 Windows 提供的 API 来进行管理 WSL 的一个命令行工具 - -#### 例:安装 Arch Linux {#install-lxrunoffline-archlinux} - -可以从 [LxRunOffline/wiki](https://github.com/DDoSolitary/LxRunOffline/wiki) 里面下载需要的发行版的 rootfs - -对于 ArchLinux,可以从[这个链接](https://lxrunoffline.apphb.com/download/ArchLinux),下载 ArchLinux 的 rootfs 得到 `archlinux-bootstrap-*.tar.gz` - -使用 LxRunOffline 进行安装 - -```shell -./LxRunOffline i -n ArchLinux -d D:/wsl/ArchLinux -f ./archlinux-bootstrap-*.tar.gz -``` - -- `-n` WSL 的名称 - -- `-d` 希望安装的位置,通过这种方法可以不需要将 wsl 安装在 C 盘 - -需要注意的是,这个安装方法不会在开始菜单中添加启动快捷方式,只能够通过命令行的方式启动: - -```powershell -wsl -d ArchLinux -``` - -在安装完成后打开,默认是以 root 的身份执行,如果需要默认以其他的用户的身份运行,可以在添加了用户后,使用 LxRunOffline 运行: - -```shell -./LxRunOffline su -n ArchLinux -v 1000 -``` - -其中: - -- `ArchLinux` 是 WSL 的名 -- `-v 1000` 是设置登录的用户的 UID - -## 使用 {#usage} - -### VS Code {#vscode-remote-wsl} - -在 VS Code 中安装插件 "Remote - WSL" 后,可以像 "Remote - SSH" 一样使用 WSL - -![VS Code Store "Remote - WSL"](./images/vscode-store-remote-wsl.png) - -### JetBrains {#jetbrains-remote-wsl} - -包括 IntelliJ IDEA、PyCharm、WebStorm 等,都有对 WSL 的支持。在打开了 WSL 下的目录后, -会自动地选择 WSL 下的工具链,Git 会使用 WSL 中安装的 Git 而不是 Window 下的 Git。 - -但是还有一些问题仍待解决,如 PyCharm 并不支持 WSL 中的虚拟环境,在添加 Python Inpterpreter 时, -无法检测并使用现有的如 `virtualenv`、`conda` 等创建的虚拟环境,也无法创建。 - -对应的的 Issue:[PY-32853](https://youtrack.jetbrains.com/issue/PY-32853) - -可以 workaround 来解决:在添加 Python Inpterpreter 时,添加 System Python Inpterpreter 而不是 -Virtual Environment,在路径中填写虚拟环境下的 Python 路径,PyCharm 可以正确识别安装的包,但是在打开 -终端的时候,不会自动地 `source` 虚拟环境。 - -### 在 WSL 中使用资源管理器打开文件夹 {#open-folder-in-wsl} - -在 WSL 的终端中,直接输入 `explorer.exe .`,会在当前目录下打开资源管理器,也可以使用 Windows 上的应用程序打开相应的文件。 - -## 已知的问题 {#known-issues} - -### 运行 32 位应用程序 {#32-bit-exe} - -由于实现原理,WSL 1 并不支持运行 32 位应用程序。WSL 2 由于是完整的 Linux 内核,可以在安装了对应的 -运行时后可以运行 32 位应用程序。 - -### WSL 下 `/mnt/` 性能问题 {#mnt-performance} - -WSL 2 由于相当于是通过网络的方式访问 `/mnt/` 下的文件,导致性能相对于 WSL 1 有明显的下降。 - -可以通过将文件放在 `/` 下(不包括 `/mnt/`),来提高性能。 - -相关的 Issue 和评论:[microsoft/WSL#4197](https://github.com/microsoft/WSL/issues/4197#issuecomment-604592340) - -### systemd {#systemd} - -无论是 WSL 1 还是 WSL 2,都不原生支持 systemd,如果需要 systemd,可以通过 [systemd-genie](https://github.com/arkane-systems/genie) 来实现。 diff --git a/docs/credits.md b/docs/credits.md index 61f6750b..ba436866 100644 --- a/docs/credits.md +++ b/docs/credits.md @@ -2,11 +2,11 @@ ## 主要贡献者 {#team} -项目主策划:[RiessarSadyx](https://github.com/RiessarSadyx) +项目主策划:[Riessarius](https://github.com/Riessarius) 各章主要作者: -- 第一章:[RiessarSadyx](https://github.com/RiessarSadyx) +- 第一章:[Riessarius](https://github.com/Riessarius) - 第二章:[LamWS](https://github.com/LamWS) - 第三章:[zeyugao](https://github.com/zeyugao) - 第四章:[psi-cmd](https://github.com/psi-cmd) diff --git a/mkdocs.yml b/mkdocs.yml index 9366aec0..1218a38f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -118,3 +118,4 @@ nav: - Markdown 教程: Appendix/markdown.md - man 文档的一些示例: Appendix/man.md - 其他的 Linux 发行版:技术差异简介: Appendix/distribution.md + - 使用 WSL 安装 Linux: Appendix/wsl.md