This tutorial demonstrates how to install, manage, and use a full Linux environment on your Windows 10 PC with WSL2. You’ll be running a Linux kernel and executing native applications while seamlessly sharing files and services with Windows code editors and browsers. 本教程演示了如何在具有WSL2的Windows 10 PC上安装,管理和使用完整的Linux环境。 您将运行Linux内核并执行本机应用程序,同时与Windows代码编辑器和浏览器无缝共享文件和服务。 为什么要使用Linux? (Why Use Linux?)Your host almost certainly uses Linux. It’s fast, open source, and runs the majority of web software including: 您的主机几乎可以肯定使用Linux。 它是快速,开放源代码,并且运行大多数Web软件,包括:
Some of these dependencies may be available on Windows and macOS, but installing and maintaining identical versions is difficult. They’re often slower and you may encounter subtle differences which will not become apparent until you deploy. 其中一些依赖项可能在Windows和macOS上可用,但是很难安装和维护相同的版本。 它们通常较慢,并且您可能会遇到细微的差异,直到您进行部署,这些差异才会变得明显。 Some developers install Linux on their desktop, but that may not be viable if you regularly require non-Linux software such as Microsoft or Adobe products. Alternative options: 一些开发人员在其桌面上安装Linux,但是如果您经常需要非Linux软件(例如Microsoft或Adobe产品),则可能不可行。 替代选项:
什么是Linux的Windows子系统? (What is the Windows Subsystem for Linux?)The Windows Subsystem for Linux (WSL) lets developers run a Linux environment directly on Windows 10. It’s an unmodified OS, but highly integrated into Windows and without the overhead of a virtual machine. Windows Linux子系统(WSL)使开发人员可以在Windows 10上直接运行Linux环境。这是未经修改的OS,但高度集成到Windows中,并且没有虚拟机的开销。 WSL1 was groundbreaking and translated Linux system calls to Windows equivalents. This could be slow and several features weren’t available. WSL2 uses Hyper-V virtual machine technology so Linux compatibility is excellent and file access is up to 20x faster than before. WSL1具有开创性,并将Linux系统调用转换为Windows等效项。 这可能很慢,并且某些功能不可用。 WSL2使用Hyper-V虚拟机技术,因此Linux兼容性出色,文件访问速度比以前快20倍。 Windows 10要求 (Windows 10 Requirements)WSL2 is compatible with the Home, Pro, or Server editions of Windows but not Windows 10 S (although you can often upgrade to the Home edition for free). WSL2与Windows的Home,Pro或Server版本兼容,但与Windows 10 S 不兼容(尽管您通常可以免费升级到Home版本)。 The Windows May 2020 update is essential — that’s version 2004 or above. Choose the Settings cog from the Start menu, then select System followed by About. The Windows specifications are shown at the bottom of the panel: Windows 2020年5月更新非常重要-版本2004或更高版本。 从开始菜单中选择设置齿轮,然后选择系统 ,然后选择关于 。 Windows规格显示在面板底部: ![]() The update has been slowly rolling out since the end of May 2020. If you have a version below 2004, you may be able to trigger the update by clicking the Check for updates button in Settings, then Update & Security. 更新自2020年五月的结束已经慢慢铺开如果你有2004下方的版本,您可以通过点击设置 检查更新按钮,则更新和安全触发更新。 If 2004 remains stubbornly unavailable, you may be able to download and install it manually from microsoft.com/software-download/windows10 如果2004年仍然无法使用,则可以从microsoft.com/software-download/windows10手动下载并安装它 However, be aware some PCs report that version 2004 is currently unavailable in the Windows Update panel. You won’t be able to upgrade until Microsoft has released a fix for your device. 但是,请注意,有些PC报告Windows Update面板中当前不提供2004版。 在Microsoft为您的设备发布修复程序之前,您将无法升级。 电脑要求 (PC Requirements)WSL2 (and Hyper-V) require hardware virtualization support to be enabled in your BIOS. WSL2(和Hyper-V)要求在您的BIOS中启用硬件虚拟化支持。 WARNING: fiddling with your BIOS settings can trash your PC! Be careful when making changes. Consult your manufacturer’s help pages or search for online advice about your specific make and model. 警告: 摆弄BIOS设置可能会损坏您的PC! 进行更改时要小心。 请查阅制造商的帮助页面,或搜索有关特定品牌和型号的在线建议。 Hardware virtualization will be active on most devices, but you can check by rebooting, and opening the BIOS panels — typically by hitting the DEL, F2, or F10 key as the system starts. Look for Virtualization Technology, VTx or similar options. Ensure they are enabled, save, and reboot the device. 硬件虚拟化将在大多数设备上处于活动状态,但是您可以通过重新启动并打开BIOS面板进行检查-通常在系统启动时按DEL , F2或F10键。 寻找虚拟化技术 , VTx或类似选项。 确保已启用它们,保存并重新启动设备。 Finally, ensure your 最后,确保您的 At the time of writing, WSL2 will install Linux to your 在撰写本文时,WSL2会将Linux安装到您的 启用WSL2 (Enable WSL2)The Virtual Machine Platform and Windows Subsystem for Linux features must be enabled in Turn Windows features on or off. The panel can be accessed by hitting Start and typing “features” or from the Programs and Features icon in the classic Control Panel. 必须在“ 打开或关闭Windows功能”中启用适用于Linux的虚拟机平台和Windows子系统功能。 可以通过单击开始并键入“功能”或从经典控制面板中的“ 程序和功能”图标来访问该面板。 ![]() Alternatively, you can execute the following commands in a Windows Powershell terminal run as an Administrator: 或者,您可以在以管理员身份运行的Windows Powershell终端中执行以下命令:
Reboot Windows 10, then enable WSL2 as the default by entering the following command in a Windows Powershell or command prompt: 重新启动Windows 10,然后在Windows Powershell或命令提示符中输入以下命令,将WSL2设置为默认值:
下载Linux (Download Linux)You can install any number of Linux distros from the Microsoft Store accessed in the Start menu. This includes Ubuntu, Debian, SUSE, Kali, and Alpine. Enter “Linux” in the search box: 您可以从“开始”菜单中访问的Microsoft Store安装任何数量的Linux发行版。 其中包括Ubuntu,Debian,SUSE,Kali和Alpine。 在搜索框中输入“ Linux” : ![]() Ubuntu is a good choice unless you have specific requirements. The latest edition will be installed if you click the Ubuntu icon followed the Get button. 除非您有特定要求,否则Ubuntu是一个不错的选择。 如果单击“ 获取”按钮后的Ubuntu图标,将安装最新版本。 The download could take a while depending on your network speed. 下载可能需要一段时间,具体取决于您的网络速度。 启动Linux (Launch Linux)Following download, click the Launch button on the Microsoft Store app, or the new Ubuntu icon in the Start menu. This will complete the installation process and can take several minutes. 下载后,单击Microsoft Store应用程序上的“ 启动”按钮,或单击“开始”菜单中的新Ubuntu图标。 这将完成安装过程,可能需要几分钟。 Note: the first time you launch a WSL2 distro, you may see a message in the terminal about a kernel update. Copy the link into your browser, then download and install the update. You will need to launch Ubuntu again to continue with the installation. 注意:第一次启动WSL2发行版时,您可能会在终端中看到有关内核更新的消息。 将链接复制到浏览器中,然后下载并安装更新。 您将需要再次启动Ubuntu才能继续安装。 You’ll be prompted to enter a username and password. These are the credentials for Linux administration: they are completely separate from your Windows username and password (although choosing the same ones may be practical). 系统将提示您输入用户名和密码。 这些是Linux管理的凭据: 它们与Windows用户名和密码完全分开 (尽管选择相同的用户名和密码是可行的)。 Linux will eventually be ready and your terminal will show content similar to this: Linux最终将准备就绪,您的终端将显示类似以下内容:
更新Linux (Update Linux)There are likely to be several Linux updates. To update Ubuntu, enter the following commands in the Linux terminal: 可能会有几个Linux更新。 要更新Ubuntu,请在Linux终端中输入以下命令:
Other Linux distros will have a similar process, but check the documentation for assistance. 其他Linux发行版将具有类似的过程,但请查看文档以获取帮助。 在WSL1和WSL2之间切换 (Switch Between WSL1 and WSL2)Existing WSL1 distros can be converted to WSL2. Entering the following command in a Windows Powershell terminal to display the installed Linux distros and their WSL version: 现有的WSL1发行版可以转换为WSL2。 在Windows Powershell终端中输入以下命令以显示已安装的Linux发行版及其WSL版本:
(The * asterisk highlights the default Linux installation.) (*号突出显示了默认的Linux安装。) To switch 要将
Similarly, to switch back to WSL1, enter: 同样,要切换回WSL1,请输入: wsl --set-version Ubuntu 1 设置默认的Linux发行版 (Set a Default Linux Distribution)When you have multiple Linux distributions installed, one must be set as the default. It will be used when 当您安装了多个Linux发行版时,必须将其设置为默认发行版。 在Windows Powershell终端中输入 To set the default distro, list your installations: 要设置默认发行版,请列出您的安装:
and set a default with: 并设置默认值: wsl --setdefault <DistributionName> Ubuntu example: Ubuntu示例: 以特定用户身份运行Linux (Run Linux as a Specific User)To run your default distribution as a specific user, enter the following command in a Windows Powershell terminal: 要以特定用户身份运行默认分发,请在Windows Powershell终端中输入以下命令:
A default user is defined during installation and you can create others. A 在安装过程中定义了一个默认用户,您可以创建其他用户。 还创建了一个 移动您的Linux磁盘映像 (Move Your Linux Disk Image)The Linux disk image is installed on your Linux磁盘映像安装在 In a Windows Powershell terminal (not the Ubuntu terminal), enter 在Windows Powershell终端( 不是 Ubuntu终端)中,输入
Unregister the same distribution to remove it from the 注销相同的发行版以将其从
Enter 输入 Import the backup into a new WSL2 distribution at another location, such as 将备份导入另一个位置的新WSL2发行版,例如
Verify it has been successfully created by entering 通过输入 Unfortunately, Ubuntu will now use 不幸的是,Ubuntu现在将使用
where 其中 Presuming all has gone well, you can safely delete the backup file ( 假设一切顺利,您可以安全地删除备份文件( 安装Windows Terminal (Install Windows Terminal)To launch the Linux terminal, you can use the Ubuntu icon, enter 要启动Linux终端,可以使用Ubuntu图标,在Powershell中输入 Another option is Windows Terminal, which is also available from the Microsoft Store or its repository at github.com/microsoft/terminal/. 另一个选项是Windows Terminal ,它也可以从Microsoft Store或其github.com/microsoft/terminal/的存储库中获得 。 ![]() Windows Terminal will automatically add your WSL2 Linux distros and offers a highly configurable range of options including tabs, split views, themes, transparency, and key bindings. Windows Terminal会自动添加您的WSL2 Linux发行版,并提供高度可配置的选项范围,包括选项卡,拆分视图,主题,透明度和键绑定。 Options are defined in the 选项在定义 If you want the complete in-depth guide to installing, configuring, and using Windows Terminal, we’ve got you covered. 如果您想获得有关安装,配置和使用Windows Terminal的完整的深入指南,我们将帮助您。 ➤ The Complete Guide to Windows Terminal ➤Windows 终端机完整指南 从Windows访问Linux文件 (Accessing Linux Files from Windows)Linux files can be accessed at the network path 可以在网络路径 Your installed Linux distros are listed, so you can access the Ubuntu root directory at 列出了已安装的Linux发行版,因此您可以访问 \\wsl$\Ubuntu\home\<yourname> It’s best to use this as the starting folder in Windows Terminal. Open the Settings, and add the following configuration line to the Ubuntu profile: 最好将其用作Windows Terminal中的起始文件夹。 打开“ 设置” ,然后将以下配置行添加到Ubuntu配置文件:
where 其中 映射网络驱动器 (Map a Network Drive)You can mount a network drive to 您可以通过在文件资源管理器中打开 ![]() Unfortunately, it’s not currently possible to map deeper subdirectories. You may also encounter mapped-drive issues with some applications. 不幸的是,当前无法映射更深的子目录。 您可能还会在某些应用程序中遇到映射驱动器问题。 从Linux访问Windows文件 (Accessing Windows Files from Linux)Windows drives are mounted in the Linux Windows驱动器安装在Linux /mnt/c/Users/<yourname> For ease of access, you can create a symbolic link to any Windows folder from the Ubuntu terminal, e.g. for 为了便于访问,您可以从Ubuntu终端创建指向任何Windows文件夹的符号链接,例如
A Accessing Windows files from Linux is considerably slower than using the native Linux file system. Where possible, create projects within the Linux file space, typically within your home folder ( 从Linux访问Windows文件比使用本机Linux文件系统要慢得多。 尽可能在Linux文件空间中创建项目,通常在您的主文件夹( 从Windows运行Linux命令 (Running Linux Commands from Windows)Any Linux (bash) shell command can be run from a Windows Powershell or command line terminal using 可以使用 wsl <linux-command> For example: 例如: 从Linux运行Windows应用程序 (Running Windows Applications from Linux)Any Windows executable can be launched from Linux (it’s normally necessary to specify the 可以从Linux启动任何Windows可执行文件(通常需要指定
or edit 或在记事本中编辑 notepad.exe ~/.bashrc or open the a specific project directory in VS Code: 或在VS Code中打开特定的项目目录:
安装应用程序 (Installing Applications)Always remember you’re running two operating systems. They may be highly integrated, but there are situations when you want an application installed in one or both. 永远记住您正在运行两个操作系统。 它们可能是高度集成的,但是在某些情况下,您可能希望将应用程序安装在一个或两个中。 It may be practical to use Git from either Windows or Linux. The Windows edition is installed by downloading an executable, but Git on Ubuntu is installed using: 从Windows或Linux使用Git可能是实际的。 Windows版本是通过下载可执行文件来安装的,而Ubuntu上的Git是使用以下命令安装的:
Similarly, you may want to test Node.js applications in both Windows and Linux. Again, Windows has an installer, but Node.js 14.x is installed in Ubuntu using the commands: 同样,您可能要在Windows和Linux中测试Node.js应用程序。 同样, Windows有一个安装程序 ,但是使用以下命令在Ubuntu中安装了Node.js 14.x:
安装Docker (Installing Docker)One exception to the “install twice” rule is Docker! “两次安装”规则的一个例外是Docker! Docker runs applications in isolated Linux containers which are conceptually similar to lightweight virtual machines with an OS, single application, and its dependencies. Using Docker for web development provides a consistent environment which is easy to install and works on any device. Docker在隔离的Linux容器中运行应用程序,这些容器在概念上类似于具有OS,单个应用程序及其依存关系的轻型虚拟机。 使用Docker进行Web开发可提供一致的环境,该环境易于安装并且可在任何设备上运行。 Docker Desktop for Windows is fully WSL2-enabled: 适用于Windows的Docker桌面完全支持WSL2:
Docker Desktop suggests you use WSL2 when it’s first launched. Alternatively, you can select Settings from the Docker system tray icon menu, then choose the General tab, check Use the WSL 2 based engine, and hit Apply & Restart. Docker Desktop建议您在WSL2首次启动时使用它。 或者,您可以从Docker系统任务栏图标菜单中选择设置 ,然后选择常规选项卡,选中使用基于WSL 2的引擎 ,然后点击Apply&Restart 。 ![]() Docker will use the default Linux distro, but you can also enable it in other installed distros from the WSL Integration panel in Settings, then Resources. Docker将使用默认的Linux发行版,但是您也可以在其他已安装的发行版中,从“ 设置”中的“ WSL集成”面板,然后单击 “ 资源 ”中启用它。 使用Docker启动Apache和PHP应用程序 (Launching Apache and PHP Applications Using Docker)It’s possible to install web dependencies such as Apache and PHP directly in your WSL2 Linux environment. However, Docker is safer, quicker, and more configurable: it allows you to run different versions of PHP on the same device at the same time. 可以直接在WSL2 Linux环境中安装Web依赖项,例如Apache和PHP。 但是,Docker更安全,更快捷,更可配置:它允许您在同一设备上同时运行不同版本PHP。 In this example, you’ll create a small PHP file and run it using the latest release of PHP 7.4 in a Docker container. Ensure you’ve installed Docker Desktop for Windows and enabled WSL2 mode, then create a folder in your Linux environment with an 在此示例中,您将创建一个小PHP文件,并在Docker容器中使用最新版本PHP 7.4运行它。 确保已经安装了适用于Windows的Docker桌面并启用了WSL2模式,然后在Linux环境中使用
Edit 编辑
Run the following 运行以下
Note: this command would also work in Windows, although the reference to 注意:此命令在Windows中也可以使用,尽管对 Open 在任何Windows Web浏览器中打开 ![]() To finish, press Ctrl + C in your Docker terminal to stop and delete the container. 要完成操作,请在Docker终端中按Ctrl + C以停止并删除容器。 WSL2和
|
|