How to Add Swap Space on Ubuntu
Abstract: Check Filesystem Disk Usage in Ubuntu The partition with / has enough space available to create a swap file. Step 3Enable Swap Space in Ubuntu Verify
One of the simplest ways of watching against out-of-memory problems in applications is to increase some swap size in your server. In this article, we will explain how to add a swap file to an Ubuntu server.
Step 1: Checking Swap InformationBefore we start, first make sure to check if the system already has swap space available by running the following command.
$ sudo swapon --showCheck Swap Space in Ubuntu
If you don’t see any output, that means your system doesn’t have swap space available currently.
You can also confirm that there is no swap space available using the free command.
$ free -hCheck Free Swap Space in Ubuntu
You can see from the above output, that there is no active swap on the system.
Step 2: Checking Available Space on the PartitionTo create a swap space, first, you need to check your current disk usage and confirm that there is enough space to create a swap file on the system.
$ df -hCheck Filesystem Disk Usage in Ubuntu
The partition with /
has enough space available to create a swap file.
Now we will create a swap file called "swap.img"
on our Ubuntu root (/)
directory using the fallocate command with the size of 1GB (you can adjust the size as per your needs) and verify the size of swap using ls command as shown.
$ sudo fallocate -l 1G /swap.img $ ls -lh /swap.imgCreate Swap File in Ubuntu
From the above output, you can see that we’ve created the swap file with the correct amount of space i.e. 1GB.
Step 4: Enabling the Swap File in UbuntuTo enable the swap file in Ubuntu, first, you need to set the correct permissions on the file so that only the root
user have access to the file.
$ sudo chmod 600 /swap.img $ ls -lh /swap.imgSet Permission on Swap File
From the above output, you can see that only the root
user has the read and write permissions.
Now run the following commands to mark the file as swap space and enable the swap file to start utilizing it on the system.
$ sudo mkswap /swap.img $ sudo swapon /swap.imgEnable Swap Space in Ubuntu
Verify that the swap space is available by running the following commands.
$ sudo swapon --show $ free -hVerify Swap Space in Ubuntu
From the above output, it is clear that our new swap file has been created successfully and our Ubuntu system will start to use it as necessary.
Step 5: Mount the Swap File Permanent in UbuntuTo make the swap space permanent, you need to add the swap file information in the /etc/fstab
file and verify it by running the following commands.
$ echo '/swap.img none swap sw 0 0' | sudo tee -a /etc/fstab $ cat /etc/fstabMount Swap File Permanently in Ubuntu Step 6: Tuning Swap Settings in Ubuntu
There are a few settings that you need to configure that will have an effect on your Ubuntu’s performance when using the swap.
Adjusting the Swappiness ValueSwappiness is a Linux kernel parameter the specifies how much (and how often) your system swaps data out of RAM to the swap space. The default value for this parameter is 「60」 and it can use anything from 「0」 to 「100」. The higher the value, the higher the usage of swap space by Kernel.
First, check the current swappiness value by typing the following command.
$ cat /proc/sys/vm/swappinessCheck Swappiness Value
The current swappiness value of 60 is perfect for Desktop usage, but for a server, you must set it to lower value i.e. 10.
$ sudo sysctl vm.swappiness=10
To make this setting permanent, you need to add the following line to the /etc/sysctl.conf
file.
vm.swappiness=10Adjusting the Cache Pressure Setting
Another similar setting that you may want to alter is the vfs_cache_pressure
– this setting specifies how much the system will want to cache inode and dentry details over other data.
You can check the current value by querying the proc filesystem.
$ cat /proc/sys/vm/vfs_cache_pressureCheck Cache Pressure Setting
The current value is set to 100, that means our system removes inode information from the cache too rapidly. I suggest, we should set this to a more stable setting like 50.
$ sudo sysctl vm.vfs_cache_pressure=50
To make this setting permanent, you need to add the following line to the /etc/sysctl.conf
file.
vm.vfs_cache_pressure=50
Save and close the file when you are done.
Step 7: Removing a Swap File in UbuntuTo remove or delete the newly created swap file, run the following commands.
$ sudo swapoff -v /swap.img $ sudo rm -rf /swap.imgDelete Swap File in Ubuntu
Finally, delete the swap file entry from the /etc/fstab file.
That’s all! In this article, we have explained how to create a swap file on your Ubuntu distribution. If you have any queries regarding this article, feel free to ask your questions in the comment section below.