Lo-Fi Python

Feb 09, 2024

Ways to Free Up Disk Space on Your Computer for Python Developers

Below are some ways to free up disk space on your computer. This will be most helpful for Ubuntu users and Python developers. The pip examples show what I used on my Python version 3.11, so if you're running a different version use that number, like pip3.12, pip3.10, pip3.9, etc.

Benchmark your current disk space.

Before you start freeing up space, you might want to see the current state of your hard drive. You can print human readable disk space stats on Ubuntu with the df command.

df -h
read disk space stats on Ubuntu

Alternatively, here is a Python script that reads available disk space from your hard drive.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import shutil

def readable_format(size: int) -> str:
    """Converts a bytes integer to a human-readable format.

    Args:
        size (int): The bytes integer to convert.

    Returns:
        str: The human-readable format of the bytes integer.
    """
    for unit in ["B", "KB", "MB", "GB", "TB"]:
        if size < 1000:
            return f"{size:.2f} {unit}"
        size /= 1000
    return f"{size:.2f} PB"


def disk_space(path="."):
    """Returns the current total, used and free disk space in bytes."""
    usage = shutil.disk_usage(path)
    total_space = usage.total
    used_space = usage.used
    free_space = usage.free
    return total_space, used_space, free_space


# Call the function with the current directory (you can specify a different path)
total_space, used_space, free_space = disk_space()
print(f"Total space: {readable_format(total_space)}")
print(f"Used space: {readable_format(used_space)}")
print(f"Free space: {readable_format(free_space)}")
Total space: 21.47 GB
Used space: 10.34 GB
Free space: 10.50 GB

Clear your browser cache.

read disk space stats on Ubuntu

Purge your pip cache.

Before purging the Python pip package manager's cache, you can use the pip cache info command to see how much storage is consumed by the cache.

pip3.11 cache info

Next, use the pip cache purge command to clear up space on your system. Pip will print how many files it removed to the terminal.

pip3.11 cache purge
clear the pip package manager cache

Uninstall unnecessary Python libraries.

I tend to build up modules that I installed to see how it works or to quickly test something out, then never use again. It makes sense to cull your pip installed libraries occasionally. Be aware that sometimes an unknown module may be a required dependency of a module you want to use. First, use the pip list command to see your installed libraries:

pip3.11 list
view pip installed libraries

The pip uninstall command makes removing Python libraries easy. For example, let's say you're already using both the ruff Python linter and black. The ruff module recently introduced a new formatter that is more or less identical to Black. Therefore, I can uninstall black and the use "ruff format" command instead to format my code.

pip3.11 uninstall black

If you're not sure about a package, use the pip show command to learn more about it:

pip3.11 show ruff
view info about a Python library with pip

Run the autoremove Linux command.

autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime. - Linux apt Man Pages
sudo apt autoremove

Run the clean and autoclean Linux commands.

sudo apt clean
sudo apt autoclean

Read more on Ask Ubuntu: What is the difference between the options "autoclean" "autoremove" and "clean"?

Purge unnecessary Linux packages.

First, create a text file with all your installed Linux packages. Then browse the packages and assess if they can be safely removed.

apt list --installed > installed_packages.txt

You'll free up more space by deleting the largest optional packages. To list your installed packages in order of their file sizes and priority, you can use dpkg-query:

dpkg-query -W -f='${Installed-Size;8}\t${Priority}\t${Package}\n' | sort -n -r
see information about linux packages with dpkg-query

Once you've targeted a package, learn more about it with the apt show command. It shows if a package is essential or required, a description and its dependency modules. Optional packages are probably safe to delete assuming it's not a dependency of software you're actually using. However, purge with caution. Some of these packages are used in the software underneath your Ubuntu environment. Any leftover packages will be removed by the autoremove command if they are "orphaned" after you purge a package.

apt show <package-name>
see information about a linux package

If you are certain a Linux package can be deleted, the apt-get purge command removes a package and all configuration files from your computer. Be careful not to remove any critical Linux packages.

sudo apt-get purge <package-name>

Find and delete your largest Linux files.

This command prints the largest files on your root Linux file system. Then you can use the rm command to remove the file. Hint: sometimes PDF files can be deceptively large and can be good targets to free up space.

sudo find / -xdev -type f -size +25M -exec du -sh {} ';' | sort -rh | head -n 20
rm ~/large_file.pdf

That sums up a few ways Ubuntu users and Python developers can add some extra available disk space. It can definitely be frustrating to watch an install fail because there's no more space on your computer. These are a few strategies you can deploy to make room to operate on a disk space constrained system.