1). log in as root in your linux computer.
2). Determine if you already have a swapfile. type this command free -h
You will see something like this below. You will observe that Swap has 0B meaning no swap file
total used free shared buffers cached
Mem: 3.9G 3.5G 406M 106M 116M 1.1G
-/+ buffers/cache: 2.3G 1.6G
Swap: 0B 0B 0B
How to add Swap File
Follow these steps to add 4GB of swap to your server. If you want to add 2GB instead of 1 GB, replace 4G
 with 2G
.
-
Create a file that will be used for swap:
sudo fallocate -l 4G /swapfile
IfÂfaillocate
 is not installed or if you get an error message sayingÂfallocate failed: Operation not supported
 then you can use the following command to create the swap file:sudo dd if=/dev/zero of=/swapfile bs=1K count=4M
Only the root user should be able to write and read the swap file. To set the correct permissions type:
So by using multiplicative suffixes it's easier to count (1K * 4M = 4 GiB). -
sudo chmod 600 /swapfile
Use theÂmkswap
 utility to set up the file as Linux swap area: -
sudo mkswap /swapfile
Enable the swap with the following command: -
sudo swapon /swapfile
To make the change permanent open theÂ/etc/fstab
 file and append the following line:/etc/fstab/swapfile swap swap defaults 0 0
To verify that the swap is active, use either theÂswapon
 or theÂfree
 command as shown below: -
sudo swapon --show
If the above command does not work type the first command given in this guide which is free -h