Hướng dẫn sử dụng WSL2 trên Windows 11: Linux cho lập trình viên
Mục lục bài viết
1. WSL2 là gì và tại sao cần?
WSL (Windows Subsystem for Linux) cho phép chạy Linux trực tiếp trên Windows: không cần máy ảo, không cần dual-boot.
WSL2 vs WSL1:
| Tính năng | WSL1 | WSL2 |
|-----------|------|------|
| Kiến trúc | Translation layer | Linux kernel thật |
| Tốc độ I/O | Chậm (qua API) | Nhanh (ext4) |
| Tương thích | Hạn chế | Full system call |
| Docker | Không | Có |
Lợi ích cho lập trình viên:
- Dùng Linux terminal trên Windows.
- Chạy Docker native.
- Develop trên môi trường giống production.
- Dùng Node.js, Python, Ruby, Go... không confict với Windows.
2. Cài đặt WSL2
Bước 1: Mở PowerShell (Admin) và chạy:
```powershell
wsl --install
```
Lệnh này tự động bật WSL, cài Linux kernel, và cài Ubuntu.
Bước 2: Restart máy.
Bước 3: Mở Start > Ubuntu: lần đầu sẽ yêu cầu tạo username/password cho Linux.
Kiểm tra phiên bản:
```bash
wsl -l -v
```
Nếu là WSL1, nâng cấp:
```powershell
wsl --set-version Ubuntu 2
wsl --set-default-version 2
```
wsl --install -d Debian, -d kali-linux, -d Ubuntu-24.04.3. Cấu hình cơ bản
Update packages:
```bash
sudo apt update && sudo apt upgrade -y
```
Cài dev tools cơ bản:
```bash
sudo apt install -y build-essential git curl wget unzip
```
Cài Node.js (qua nvm):
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
source ~/.bashrc
nvm install --lts
```
Cài Docker (trong WSL2):
```bash
sudo apt install -y docker.io
sudo service docker start
sudo usermod -aG docker $USER
```
File system:
- Linux files:
/home/username/: nên làm việc ở đây (ext4, nhanh). - Windows files:
/mnt/c/Users/...: chậm hơn vì qua 9P protocol.
/home/...), không phải /mnt/c/...: hiệu năng cao hơn, nhất là với npm install, git.4. Tích hợp với Windows Tools
VS Code + WSL:
- Cài Remote - WSL extension.
- Mở terminal WSL >
code .: VS Code mở trong WSL context. - Terminal, debugger, extensions đều chạy trên Linux.
Windows Terminal:
- Cài từ Microsoft Store: quản lý nhiều terminal (PowerShell, CMD, WSL) trong một cửa sổ.
- Cấu hình: Settings > Profiles > Ubuntu > Appearance > Color scheme.
File Explorer:
explorer.exe .: mở thư mục hiện tại trong Windows File Explorer.- Linux files xuất hiện như network share:
\\wsl$\Ubuntu\home\username\.
Chạy Windows app từ WSL:
```bash
notepad.exe ~/test.txt
code .
```
5. WSL Config và Performance
File .wslconfig (Windows user folder C:\Users\):
```ini
[wsl2]
memory=4GB
processors=4
localhostForwarding=true
kernelCommandLine=intel_pstate=disable
swap=2GB
```
Quản lý WSL:
```bash
wsl --shutdown # Tắt WSL
distro-tool.exe export Ubuntu backup.tar
distro-tool.exe import Ubuntu C:\wsl\Ubuntu backup.tar
```
VSCode remote tunnel:
code tunnel: expose WSL dev server ra internet qua VS Code Tunnel.
GPU Acceleration:
- CUDA: Cài CUDA Toolkit trong WSL2: dùng GPU cho ML/DL.
- Windows 11 có DirectML backend cho WSL2.
memory=4GB giới hạn RAM WSL dùng. Nếu WSL ngốn hết RAM Windows, giảm con số này. Swap tăng nếu hay build lớn.6. Use cases và best practices
Use cases phổ biến:
- Web development: Node.js, Python, Ruby on Rails (môi trường Linux giống production).
- Docker: Chạy Docker containers native trên WSL2.
- Data science: Python, Jupyter, TensorFlow với GPU.
- DevOps: Terraform, Ansible, Kubernetes (k3s).
- Embedded Linux: Cross-compile cho ARM/Raspberry Pi.
Best practices:
/mnt/c/.wsl --export.wsl --update..wslconfig nếu cần..wslconfig: [boot]\nsystemd=true.🙋 Câu hỏi thường gặp
WSL2 có thay thế hoàn toàn Linux dual-boot không?
Cho 90% nhu cầu. Lập trình web, Docker, data science: đủ. Nếu cần GUI Linux (Blender, GIMP full), đồ hoạ nặng, hoặc kernel modules: dual-boot vẫn cần.
WSL2 chậm hơn Linux thật không?
Có, khoảng 5-10% chậm hơn Linux native (I/O file, network). Nhưng nhanh hơn nhiều so với máy ảo (VirtualBox, VMware). Với lập trình web, khác biệt không đáng kể.