Windows下基于WSL+VSCode开发环境的完整配置指南

前言

Windows Subsystem for Linux(WSL)是微软推出的一个兼容层,能够在Windows系统上运行Linux环境。对于开发者来说,WSL提供了在Windows下使用Linux工具链的便利,同时避免了传统虚拟机资源占用大的问题。本文将详细介绍如何在Windows 11下配置完整的WSL开发环境。

系统要求

  • Windows 10版本2004及更高版本(Build 19041及以上)
  • 或Windows 11
  • 至少4GB内存(推荐8GB以上)
  • 足够的磁盘空间(建议预留20GB)

WSL安装步骤

1. 启用WSL功能

通过PowerShell(管理员权限)启用WSL:

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

2. 启用虚拟机平台

1
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

3. 重启计算机

完成上述步骤后,重启计算机使更改生效。

4. 下载Linux内核更新包

访问微软官网下载WSL2 Linux内核更新包并安装。

5. 设置WSL2为默认版本

1
wsl --set-default-version 2

安装Linux发行版

1. 通过Microsoft Store安装

打开Microsoft Store,搜索并安装Ubuntu 22.04 LTS(或其他你偏好的发行版)。

2. 首次启动配置

安装完成后,启动Ubuntu,系统会提示你创建用户名和密码。

基础环境配置

1. 配置国内镜像源

为了提高下载速度,建议配置国内镜像源:

配置apt国内镜像(以阿里云为例):

1
2
3
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list
sudo sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list

2. 更新系统包

1
sudo apt update && sudo apt upgrade -y

3. 安装常用工具

1
sudo apt install -y curl wget git vim nano htop tree

3. 配置SSH(可选)

1
2
3
sudo apt install openssh-server
sudo systemctl enable ssh
sudo systemctl start ssh

开发环境配置

1. Python环境

安装Python 3.12和pip:

1
sudo apt install python3.12 python3.12-venv python3.12-pip

配置pip国内镜像(以清华源为例):

1
2
pip3.12 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip3.12 config set global.trusted-host pypi.tuna.tsinghua.edu.cn

配置Python虚拟环境管理:

1
pip3.12 install pipenv poetry

2. Conda安装(推荐)

下载并安装Miniconda:

1
2
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

初始化conda:

1
source ~/.bashrc

3. Node.js环境

使用nvm安装Node.js:

1
2
3
4
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts

配置npm国内镜像(以淘宝镜像为例):

1
2
3
4
npm config set registry https://registry.npmmirror.com/
npm config set disturl https://npmmirror.com/dist
npm config set cache ~/.npm
npm config set prefix ~/.npm-global

4. Docker配置

安装Docker Desktop for Windows,并在WSL中启用Docker支持。

开发工具配置

1. Visual Studio Code集成

安装VS Code的WSL扩展:

  • Remote - WSL
  • Remote - Containers

2. Git配置

1
2
3
git config --global user.name "你的姓名"
git config --global user.email "你的邮箱"
git config --global core.autocrlf input

3. Zsh和Oh My Zsh(可选)

1
2
sudo apt install zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

性能优化

1. WSL存储迁移到非系统盘

为了避免占用系统盘空间,建议将WSL存储迁移到其他硬盘分区:

方法一:导出导入法(推荐)

  1. 停止WSL服务:

    1
    wsl --shutdown
  2. 导出当前WSL发行版:

    1
    wsl --export Ubuntu-22.04 D:\wsl\ubuntu-22.04.tar
  3. 注销当前发行版:

    1
    wsl --unregister Ubuntu-22.04
  4. 导入到新位置:

    1
    wsl --import Ubuntu-22.04 D:\wsl\ubuntu-22.04 D:\wsl\ubuntu-22.04.tar --version 2
  5. 设置默认用户:

    1
    ubuntu2204.exe config --default-user 你的用户名

方法二:使用WSL2迁移工具

可以使用第三方工具如LxRunOffline进行更灵活的迁移管理。

2. 内存限制配置

在Windows用户目录下创建.wslconfig文件:

1
2
3
4
5
[wsl2]
memory=16GB
processors=8
swap=4GB
localhostForwarding=true

3. 磁盘性能优化

避免在WSL中操作Windows文件系统,尽量在WSL文件系统中工作。

4. 网络配置

配置代理(如果需要):

1
2
export http_proxy=http://host:port
export https_proxy=http://host:port

常见问题解决

1. WSL启动失败

1
2
3
wsl --shutdown
wsl --terminate Ubuntu-22.04
wsl -d Ubuntu-22.04

2. 文件权限问题

避免在/mnt目录下直接进行开发,建议在WSL文件系统中工作。

3. 中文显示问题

1
sudo apt install fonts-noto-cjk

最佳实践

  1. 项目位置: 将项目放在WSL文件系统中(如/home/username/projects
  2. 备份策略: 定期导出WSL发行版进行备份
  3. 版本控制: 使用Git进行代码版本管理
  4. 环境隔离: 为不同项目创建独立的虚拟环境

结语

通过本文的完整配置指南,你应该能够在Windows系统上搭建一个功能完善的WSL开发环境。WSL2提供了接近原生Linux的性能,同时保持了Windows系统的便利性,是现代开发者的理想选择。

如果在配置过程中遇到任何问题,欢迎在评论区留言讨论。


本文配置适用于Windows 11和Ubuntu 22.04 LTS,其他版本可能略有差异