Swap on a Linux is a memory management scheme, when your computer doesn’t have sufficient Virtual Memory “RAM”, it’s starting to use computer HDD Physical Memory to store data from Virtual Memory, this will allow computer to work further even if there is not enough RAM.

Instead of adding RAM you can create and use a SWAP file this will allow Linux to work, but if SWAP doesn’t help to much you’ll have to add more RAM.

Steps to configure and enable SWAP file on Linux:

Step 1. Check if your computer has Swap memory enabled

Write down following command:

bash-root@username:/# free

if result shows swap and you you see different numbers than Swap is configured, if at swap line you see: number 0 everywhere means it’s not configured, you’ll have to configure by following next step.

Step 2. Create the Swap file

Usually windows uses a dedicated partition for swap where it will store the virtual memory, here on Linux we can create a disk image file which will be used by Swap, so no additional work for creating another partition dedicated just for Swap.

Swap file is recommended to be stored in /var and we will call it simple: “swap.img”, also we must set file permissions to 600 so none of the other users could use or read the file, this can cause problems to the system memory. Write following commands:

bash-root@username:/# cd /var
bash-root@username:/var# touch swap.img
bash-root@username:/var# chmod 600 swap.img

Step 3. Set size for the Swap file

Now you should set Swap size depending on how much RAM you have, too much Swap the computer will work slow. If your computer has 512Mb of RAM,  Swap can be set to 1Gb, if you have 1Gb of Ram, swap can be set to 2Gb, if you have 4Gb of Ram you can set 1Gb of swap, as from now there should be enough RAM.

Now we will fill the swap file with some size, in out case we have a 512Mb server and we will fill it with 1Gb of memory, follow the commands:

bash-root@username:/var# dd if=/dev/zero of=/var/swap.img bs=1024k count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 4.0868896 s, 287 MB/s

After command, we will have to white some minutes, and we will get the results that bytes have been copied and at what speed rate.

Step 4. Initialize Swap filesystem

Follow the commands:

bash-root@my.droplet:/var# mkswap /var/swap.img
Setting up swapspace version 1, size = 1020 GiB
no label, UUID=72761533-4bbe-316g-v02e-c0ssbe4cfdf2

If you got similar result, it means that swap was initialized.

Step 5. Enable Swap

To enable swap we will have just to turn it on, to check if it’s enabled try the command free from first step.

bash-root@my.droplet:/var# swapon /var/swap.img

Step 5. Enable Swap during boot

Swapon command only enables the file for your current boot, after reboot swap file will not be enabled, you can enable it again, but also you can edit file: /etc/fstab and add it to boot, so it will automatically mount itself at boot.

To edit fstab file:

bash-root@username:/# sudo gedit /etc/fstab

At the end of the file add follwing line, instead of spaces use Tab key.

/var/swap.img    none    swap    sw    0    0

Now Ctrl+X to exit and Y to save!

Step 6. Test and Conclusion

You can do a reboot of the system with sudo reboot after reboot swap should be in use, check with free command. Swap will make your Linux more stable, and you system will not try to kill your apps because of not enough RAM errors which can appear from time to time, better enable swap and be safe.

Leave a Reply