Score:0

Reducing space consumption by "Ubuntu on Windows"

ir flag

I'm running Windows Subsystem for Linux (WSL2) with Windows Terminal, and I currently have downloaded "Ubuntu on Windows" from the Microsoft Store. When I run the "Ubuntu on Windows" application, I see all my Ubuntu directories in the terminal.

However, from what I understand from this Ubuntu Forum post, this app shouldn't be used. Instead, the "Ubuntu" Microsoft store app can be used to run WSL.

Currently Ubuntu is Windows is taking about 40GB of hard disk space. My question is, will changing to the "Ubuntu" app take less space on disk? If so, can someone please help how I can move from "Ubuntu on Windows" to "Ubuntu" without losing any of my data.

Output of wsl -l -v

$ wsl.exe -l -v
  NAME                   STATE           VERSION
* Ubuntu                 Running         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2

output of df -h /

$ df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdc        251G   27G  213G  11% /
David avatar
cn flag
Your third sentence is not making any sense. Nor does it have any reference to where you got this info.
Sanchit avatar
ir flag
sorry @David I have added the Link
Pilot6 avatar
cn flag
What do you want to achieve? To use less "memory" or what? It is totally unclear.
Sanchit avatar
ir flag
Yes, I want to reduce the memory, While searching I found the forum which states that my current setup should not be used and the new one is to be used, I am not understanding the difference between the two and whether it will help me or not
David avatar
cn flag
It may also help to use the term memory for memory not for hard disk space. That is not called memory-
NotTheDr01ds avatar
vn flag
Working on an answer, but as a side note, I'm curious what `wsl.exe -l -v` shows for you? Can you add it to your question at the bottom? Thanks!
Score:2
vn flag

will changing to the "Ubuntu" app take less space on disk?

Let's get that main question handled first -- Not really. Changing the app itself that you installed from doesn't have any direct impact on the disk space.

Indirectly? Probably. But only because backing up and restoring a WSL distribution has the side-effect of "compacting" it.

Why is Ubuntu on WSL taking up so much space?

The WSL2 virtual disk image that contains Ubuntu is a dynamic Hyper-V virtual disk. "Dynamic" here means that it starts out as a near 0-byte file and grows as you add data to it. All of the Ubuntu images for WSL so far have been based on Ubuntu Server and take up around 1GB of disk space just after installing.

For instance, I just removed my Ubuntu-22.04 distribution, then, in PowerShell, re-ran:

ubuntu2204.exe

After it asks for my username and password, the resulting image is 1.1GB.

Then, after a basic sudo apt update && sudo apt upgrade -y (the first thing you should always do after installing Ubuntu on WSL anyway), the distribution now takes ~1.7GB. Note that I'm using this PowerShell script to get the VHDX size, but you could also use File Explorer to find the ext4.vhdx file and observe its size.

So let's say I install the Nix package manager on Ubuntu at this point, with a few of my preferred stock packages. All of a sudden the disk size jumps to ~3.8GB. Yikes! (Not really, but let's say that was too much for me). So I delete all of /nix.

But now my disk image is still 3.8GB. Why didn't it go back to ~1.6GB?

The answer is that while the dynamic virtual disk grows to accommodate new files, it doesn't automatically shrink when those files are removed.

You can get an approximate (but not exact) idea of how much space should be consumed by Ubuntu with:

df -h /

If you find that you are really using 40GB files in Ubuntu (which is entirely possible), then there's really not much you can do other than uninstall or remove items.

However, if you are running into a situation where there's a large discrepancy between the size of the ext4.vhd virtual disk and what you are really using in Ubuntu, then you can follow this answer for some guidance on how to shrink the virtual disk.

"Ubuntu on Windows" vs. "Ubuntu"

from what I understand ... this app shouldn't be used. Instead, the "Ubuntu" Microsoft store app can be used to run WSL.

Not really. Once you have installed any of the Store versions of Ubuntu, the Store app really doesn't "do" much any longer. It's confusing, but also read this answer for more detail. The point that the Canonical developer was making in that thread was simply that they have "renamed" the app to avoid confusion. The fact that you installed when it was called something different is quite alright and isn't something you need to "fix."

The only purpose of the Store app is to:

  • Ask for your initial username and password
  • Copy the "template" (rootfs) for Ubuntu into the ext4.vhdx
  • Run the distribution for the first time

In the future, when you run ubuntu.exe or ubuntu2004.exe (which is likely what your Start menu item is point to), it detects that it is already installed and simply skips the first two steps.

Regardless of whether you run Ubuntu, Ubuntu on Windows, Ubuntu 22.04, Ubuntu 20.04, etc. (all options, at some point at least, in the Microsoft Store), the end result is that you are running your installed version of the distribution that was created from the Microsoft Store template.

Recent releases of the Ubuntu app in the Store even allow you to uninstall the app, leaving your Ubuntu distribution intact. However, I don't know if the one you are using is new enough for this, so I don't recommend it. Regardless, the "template" files only take up around 800MB or so, so it's only worth worrying about if you are are much tighter on disk space.

Sanchit avatar
ir flag
Thank you for the answer, After running the diskpart commands in the github issue, I was able to recover about 10GB back
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.