Windows 10 上適用於 Linux 的 Windows 子系統安裝指南Windows Subsystem for Linux Installation Guide for Windows 10

有兩個選項可用來安裝 Windows 子系統 Linux 版 (WSL):There are two options available for installing Windows Subsystem for Linux (WSL):

  • 簡化的安裝 (預覽版本):wsl --installSimplified install (preview release): wsl --install

    wsl --install 簡化的安裝命令需要加入 Windows 測試人員計畫,並安裝 Windows 10 的預覽組建 (OS 組建 20262 或更新版本),但不需要遵循手動安裝步驟。The wsl --install simplified install command requires that you join the Windows Insiders Program and install a preview build of Windows 10 (OS build 20262 or higher), but eliminates the need to follow the manual install steps. 您只需要使用系統管理員權限來開啟命令列視窗,然後執行 wsl --install,在重新開機之後,您就可以開始使用 WSL。All you need to do is open a command window with administrator privileges and run wsl --install, after a restart you will be ready to use WSL.

  • 手動安裝 :請遵循下列六個步驟。Manual install: Follow the six steps listed below.

    適用於 WSL 的手動安裝步驟如下所列,可用來在任何版本的 Windows 10 上安裝 Linux。The manual install steps for WSL are listed below and can be used to install Linux on any version of Windows 10.

Windows 測試人員的簡化安裝Simplified Installation for Windows Insiders

Windows 子系統 Linux 版的安裝程序已在 Windows 10 最新的 Windows 測試人員預覽組建中大幅改進,以單一命令取代下列手動步驟。The installation process for Windows Subsystem for Linux has been significantly improved in the latest Windows Insiders preview builds of Windows 10, replacing the manual steps below with a single command.

若要使用 wsl --install 簡化的安裝命令,您必須:In order to use the wsl --install simplified install command, you must:

  • 加入 Windows 測試人員計畫Join the Windows Insiders Program
  • 安裝 Windows 10 的預覽組建 (OS 組建 20262 或更新版本)。Install a preview build of Windows 10 (OS build 20262 or higher).
  • 使用系統管理員權限開啟命令列視窗Open a command line windows with Administrator privileges

符合這些需求之後,就可以安裝 WSL:Once those requirements are met, to install WSL:

  • 在您以系統管理員模式開啟的命令列中,輸入此命令:wsl.exe --installEnter this command in the command line you've opened in Admin mode: wsl.exe --install
  • 重新啟動您的電腦Restart your machine

當您第一次啟動新安裝的 Linux 發行版本時,主控台視窗隨即開啟,而系統會要求您等候檔案解壓縮並儲存在您的電腦上。The first time you launch a newly installed Linux distribution, a console window will open and you'll be asked to wait 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 作業系統完全整合的 Linux 發行版本!CONGRATULATIONS! You've successfully installed and set up a Linux distribution that is completely integrated with your Windows operating system!

--install 命令會執行下列動作:The --install command performs the following actions:

  • 啟用選用的 WSL 和虛擬機器平台元件Enables the optional WSL and Virtual Machine Platform components
  • 下載並安裝最新的 Linux 核心Downloads and installs the latest Linux kernel
  • 將 WSL 2 設定為預設值Sets WSL 2 as the default
  • 下載並安裝 Linux 散發套件 (可能需要重新開機)Downloads and installs a Linux distribution (reboot may be required)

根據預設,已安裝的 Linux 散發套件會是 Ubuntu。By default, the installed Linux distribution will be Ubuntu. 這可以使用 wsl --install -d <Distribution Name> 進行變更。This can be changed using wsl --install -d <Distribution Name>. (使用您想要的散發套件名稱取代 <Distribution Name>。) 使用 wsl --install -d <Distribution Name> 命令初始安裝之後,可能會將額外的 Linux 散發套件新增至您的電腦。(Replacing <Distribution Name> with the name of your desired distribution.) Additional Linux distributions may be added to your machine after the initial install using the wsl --install -d <Distribution Name> command.

若要查看可用的 Linux 散發套件清單,請輸入 wsl --list --onlineTo see a list of available Linux distributions, enter wsl --list --online.

