How to Add Swap Space on Ubuntu?

How to Add Swap Space on Ubuntu?

May 5, 2020 / Nirav S

What is Swap Memory ?

  • Swap is a space on a hard disk that is utilized when the physical RAM memory is full. It is utilized at the point when a Linux framework comes up short on RAM, latent pages are moved from the RAM to the swap space.
  • Swap space can appear as either a devoted swap segment or a swap record. For the most part when running Ubuntu on a virtual machine, a swap part is absent, and the main alternative is to make a swap record.

Before You Begin

  • To verify that Swap is installed Open terminal and write the following command
$ sudo swapon --show

  • Start by creating a file which will be used for swap:
$ sudo fallocate -l 1G /swapfile

  • ( Optional Troubleshoot )If fallocate is not installed or you get an error message saying fallocate failed: Operation not supported then use the following command to create the swap file:
$ sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
  • Only the root user should be able to write and read the swap file. Set the correct permissions by typing:
$ sudo chmod 600 /swapfile

  • Use the mkswap utility to set up a Linux swap area on the file:
$ sudo mkswap /swapfile

  • Activate the swap file using the following command:
$ sudo swapon /swapfile

  • To make the change permanent open the /etc/fstab file:
$ sudo nano /etc/fstab

  • and paste the following line:
/swapfile swap swap defaults 0 0

  • Verify that the swap is active by using either the swapon
$ sudo swapon --show

Talk to AWS Certified Consultant

    Let Us Talk About Your AWS Development Requirements

    Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project