Contents
  1. 1. Check the free memory and free space
  2. 2. Create the swap file and enable swap space
  3. 3. Make the swap file permanent

If we don’t have enough memory in a Linux machine, we can enable the swap space to increase the “memory”.

Check the free memory and free space

1
2
3
4
5
6
7
8
#check if any swap space enable
sudo swapon --show

#verify there is no active swap
free -h

#check free space of the hard drive
df -h

Create the swap file and enable swap space

1
2
3
4
5
6
7
8
9
10
11
#create the swap file
sudo dd if=/dev/zero of=/swapfile count=4096 bs=1MiB

#make the file only accessible by root
sudo chmod 600 /swapfile

#mark the file as swap space
sudo mkswap /swapfile

#enable the swap file
sudo swapon /swapfile

Make the swap file permanent

1
2
3
4
5
#backup the /etc/fstab file in case anything goes wrong
sudo cp /etc/fstab /etc/fstab.bak

#add the swap file information to the end
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That’s all for enabling the swap file on Linux. For more information, we can find more on https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-16-04

Contents
  1. 1. Check the free memory and free space
  2. 2. Create the swap file and enable swap space
  3. 3. Make the swap file permanent