手動安裝步驟Manual Installation Steps

如果您不是使用 Windows 測試人員組建,則必須遵循下列步驟,手動啟用 WSL 所需的功能。If you are not on a Windows Insiders build, the features required for WSL will need to be enabled manually following the steps below.

步驟 1 - 啟用 Windows 子系統 Linux 版Step 1 - Enable the Windows Subsystem for Linux

您必須先啟用「Windows 子系統 Linux 版」選用功能,然後才能在 Windows 上安裝任何 Linux 發行版本。You must first enable the "Windows Subsystem for Linux" optional feature before installing any Linux distributions on Windows.

以系統管理員身分開啟 PowerShell 並執行:Open PowerShell as Administrator and run:

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

我們現在建議您移至步驟 #2,更新至 WSL 2,但如果您只想要安裝 WSL 1,則現在可以 重新啟動 電腦,並移至 步驟 6 - 安裝選擇的 Linux 發行版本We recommend now moving on to step #2, updating to WSL 2, but if you wish to only install WSL 1, you can now restart your machine and move on to Step 6 - Install your Linux distribution of choice. 若要更新至 WSL 2,請 等候重新啟動 您的電腦,並移至下一個步驟。To update to WSL 2, wait to restart your machine and move on to the next step.

步驟 2 - 檢查執行 WSL 2 的需求Step 2 - Check requirements for running WSL 2

若要更新至 WSL 2,您必須執行 Windows 10。To update to WSL 2, you must be running Windows 10.

  • 若為 X64 系統:版本 1903 或更高版本,含 組建 18362 或更高組建。For x64 systems: Version 1903 or higher, with Build 18362 or higher.
  • 若為 ARM64 系統:版本 2004 或更高版本,含 組建 19041 或更高組建。For ARM64 systems: Version 2004 or higher, with Build 19041 or higher.
  • 低於 18362 的組建不支援 WSL 2。Builds lower than 18362 do not support WSL 2. 使用 Windows 更新小幫手來更新您的 Windows 版本。Use the Windows Update Assistant to update your version of Windows.

若要檢查您的版本及組建號碼,請選取 [Windows 標誌鍵 + R]、輸入 winver,然後選取 [確定]。To check your version and build number, select Windows logo key + R, type winver, select OK. (或在 Windows 命令提示字元中輸入 ver 命令)。(Or enter the ver command in Windows Command Prompt). 在 [設定] 功能表中更新至最新的 Windows 版本Update to the latest Windows version in the Settings menu.

注意

如果您執行的是 Windows 10 版本1903 或 1909,請從 Windows 功能表中開啟 [設定]、瀏覽至 [更新與安全性],然後選取 [檢查更新]。If you are running Windows 10 version 1903 or 1909, open "Settings" from your Windows menu, navigate to "Update & Security" and select "Check for Updates". 您的組建編號必須是 18362.1049+ 或 18363.1049+,次要組建號碼為 1049。Your Build number must be 18362.1049+ or 18363.1049+, with the minor build # over .1049. 閱讀更多內容:Windows 10 版本 1903 和 1909 即將提供 WSL 2 支援Read more: WSL 2 Support is coming to Windows 10 Versions 1903 and 1909. 請參閱疑難排解指示See the troubleshooting instructions.

步驟 3 - 啟用虛擬機器功能Step 3 - Enable Virtual Machine feature

在安裝 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.

步驟 4 - 下載 Linux 核心更新套件Step 4 - Download the Linux kernel update package

  1. 下載最新套件:Download the latest package:

    注意

    如果您使用的是 ARM64 電腦,請改為下載 ARM64 套件If you're using an ARM64 machine, please download the ARM64 package instead. 如果您不確定您有何種類型的電腦,請開啟命令提示字元或 PowerShell,然後輸入:systeminfo | find "System Type"If you're not sure what kind of machine you have, open Command Prompt or PowerShell and enter: systeminfo | find "System Type".

  2. 請執行在上一個步驟中下載的更新套件。Run the update package downloaded in the previous step. (按兩下以執行 - 系統會提示您提供更高的權限,請選取 [是] 以核准此安裝作業。)(Double-click to run - you will be prompted for elevated permissions, select ‘yes’ to approve this installation.)

