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. AvaJohnson AvaJohnson

      The verification commands at the end are helpful for making sure everything worked. wsl --list --verbose is my go-to check now.

    2. 王浩然 王浩然

      代理配置那一段虽然简短但很关键,有时候apt连不上就是因为没有代理。加了环境变量之后一切都顺畅了。

    3. MiaWilson MiaWilson

      I followed the WSL 2 steps exactly and now have Ubuntu running perfectly on Windows 11. No more dual boot headaches!

    4. 陈思琪 陈思琪

      Windows Terminal确实比默认终端好用太多了,配色好看,还支持多标签页。强烈推荐大家装一个。

    5. OliverMiller OliverMiller

      The WSL 2 kernel update link saved me. I kept getting that error and didn't know where to download the update. Much appreciated!