适用于 Linux 的 Windows 子系统安装指南 (Windows 10)Windows Subsystem for Linux Installation Guide for Windows 10

安装适用于 Linux 的 Windows 子系统Install the Windows Subsystem for Linux

必须先启用“适用于 Linux 的 Windows 子系统”可选功能,然后才能在 Windows 上安装 Linux 分发版。Before installing any Linux distributions on Windows, you must enable the "Windows Subsystem for Linux" optional feature.

以管理员身份打开 PowerShell 并运行:Open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

若要仅安装 WSL 1,现在应重启计算机并继续安装所选的 Linux 分发版,否则请等待重启并继续更新到 WSL 2。To only install WSL 1, you should now restart your machine and move on to Install your Linux distribution of choice, otherwise wait to restart and move on to update to WSL 2. 阅读有关比较 WSL 2 和 WSL 1 的详细信息。Read more about Comparing WSL 2 and WSL 1.

更新到 WSL 2Update to WSL 2

若要更新到 WSL 2,必须满足以下条件:To update to WSL 2, you must meet the following criteria:

启用“虚拟机平台”可选组件Enable the 'Virtual Machine Platform' optional component

安装 WSL 2 之前,必须启用“虚拟机平台”可选功能。Before installing WSL 2, you must enable the "Virtual Machine Platform" optional feature.

以管理员身份打开 PowerShell 并运行:Open PowerShell as Administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

重新启动计算机,以完成 WSL 安装并更新到 WSL 2。Restart your machine to complete the WSL install and update to WSL 2.

将 WSL 2 设置为默认版本Set WSL 2 as your default version

安装新的 Linux 分发版时,请在 PowerShell 中运行以下命令,以将 WSL 2 设置为默认版本:Run the following command in PowerShell to set WSL 2 as the default version when installing a new Linux distribution:

wsl --set-default-version 2