一旦完成安裝,就會移至下一個步驟 - 在安裝新的 Linux 發行版本時,將 WSL 2 設定為預設版本。Once the installation is complete, move on to the next step - setting WSL 2 as your default version when installing new Linux distributions. (如果您想要將新的 Linux 安裝設定為 WSL 1,請略過此步驟)。(Skip this step if you want your new Linux installs to be set to WSL 1).

注意

如需詳細資訊,請參閱 Windows 命令列部落格上的關於更新 WSL2 Linux 核心的變更一文。For more information, read the article changes to updating the WSL2 Linux kernel, available on the Windows Command Line Blog.

步驟 5 - 將 WSL 2 設定為預設版本Step 5 - Set WSL 2 as your default version

在安裝新的 Linux 發行版本時,開啟 PowerShell,然後執行下列命令,以將 WSL 2 設定為預設版本:Open PowerShell and run this command to set WSL 2 as the default version when installing a new Linux distribution:

wsl --set-default-version 2

步驟 6 - 安裝您選擇的 Linux 發行版本Step 6 - 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 發行版本

當您第一次啟動新安裝的 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 解壓縮

恭喜!您已成功安裝並設定與 Windows 作業系統完全整合的 Linux 發行版本!CONGRATULATIONS! You've successfully installed and set up a Linux distribution that is completely integrated with your Windows operating system!

安裝 Windows 終端機 (選用)Install Windows Terminal (optional)

Windows 終端機可啟用多個索引標籤 (在多個 Linux 命令列、Windows 命令提示字元、PowerShell、Azure CLI 等之間快速切換)、建立自訂按鍵繫結 (開啟或關閉索引標籤、複製+貼上等的快速鍵)、使用搜尋功能及自訂佈景主題 (色彩配置、字型樣式和大小、背景影像/柔邊/透明度)。Windows Terminal enables multiple tabs (quickly switch between multiple Linux command lines, Windows Command Prompt, PowerShell, Azure CLI, etc), create custom key bindings (shortcut keys for opening or closing tabs, copy+paste, etc.), use the search feature, and custom themes (color schemes, font styles and sizes, background image/blur/transparency). 深入了解。Learn more.

安裝 Windows 終端機Install Windows Terminal.

Windows 終端機

將發行版本設定為 WSL 1 或 WSL 2Set your distribution version to WSL 1 or WSL 2

若要針對每個已安裝的 Linux 發行版本檢查獲派的 WSL 版本,您可以開啟 PowerShell 命令列並輸入此命令 (僅適用於 Windows 組建 18362 或更高版本):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 18362 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> 取代為發行版本的實際名稱,並使用數字 '1' 或 '2' 取代 <versionNumber>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 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 年度更新版或 Creators Update 執行較舊 (舊版) 的 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.

如果 wsl --set-default-version 結果為無效的命令,請輸入 wsl --helpIf wsl --set-default-version results as an invalid command, enter wsl --help. 如果未列出 --set-default-version,則表示您的作業系統不支援,則您必須更新至版本 1903,組建 18362 或更高版本。If the --set-default-version is not listed, it means that your OS doesn't support it and you need to update to version 1903, Build 18362 or higher.

如果您在執行命令之後看到此訊息:WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernelIf you see this message after running the command: WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel. 您仍需要安裝 MSI Linux 核心更新套件。You still need to install the MSI Linux kernel update package.

