How to Setup Swap File in Linux (Ubuntu 18.04)

Searching for the step by step guide on how to setup swap file in Linux? If yes then you are a lucky one. Today, we’ll show you how to add a swap file in Linux.

This is important if you are using Linux (Ubuntu, Debian, etc) in a low memory machine or server. Swap is very useful for that system which required more RAM to run applications properly.

Swap file / Swap space is a space on the hard disk that can be used as a RAM and operating system can temporarily store data in it.

Many times application got crashed due to insufficient memory. In that situation creation of Swap file is better to keep them up and running.

Before starting, check our guide on how to create a VPS server in Digitalocean.

How to Setup Swap File in Linux

Below are the steps that you need to follow to add a swap fine in your Linux machine.

Step 1: Check Swap Information

First of all, check your system if the system already has some swap space available. You can check it by the following command.

sudo swapon --show

You can also verify that there is no active swap using the free utility:

free -h

As you can see, there is no swap active. Ok, now time to create a file which will be used for swap. The best way of creating a swap file is with the fallocate program. This command creates a file of a preallocated size instantly. Before making file make sure you have enough free space on the disk. you can check available space by executing df -h command.

Step 2: Enabling the Swap File

I have 1GB memory in my system. So I am creating swap of 2GB in size.

sudo fallocate -l 2G /swapfile

We recommend creating a swap double or equal in size of your physical memory installed in your system.

You can verify that the correct amount of space was reserved by typing below command

ls -lh /swapfile

Output:

-rw-r–r– 1 root root 2.0G Sep 18 09:43 /swapfile

As you can see, 2GB swap file has been created. Now time to set correct permission for the swap file. Make the file only accessible to root by typing

sudo chmod 600 /swapfile

Use below command to set up the file as Linux swap area

sudo mkswap /swapfile

Output:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=29765536-ff34-45c1-be3c-b100c473d9e3

After marking the file, Now setup the swap for system

sudo swapon /swapfile

Again check swap is active.

sudo swapon --show

Output:

NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2

The swap has been set up successfully and our operating system will begin to use it when necessary.

Step 2: Make Swap Permanent

Now the problem is when you reboot your system it will not able to retain the swap settings automatically. To make the change permanent open the /etc/fstab file and paste the below code at the end of the file (refer screenshot)

Open /etc/fstab in edit mode

sudo nano /etc/fstab

Add below one line code at the end of this file

/swapfile none swap sw 0 0

Press Ctrl + x then y and ENTER to save the file.

That’s all. You have successfully setup swap file in Linux. There are a few options that you can configure that will have an impact on your system’s performance when dealing with swap.

Setup Swapiness

The Swappiness is a Linux kernel parameter that defines how often your system will use the swap space. You can set any value between 0 and 100. A low value means the kernel to try to avoid swapping whenever possible and a higher value means the kernel use the swap space more aggressively.

By default, it is 60. You can check the current swappiness value by typing the following command:

cat /proc/sys/vm/swappiness

We know that RAM is faster than SSD/HDD so less Swappiness is better for a production server.

Open /etc/sysctl.conf file in edit mode by using below command & add vm.swappiness=10 at the end of the file. (refer screenshot)

sudo nano /etc/sysctl.conf

Save the file and reboot your system one-time using sudo reboot command.

Conclusion

If you are running into OOM (out of memory) errors in Linus then add swap space in your system to overcome this problem.

Must Read: How to Install LAMP in Ubuntu 18.04 (With PHPMyAdmin)

In this article, you learned how to setup Swap file in Linux. If you have any question regarding this article or facing some problems then comment below, we’ll happy to solve them.

Recommended Posts

No comment yet, add your voice below!


Add a Comment

Your email address will not be published. Required fields are marked *