make a swap file on the fly

Disclaimer: this should only be used if you can’t partition your drive by yourself, or it would be a hazzle to do so. I’ve used that method to make one compile process work, otherwise I don’t really need it.

Check for present swap space:
*if there’s any output you might consider another solutions*

sudo swapon -s

Create the actual file:
*bs times count equals filesize, in this case 1gb*

sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k

Create a linux swap area:

sudo mkswap /swapfile

Output looks something like this:

Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7ebsudo swapon -s

Activate the swap file

sudo swapon /swapfile

Now `swapon -s` should show something like this

Filename Type Size Used Priority
/swapfile file 262140 0 -1

Make it persistent in the /etc/fstab with the following entry

/swapfile none swap sw 0 0

Make swappiness 0, otherwise performance will be poor. So it’s just an emergency buffer:

echo 0 > /proc/sys/vm/swappiness

And finally a bit if good practice, since this is root’s business:

chown root:root /swapfile
chmod 0600 /swapfile