$ sudo fallocate -l 500M /swapfile
$ sudo chmod 600 /swapfile
$ sudo mkswap /swapfile
This is a two stage process where both parts are important if you want your swap to continue to work after a reboot. We will activate the swap as well as changing the system's swapiness.
The swappiness defines how much your system uses the swap. A very low swappiness lets your system fill up lot's of RAM and only swap if necessarry, whereas a high swappiness value makes the swapping very aggressive.
For servers I like to use a low swappiness value of 3
This will activate the swap but only for as long as the system stays online.
After a reboot the swap would go back to being disabled.
$ sudo swapon /swapfile
$ sudo sysctl vm.swappiness=3
First open your fstab. This file dictates how your system mounts things (including swap).
Feel free to replace
nano with whatever text editor you like best. You can also just open the file at
/etc/fstab with any graphical text editor if you like.
$ sudo nano /etc/fstab
Then just go to the end of the file and add the following line:
/swapfile none swap sw 0 0
Then just do the same for
/etc/sysctl.conf and add the line
vm.swappiness=3 at the end of the file (or repalce the existing line if it already exists).
$ sudo nano /etc/sysctl.conf