Add swap space to Ubuntu

Get the swap space ready:

$ sudo fallocate -l 2G /extra.swap

$ sudo chmod 0600 /extra.swap

$ sudo mkswap /extra.swap

Now check your memory

$ free -mh

and now add the swap space to your system

$ sudo swapon /extra.swap

If you check the free memory again you should see the increase in your swap memory.

$ df -h

If you want to disable the extra swap space, for example to remove the file, you can do:

$ sudo swapoff /extra.swap