How to Increase Swap Space on Linux Ubuntu?

In this article, we will learn how to increase the swap space, which will solve the memory errors in applications. This will cover how to add a swap file to an Ubuntu Linux.
Anyway, this is recommended to utilize the spinning hard disk drivers for swap as SSD can cause issues with hardware degradation over time. Due to this reason, do not enable swap on Cloud where the SSD storage is provided.
The Swap is a place where the OS can store temporary data as it can no longer holds in RAM. Basically, this will increase the ability of the amount of data that a server can hold during its working memory. The swap space on the hard disk drive will be used mainly when sufficient space is available in the RAM to hold the data.
The data written on the disk will be slower than the RAM, but the OS will prefer to keep the running applications and data in memory and keep the old data in the swap.
We will first check the system which already has the swap space. We can have multiple swap files or partitions.
Please note that, we can see the swap available in the system by using the below command –
# sudo swapon --show
[sudo] password for ubuntu:
NAME TYPE SIZE USED PRIO
/dev/dm-1 partition 1020M 0B -1
Or we can use free -h command to display the swap space.
$ free -h
total    used    free    shared    buffers    cached
Mem:     975M    254M    720M       4.8M       12M    133M
-/+ buffers/cache: 109M 866M
Swap:    1.0G    0B      1.0G
We can see the Swap row in the output of the machine. Here in this machine we have 1.0 G swap space allocated.
The most common way of allocating the swap space on the Linux is to use the separate partitions allocated for the swap. We cannot alter the partition scheme which is impossible, but we can easily create a swap file that resides on the existing partition.
Before we do this we will check the current disk space by using below command.
$ df -h
Filesystem    Size    Used    Avail    Use%    Mounted on
udev          473M       0    473M       0%     /dev
tmpfs          98M    4.9M     93M       5%     /run
/dev/dm-0      19G    3.1G     15G       18%    /
tmpfs         488M       0    488M       0%     /dev/shm
tmpfs         5.0M       0    5.0M       0%     /run/lock
tmpfs         488M       0    488M       0%     /sys/fs/cgroup
/dev/sda1     236M     51M    173M       23%    /boot
tmpfs          98M       0     98M       0%     /run/user/1000
The device under the /dev is the hard disk drives here we have 15 G space available.
In general, the amount equal to or double the amount of RAM on the machine is recommended for a good starting.
As we know the available hard disk space, we can go head by creating a swap file within our filesystem. Also, note that, a file of the swap size which we are calling as ‘swapfile’ is in our root partition / directory.
The best way to create a swap file is by using a file called ‘fallocate’ program, this command will creates a file of a pre-allocated size instantly.
As we have 1 GB RAM allocated to our machine we will create more 2 GB file to meet the minimum requirement of the Linux.
$ sudo fallocate -l 2G /swapfile
We can verify that using the below command.
$ ls -lh /swapfile
-rw-r--r-- 1 root root 2.0G May 16 12:52 /swapfile
We have created the swap file of our requirement but it needs to be turned on in this swap space. Before we turn on the swap file, we needed to lock the permissions of the file to only root users privileges who can read the contents of the files which will prevents the normal users from being able to access the file.
We can do this using the below command
$ sudo chmod 600 /swapfile
To verify the permissions we can see using the below command
$ ls -lh /swapfile
-rw------- 1 root root 2.0G May 16 12:52 /swapfile
We can turn on the ''swapfile'' to use as swap space by using the below command
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=049218ad-50b4-4c78-98e4-7a1ea21ca77e
We have to verify that the swap is available with the amount of space allocated. For this, we can use below command –
$ sudo swapon --show
NAME       TYPE       SIZE    USED    PRIO
/dev/dm-1 partition   3068M    0B       -1
OR Use the following command –
$ free -h
total    used    free    shared    buffers    cached
Mem:     975M    255M    720M       4.8M       12M    133M
-/+ buffers/cache: 108M 867M
Swap:    3.0G    0B    3.0G
As we have changed in swap file for the current session, we also need to reboot the server that will not retain the swap settings to this permanant setting. Also, automatically we can add this swap file settings to ‘/etc/fstab’ file.
$ sudo vi /etc/fstab
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
/dev/mapper/server--vg-root / ext4 errors=remount-ro 0 1
# /boot was on /dev/sda1 during installation
UUID=40f8b7fe-3195-414a-a0e4-a4443cceb78c /boot ext2 defaults 0 2
/dev/mapper/server--vg-swap_1 none swap sw 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
/swapfile none swap sw 0 0
We can use the above steps to increase the swap space using a swap file, where we can fix the issues related to the memory exceptions. If we are running out of memory on any Linux servers then the above content will be specially be useful.