Last Updated: February 25, 2016
·
281
· fuzzyalej

Set up a swap file

Create an empty swapfile:

sudo install -o root -g root -m 0600 /dev/null /swapfile

Write a 2 GB file named swapfile:

dd if=/dev/zero of=/swapfile bs=1k count=2048k

Tell Linux this is the swap file:

mkswap /swapfile

Activate it:

swapon /swapfile

Add it to the file system table so it will be maintained even after reboot:

echo "/swapfile       swap    swap    auto      0       0" | sudo tee -a /etc/fstab

Set the swappiness to 10 so it's used only as an emergency buffer:

sudo sysctl -w vm.swappiness=10
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

Now you have a swapfile. Try free -m to verify that the swap now appears.

free -m

(from digitalocean)