Operations 13 min read

Stop WSL from Filling Up Your C: Install Ubuntu .wsl Directly to D and Migrate Existing Installations

This guide explains why WSL's ext4.vhdx virtual disk constantly grows on the C drive, presents three solutions—including installing Ubuntu via a .wsl file directly to D, online installation with a custom location, and exporting then importing an existing distro—to free up space, and shows how to compact the vhdx file afterward.

Ubuntu
Ubuntu
Ubuntu
Stop WSL from Filling Up Your C: Install Ubuntu .wsl Directly to D and Migrate Existing Installations

Why WSL Is a C‑Drive Space Hog

WSL 2 stores the Linux file system in an ext4.vhdx virtual disk. The disk only expands ( "grow‑only" ) when you install software, pull images, or compile code, but it never shrinks after deletions, so a 20 GB+ file can quickly consume a limited C‑drive.

Install Docker image (5 GB) → +5 GB ✅

Delete Docker image (5 GB) → 0 GB ❌ No shrink

Compile Linux kernel (10 GB) → +10 GB ✅

Clean build artifacts (10 GB) → 0 GB ❌ No shrink

On a 128 GB system, WSL can easily eat 30‑50 GB.

Solution 1: Install Ubuntu via Official .wsl File Directly to D (Recommended)

Ubuntu provides a .wsl installer that works with WSL 2.4+ and supports the --location flag to specify the install directory.

1. Verify WSL version

wsl --version

WSL ≥ 2.7.1 is required for --location and --from-file. Update if needed:

wsl --update

2. Download the Ubuntu .wsl file

Choose a version and download the corresponding .wsl file (≈400 MB):

Ubuntu 26.04 LTS – https://releases.ubuntu.com/26.04/ubuntu-26.04-wsl-amd64.wsl Ubuntu 24.04 LTS – https://releases.ubuntu.com/24.04/ubuntu-24.04-wsl-amd64.wsl Daily build – https://cdimage.ubuntu.com/ubuntu-wsl/daily-live/current/ Chinese users can accelerate with the Aliyun mirror:

https://mirrors.aliyun.com/ubuntu-cdimage/ubuntu-wsl/

Example PowerShell download:

# Create download directory
mkdir D:\WSL\images -Force
# Download Ubuntu 26.04 LTS .wsl (≈400 MB)
curl.exe -L -o D:\WSL\images\ubuntu-26.04-wsl-amd64.wsl https://releases.ubuntu.com/26.04/ubuntu-26.04-wsl-amd64.wsl

3. Install to D with one command

# Install to D:\WSL\Ubuntu
wsl --install --from-file D:\WSL\images\ubuntu-26.04-wsl-amd64.wsl --location D:\WSL\Ubuntu

After installation the ext4.vhdx resides in D:\WSL\Ubuntu-26.04\ and occupies no space on C.

4. First‑time configuration

# Launch
wsl -d Ubuntu
# Create default user
adduser myuser
usermod -aG sudo myuser
# Set default user in wsl.conf
cat > /etc/wsl.conf <<'EOF'
[user]
default=myuser
[boot]
systemd=true
EOF
# Restart WSL
exit
wsl -t Ubuntu
wsl -d Ubuntu

5. Set as default distribution

wsl --set-default Ubuntu

Solution 2: Online Installation with Custom D Location

If you prefer not to download a .wsl file, you can install directly from the Microsoft Store while specifying the target directory:

wsl --install -d Ubuntu --location D:\WSL\Ubuntu
Note: This method may be slower and could install a non‑latest LTS version.

Solution 3: Migrate an Existing C‑Drive Installation to D (Three‑Step Process)

Step 1 – Export Backup

# Shut down all WSL instances
wsl --shutdown
# List current distributions
wsl -l -v
# Export Ubuntu to a tar file
wsl --export Ubuntu D:\WSL\ubuntu-backup.tar
Export time varies (10‑30 min) depending on the size of the distro. Do not use WSL during export.

Step 2 – Unregister the C‑Drive Instance

# Remove the original Ubuntu registration (frees C‑drive space)
wsl --unregister Ubuntu
This deletes the ext4.vhdx on C, releasing space. Ensure the export succeeded.

Step 3 – Import to D

# Create target directory
mkdir D:\WSL\Ubuntu -Force
# Import the tar as a new Ubuntu instance on D
wsl --import Ubuntu D:\WSL\Ubuntu D:\WSL\ubuntu-backup.tar --version 2

Verify the vhdx location:

dir D:\WSL\Ubuntu\ext4.vhdx

Restore Default User After Migration

After import WSL defaults to root. Choose one of the following:

Command line: Ubuntu config --default-user your_username Edit /etc/wsl.conf inside the distro to set default=your_username Registry edit (if the above fail): set DefaultUid to 1000 under

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss

Additional Skill: Compacting the vhdx File

Even after moving to D, the vhdx remains grow‑only. Periodic compaction reclaims space.

Method 1 – DiskPart (Universal)

# Shut down WSL
wsl --shutdown
# Open DiskPart
diskpart
# Select the vhdx file (replace with your path)
select vdisk file="D:\WSL\Ubuntu\ext4.vhdx"
attach vdisk readonly
compact vdisk
detach vdisk
exit
Typical compression: a 30 GB vhdx can shrink to ~12 GB, depending on deleted data.

Method 2 – Built‑in WSL Compact Command (Newer WSL)

wsl --shutdown
wsl --manage Ubuntu --compact

Method 3 – Hyper‑V Manager (GUI)

Press Win+S and search "Hyper‑V Manager".

In the right‑hand panel choose "Edit Disk".

Select the vhdx file and click "Compact".

Common Questions

Q: Will Docker still work after migration?

Yes. Docker Desktop uses the separate WSL distributions docker-desktop and docker-desktop-data. Migrate them similarly if desired:

# Export Docker data
wsl --export docker-desktop-data D:\WSL\docker-data-backup.tar
# Unregister
wsl --unregister docker-desktop-data
# Import to D
wsl --import docker-desktop-data D:\WSL\docker-data D:\WSL\docker-data-backup.tar --version 2

Q: Does Windows Terminal configuration survive the move?

Terminal settings are stored in settings.json on Windows and are unaffected. If the profile icon or name changes, verify the guid matches the entry.

Q: What is the difference between .wsl and .appx files?

.wsl

: Install via wsl --install --from-file, supports --location, can be placed on any drive (recommended). .appx: Double‑click or Store install, limited to C drive, requires later migration.

Online install: wsl --install -d Ubuntu with --location support, but may be slower and not the latest version.

Q: Will installing to D make the system slower?

Almost not. WSL 2 I/O performance depends on the underlying disk type (SSD vs HDD) rather than the drive letter. An SSD D‑drive offers comparable speed to C; a mechanical HDD will be slower.

For more Ubuntu/Linux tips, follow the UbuntuNews community.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

WindowsWSLCommandLineUbuntuvhdxDiskManagement
Ubuntu
Written by

Ubuntu

Focused on Ubuntu/Linux tech sharing, offering the latest news, practical tools, beginner tutorials, and problem solutions. Connecting open-source enthusiasts to build a Linux learning community. Join our QQ group or channel for discussion!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.