Swapfiles on Ubuntu

Swapfiles are essentially space on your hard drive that’s used as a virtual extension of your computers physical memory (RAM). Your computer “swaps” physical memory and virtual memory so that it doesn’t overload the physical memory. You typically don’t want to have swapfiles on solid state drives or on virtual private servers. Solid state drives will wear out quicker as they have a finite amount of reads and writes and on virtual private servers, it would probably be more optimal to just upgrade the RAM.

Nonetheless, heres the steps to creating a swapfile:

  1. Start by creating the swapfile. In the example below it creates a swapfile with the size of 1GB. You typically want the swapfile size to be equal to or double the amount of physical memory that you have.
sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo sh -c ‘echo “/swapfile none swap sw 0 0” >> /etc/fstab’




Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store