运行该命令后,你可能会看到此消息:WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernelYou might see this message after running that command: WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel. 跟随链接(https://aka.ms/wsl2kernel),在文档中安装来自该页面的 MSI,以便在计算机上安装 Linux 内核供 WSL 2 使用。Please follow the link (https://aka.ms/wsl2kernel) and install the MSI from that page on our documentation to install a Linux kernel on your machine for WSL 2 to use. 安装内核后,请再次运行该命令,该命令应会成功完成而不显示消息。Once you have the kernel installed, please run the command again and it should complete successfully without showing the message.

备注

从 WSL 1 更新到 WSL 2 可能需要几分钟才能完成,具体取决于目标分发版的大小。The update from WSL 1 to WSL 2 may take several minutes to complete depending on the size of your targeted distribution. 如果从 Windows 10 周年更新或创意者更新运行 WSL 1 的旧(历史)安装,可能会遇到更新错误。If you are running an older (legacy) installation of WSL 1 from Windows 10 Anniversary Update or Creators Update, you may encounter an update error. 按照这些说明卸载并删除任何旧分发Follow these instructions to uninstall and remove any legacy distributions.

安装所选的 Linux 分发版Install your Linux distribution of choice

  1. 打开 Microsoft Store,并选择你偏好的 Linux 分发版。Open the Microsoft Store and select your favorite Linux distribution.

    Microsoft Store 中的 Linux 分发版的视图

    单击以下链接会打开每个分发版的 Microsoft Store 页面:The following links will open the Microsoft store page for each distribution:

  2. 在分发版的页面中,选择“获取”。From the distribution's page, select "Get".

    Microsoft Store 中的 Linux 分发版

设置新分发版Set up a new distribution

首次启动新安装的 Linux 分发版时,将打开一个控制台窗口,系统会要求你等待一分钟或两分钟,以便文件解压缩并存储到电脑上。The first time you launch a newly installed Linux distribution, a console window will open and you'll be asked to wait for a minute or two for files to de-compress and be stored on your PC. 未来的所有启动时间应不到一秒。All future launches should take less than a second.

然后,需要为新的 Linux 分发版创建用户帐户和密码You will then need to create a user account and password for your new Linux distribution.

Windows 控制台中的 Ubuntu 解包

将分发版版本设置为 WSL 1 或 WSL 2Set your distribution version to WSL 1 or WSL 2

可以打开 PowerShell 命令行并输入以下命令(仅在 Windows 内部版本 19041 或更高版本中可用),来检查分配给每个已安装的 Linux 分发版的 WSL 版本:wsl -l -vYou can check the WSL version assigned to each of the Linux distributions you have installed by opening the PowerShell command line and entering the command (only available in Windows Build 19041 or higher): wsl -l -v

wsl --list --verbose

若要将分发版设置为受某一 WSL 版本支持,请运行:To set a distribution to be backed by either version of WSL please run:

wsl --set-version <distribution name> <versionNumber>

请确保将 <distribution name> 替换为你的分发版的实际名称,并将 <versionNumber> 替换为数字“1”或“2”。Make sure to replace <distribution name> with the actual name of your distribution and <versionNumber> with the number '1' or '2'. 可以随时更改回 WSL 1,方法是运行与上面相同的命令,但将“2”替换为“1”。You can change back to WSL 1 at anytime by running the same command as above but replacing the '2' with a '1'.

此外,如果要使 WSL 2 成为你的默认体系结构,可以通过此命令执行该操作:Additionally, if you want to make WSL 2 your default architecture you can do so with this command:

wsl --set-default-version 2

这会将安装的任何新分发版的版本设置为 WSL 2。This will set the version of any new distribution installed to WSL 2.

排查安装问题Troubleshooting installation

下面是相关的错误和建议的修复措施。Below are related errors and suggested fixes. 有关其他常见错误及其解决方法,请参阅 WSL 故障排除页Refer to the WSL troubleshooting page for other common errors and their solutions.

  • 安装失败并出现错误 0x80070003Installation failed with error 0x80070003

    • 适用于 Linux 的 Windows 子系统只能在系统驱动器(通常是 C: 驱动器)中运行。The Windows Subsystem for Linux only runs on your system drive (usually this is your C: drive). 请确保分发版存储在系统驱动器上:Make sure that distributions are stored on your system drive:
    • 打开“设置”->“存储”->“更多存储设置: 更改新内容的保存位置” 用于在 C: 驱动器中安装应用的系统设置屏幕截图Open Settings -> Storage -> More Storage Settings: Change where new content is saved Picture of system settings to install apps on C: drive
  • WslRegisterDistribution 失败并出现错误 0x8007019eWslRegisterDistribution failed with error 0x8007019e

    • 未启用“适用于 Linux 的 Windows 子系统”可选组件:The Windows Subsystem for Linux optional component is not enabled:
    • 打开“控制面板” -> “程序和功能” -> “打开或关闭 Windows 功能”-> 选中“适用于 Linux 的 Windows 子系统”,或使用本文开头所述的 PowerShell cmdlet。 Open Control Panel -> Programs and Features -> Turn Windows Feature on or off -> Check Windows Subsystem for Linux or using the PowerShell cmdlet mentioned at the beginning of this article.
  • 安装失败,出现错误 0x80070003 或错误 0x80370102Installation failed with error 0x80070003 or error 0x80370102

    • 请确保在计算机的 BIOS 内已启用虚拟化。Please make sure that virtualization is enabled inside of your computer's BIOS. 有关如何执行此操作的说明因计算机而异,并且很可能在 CPU 相关选项下。The instructions on how to do this will vary from computer to computer, and will most likely be under CPU related options.
  • 尝试升级时出错:Invalid command line option: wsl --set-version Ubuntu 2Error when trying to upgrade: Invalid command line option: wsl --set-version Ubuntu 2

    • 请确保已启用适用于 Linux 的 Windows 子系统,并且你使用的是 Windows 内部版本 19041 或更高版本。Please make sure that you have the Windows Subsystem for Linux enabled, and that you're using Windows Build version 19041 or higher. 若要启用 WSL,请在 PowerShell 提示符下以具有管理员权限的身份运行此命令:Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-LinuxTo enable WSL run this command in a PowerShell prompt with admin privileges: Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux. 可在此处找到完整的 WSL 安装说明。You can find the full WSL install instructions here.
  • 由于虚拟磁盘系统的某个限制,无法完成所请求的操作。虚拟硬盘文件必须是解压缩的且未加密的,并且不能是稀疏的。The requested operation could not be completed due to a virtual disk system limitation. Virtual hard disk files must be uncompressed and unencrypted and must not be sparse.

  • 无法将词语“wsl”识别为 cmdlet、函数、脚本文件或可运行程序的名称。The term 'wsl' is not recognized as the name of a cmdlet, function, script file, or operable program.