Windows WSL 安装 Ubuntu 教程

WSL 安装 Ubuntu 教程

WSL (Windows Subsystem for Linux) 允许在 Windows 上直接运行 Linux 环境。以下是详细安装步骤:

一、系统要求

  • Windows 10 版本 2004 及以上(内部版本 19041 及以上)或 Windows 11
  • 64 位系统

二、安装步骤

方法1:图形界面安装(推荐)

  1. 以管理员身份打开 PowerShell,运行以下命令启用 WSL:

    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  2. 启用虚拟机平台(WSL 2 需要):

    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  3. 重启电脑
  4. 将 WSL 2 设为默认版本

    wsl --set-default-version 2
  5. 打开 Microsoft Store,搜索 "Ubuntu",选择版本(推荐 Ubuntu 22.04 LTS 或 Ubuntu 24.04 LTS),点击"安装"

方法2:命令行安装

在 PowerShell(管理员)中直接运行:

# 安装 WSL 并默认安装 Ubuntu
wsl --install

# 或指定安装特定版本
wsl --install -d Ubuntu-22.04

三、首次启动配置

  1. 安装完成后,在开始菜单找到 Ubuntu 并启动
  2. 等待初始化完成,系统会提示创建用户名和密码:

    Enter new UNIX username: 你的用户名
    New password: 设置密码
    Retype password: 确认密码

四、验证安装

# 查看 WSL 版本
wsl --status

# 查看已安装的发行版
wsl --list --verbose

五、常见问题处理

1. WSL 2 内核更新

如果提示需要更新内核,下载并安装:

2. 切换 WSL 版本

# 设置默认版本为 WSL 2
wsl --set-default-version 2

# 将特定发行版切换为 WSL 2
wsl --set-version Ubuntu-22.04 2

3. 配置代理(如果需要)

在 Ubuntu 中编辑 ~/.bashrc,添加:

export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"

六、可选配置

1. 安装 Windows Terminal

从 Microsoft Store 安装 Windows Terminal,获得更好的终端体验

2. 配置国内镜像源(加速包管理)

sudo sed -i 's@archive.ubuntu.com@mirrors.aliyun.com@g' /etc/apt/sources.list
sudo apt update && sudo apt upgrade

3. 安装常用开发工具

sudo apt install build-essential git curl wget vim

安装完成后,你就可以像使用原生 Linux 一样使用 Ubuntu 了!

已有 1370 条评论

    1. 林嘉豪 林嘉豪

      WSL 2比WSL 1性能好太多了,文件系统操作快很多。教程里强调了设置默认版本为2,这个提醒很重要。

    2. CharlotteTaylor CharlotteTaylor

      I've shared this with three coworkers who wanted to start using WSL. The troubleshooting section is worth the read alone.

    3. 李雨桐 李雨桐

      装完Ubuntu之后记得装build-essential,不然编译软件会缺工具。文章里提到了这点,很贴心。

    4. EthanDavis EthanDavis

      This should be the official documentation. Clear, concise, and covers all the edge cases like kernel updates and mirror configuration.

    5. 赵子轩 赵子轩

      作为开发小白,之前一直想用Linux又不敢装双系统。WSL简直是完美解决方案,谢谢这篇教程带我入门。