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

      I've been using WSL for development for two years and still learned something new from this tutorial. The kernel update link is very helpful.

    3. 陈浩然 陈浩然

      命令行安装方法确实简单,wsl --install -d Ubuntu-22.04一条命令,连重启提示都很清楚。

    4. OliviaClark OliviaClark

      The system requirements section is important. I tried installing on an older Windows build and it failed. Good to check first.

    5. 郑子涵 郑子涵

      Windows Terminal配合WSL简直绝配,用起来比原生Linux还舒服。感谢教程指导!