WSL (Windows Subsystem for Linux) memberi kita kernel Linux asli di dalam Windows—cara terbaik memakai GCC, gdb, dan tool Linux lainnya tanpa mesin virtual berat. Untuk pengembangan C, WSL2 jauh lebih cepat daripada MinGW karena compiler adalah binary Linux native yang berjalan di kernel Linux penuh. Bab ini menuntun kita memasang WSL2, memilih distro, mengonfigurasi editor Windows agar dapat mengakses file WSL, dan memverifikasi toolchain.

1. Memasang WSL2 #

WSL2 membutuhkan Windows 10 versi 2004+ atau Windows 11. Buka PowerShell sebagai Administrator dan jalankan:

powershell
wsl --install

Perintah ini otomatis:

  1. Mengaktifkan fitur Windows VirtualMachinePlatform dan Microsoft-Windows-Subsystem-Linux.
  2. Memasang kernel Linux WSL2.
  3. Memasang distro default (Ubuntu).

Restart komputer bila diminta.

Memilih Distro Lain #

Default adalah Ubuntu. Untuk melihat distro yang tersedia:

powershell
wsl --list --online

Output yang Dihasilkan (daftar dapat berubah):

text
The following is a list of valid distributions that can be installed.
Install with 'wsl --install -d <Distro>'.

NAME               FRIENDLY NAME
Ubuntu             Ubuntu
Debian             Debian GNU/Linux
kali-linux        Kali Linux Rolling
openSUSE-Tumbleweed openSUSE Tumbleweed
Fedora-Latest-42   Fedora Linux 42.0.0

Pasang distro pilihan (mis. Debian):

powershell
wsl --install -d Debian

2. Konfigurasi User dan Password #

Setelah distro terpasang, terminal akan terbuka dan meminta:

  1. Username Unix: nama user Linux (huruf kecil, tanpa spasi). Contoh: budi.
  2. Password: password user Linux. Tidak ditampilkan saat mengetik. Ingat baik-baik—diperlukan untuk sudo.

Setelah selesai, prompt shell akan muncul:

bash
budi@hostname:~$

3. Pasang Toolchain C di Distro #

Toolchain dipasang di dalam distro Linux, bukan Windows. Gunakan package manager distro—lihat bab Setup Linux untuk detail. Untuk Ubuntu/Debian:

bash
sudo apt update
sudo apt install -y build-essential gdb cmake valgrind

Verifikasi:

bash
gcc --version | head -1
gdb --version | head -1

Output yang Dihasilkan (Ubuntu 24.04 default WSL):

text
gcc (Ubuntu 13.3.0) 13.3.0
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-0ubuntu1

4. Verifikasi dengan Hello World #

Buat file hello.c di home directory WSL:

bash
cd ~
cat > hello.c <<'EOF'
#include <stdio.h>

int main(void) {
    printf("Toolchain WSL siap\n");
    return 0;
}
EOF
gcc -std=c11 -Wall -Wextra -o hello hello.c
./hello

Output yang Dihasilkan:

text
Toolchain WSL siap

5. Akses File WSL dari Windows #

WSL2 menyediakan akses ke filesystem Linux dari Windows via path \\wsl$\<distro>\ atau \\wsl.localhost\<distro>\. Cara cepat:

  1. Buka File Explorer.
  2. Ketik di address bar: \\wsl$\Ubuntu (atau nama distro).
  3. File home directory user terlihat di \\wsl$\Ubuntu\home\budi.

Untuk membuka folder WSL di Visual Studio Code dari terminal WSL:

bash
cd ~/my-c-project
code .

Perintah code membuka VS Code Windows dengan ekstensi WSL aktif—file diedit di Windows tetapi compiler berjalan di Linux.

6. Akses File Windows dari WSL #

Sebaliknya, drive Windows (C:\, D:\) ter-mount otomatis di WSL pada path /mnt/c/, /mnt/d/, dst.

bash
ls /mnt/c/Users/

Output yang Dihasilkan:

text
All Users  Default User  Default  Public  Z User

7. Konfigurasi Editor di Windows #

Visual Studio Code #

  1. Pasang VS Code di Windows (bukan di WSL).
  2. Pasang ekstensi WSL dari Microsoft.
  3. Buka VS Code, tekan Ctrl+Shift+P, ketik WSL: New Window.
  4. VS Code baru terbuka dengan koneksi ke WSL.
  5. Buka folder: File → Open Folder → pilih direktori di Linux (/home/budi/...).

Setelah ini, VS Code berperilaku seperti di Linux—terminal integrasi membuka bash WSL, compiler berjalan di Linux, debugger memakai gdb Linux.

JetBrains CLion #

  1. Pasang CLion di Windows.
  2. Buka File → Settings → Build, Execution, Deployment → Toolchains.
  3. Klik + → pilih WSL.
  4. Pilih distro WSL (mis. Ubuntu).
  5. CLion otomatis mendeteksi GCC, gdb, make, cmake dari WSL.
  6. Set sebagai default toolchain.

8. Konfigurasi .wslconfig untuk Sumber Daya #

Secara default WSL2 memakai hingga 50% RAM dan 8 vCPU. Untuk membatasi, buat file C:\Users\<user>\.wslconfig:

ini
[wsl2]
memory=8GB
processors=4
swap=2GB

Restart WSL agar konfigurasi aktif:

powershell
wsl --shutdown
wsl

9. Networking dan Port Forwarding #

WSL2 memakai virtual network adapter terpisah dari host. Aplikasi server yang listen di WSL dapat diakses dari Windows via localhost (Windows 10 0.19041+ ke atas otomatis forward).

bash
# Di WSL, jalankan server sederhana
nc -l 8080
powershell
# Di Windows PowerShell
Test-NetConnection -ComputerName localhost -Port 8080

Bila port forwarding tidak otomatis (Windows versi lama), gunakan netsh interface portproxy:

powershell
netsh interface portproxy add v4tov4 listenport=8080 connectaddress=$(wsl hostname -I) connectport=8080

10. Troubleshooting #

wsl --install gagal dengan 0x80370102 #

Virtualisasi tidak aktif di BIOS. Restart, masuk BIOS, aktifkan Intel VT-x atau AMD-V, simpan, restart ulang. Beberapa PC juga perlu mengaktifkan Hyper-V dan Virtual Machine Platform di Turn Windows features on or off.

WSL2 lambat saat akses filesystem Windows #

Ini wajar—lihat peringatan di Section 6. Solusi: pindahkan kode ke filesystem Linux (~/).

Cannot connect to the Docker daemon at unix:///var/run/docker.sock #

Docker daemon belum berjalan di distro. Jalankan:

bash
sudo service docker start

Atau pasang Docker Desktop untuk Windows dengan WSL2 backend.

WSL tidak terdeteksi setelah restart #

Periksa status service:

powershell
wsl --status
wsl --list --verbose

Bila service tidak berjalan:

powershell
Get-Service LxssManager | Start-Service

Editor VS Code membuka file Windows alih-alih WSL #

Pastikan ekstensi WSL terpasang dan terbuka dari terminal WSL (code .). Status bar VS Code akan menampilkan WSL: Ubuntu di pojok kiri bawah bila koneksi benar.