I didn’t create any swap during initial Ubuntu 19.04 install on Dell XPS laptop, so had to improvise when compiling a massive opensource project recently. This post shows you how to temporarily add swap memory using a regular file.
Create swap Using Regular File
The usual approach is fairly simple:
you create a large enough file (the desired size of your swap memory) – 1GB or 4GB or something like that
you initialise it as swap
you activated it as swap
Swap Files Support in BTRFS
Since I’ve used BTRFS filesystem for root in my Ubuntu setup, I discovered that there are additional steps needed: BTRFS filesystem creates and writes files in a way that’s been mostly incompatible with swap usage.
But as luck would have it, BTRFS supports swap files with Linux Kernel 5.0.x, so you just need to create file with specific attribute.
Swap Space Using File Procedure
Step 1: Create new empty file
We need to create a file with NOCOW (NO Copy-On-Write) property (no harm anywhere but is a requirement for BTRFS):
Step 2: Allocate required amount of space to the file