此外,如果您要讓 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 -> **System --> 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 功能] -> 勾選 [Windows 子系統 Linux 版] ,或使用本文開頭所述的 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 組建 18362 或更高版本。Enure that you have the Windows Subsystem for Linux enabled, and that you're using Windows Build version 18362 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.
  • 由於虛擬磁碟系統限制,所以要求的作業無法完成。虛擬硬碟檔案必須是未壓縮和未加密的檔案,而且不得為疏鬆檔案。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.

    • 開啟 Linux 散發套件的設定檔資料夾,取消選取 [壓縮內容] (若已核取,同時取消選取 [加密內容])。Deselect “Compress contents” (as well as “Encrypt contents” if that’s checked) by opening the profile folder for your Linux distribution. 其應該位於 Windows 檔案系統上的資料夾中,如下所示:USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited...It should be located in a folder on your Windows file system, something like: USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited...
    • 在此 Linux 散發版本設定檔中,應該會有 LocalState 資料夾。In this Linux distro profile, there should be a LocalState folder. 在此資料夾上按一下滑鼠右鍵,以顯示選項的功能表。Right-click this folder to display a menu of options. 選取 [屬性] > [進階],然後確定未選取 [壓縮內容以節省磁碟空間] 和 [加密內容以保護資料] 核取方塊 (未核取)。Select Properties > Advanced and then ensure that the “Compress contents to save disk space” and “Encrypt contents to secure data” checkboxes are unselected (not checked). 如果系統詢問您是否只是將此套用到目前的資料夾或套用至所有子資料夾和檔案,請選取 [只是這個資料夾],因為您只是清除壓縮旗標。If you are asked whether to apply this to just to the current folder or to all subfolders and files, select “just this folder” because you are only clearing the compress flag. 在此之後,wsl --set-version 命令應該能運作。After this, the wsl --set-version command should work.

WSL 散發版本屬性設定的螢幕擷取畫面

注意

在我的案例中,我的 Ubuntu 18.04 散發套件的 LocalState 資料夾位於 C:\Users<my-user-name>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgscIn my case, the LocalState folder for my Ubuntu 18.04 distribution was located at C:\Users<my-user-name>\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc

請檢查 WSL Docs GitHub 執行緒 #4103 (英文),其中會追蹤此問題是否有更新資訊。Check WSL Docs GitHub thread #4103 where this issue is being tracked for updated information.

  • 無法辨識 'wsl' 詞彙是否為 Cmdlet、函式、指令檔或可執行程式的名稱。The term 'wsl' is not recognized as the name of a cmdlet, function, script file, or operable program.

  • 錯誤:此更新僅適用於具有 Windows 子系統 Linux 版的機器。Error: This update only applies to machines with the Windows Subsystem for Linux.

    • 若要安裝 Linux 核心更新 MSI 套件,需要 WSL,且應該先將其啟用。To install the Linux kernel update MSI package, WSL is required and should be enabled first. 如果失敗,則會看到下列訊息:This update only applies to machines with the Windows Subsystem for LinuxIf it fails, it you will see the message: This update only applies to machines with the Windows Subsystem for Linux.
    • 會看到此訊息的三個可能原因:There are three possible reason you see this message:
    1. 您仍在使用舊版的 Windows,而該版本不支援 WSL 2。You are still in old version of Windows which doesn't support WSL 2. 如需版本需求和更新的連結,請參閱步驟 #2。See step #2 for version requirements and links to update.

    2. WSL 未啟用。WSL is not enabled. 您將需要返回步驟 #1,並確定已在您的機器上啟用選用的 WSL 功能。You will need to return to step #1 and ensure that the optional WSL feature is enabled on your machine.

    3. 啟用 WSL 之後,必須重新開機才會生效。請重新開機,然後再試一次。After you enabled WSL, a reboot is required for it to take effect, reboot your machine and try again.

  • 錯誤:WSL 2 需要更新其核心元件。如需詳細資訊,請造訪 https://aka.ms/wsl2kernelError: WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel .

    • 如果 %SystemRoot%\system32\lxss\tools 資料夾中缺少 Linux 核心套件,您將會遇到此錯誤。If the Linux kernel package is missing in the %SystemRoot%\system32\lxss\tools folder, you will encounter this error. 若要解決此問題,請在這些安裝指示的步驟 #4 中安裝 Linux 核心更新 MSI 套件。Resolve it by installing the Linux kernel update MSI package in step #4 of these installation instructions. 您可能需要從 [新增或移除程式] 解除安裝 MSI,然後重新安裝。You may need to uninstall the MSI from 'Add or Remove Programs', and install it again.