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 Information
Before we start, first make sure to check if the system already has swap space available by running the following swapon command.
$ sudo swapon --show
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 -h
You can see from the above output, that there is no active swap on the system.
Step 2: Checking Available Space on the Partition
To 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 -h
The partition with /
has enough space available to create a swap file.
Step 3: Creating a Swap File in Ubuntu
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 the swap using the ls command as shown.
$ sudo fallocate -l 1G /swap.img $ ls -lh /swap.img
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 Ubuntu
To enable the swap file in Ubuntu, first, you need to set the correct permissions on the file so that only the root
user has access to the file.
$ sudo chmod 600 /swap.img $ ls -lh /swap.img
From the above output, you can see that only the root
user has 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.img
Verify that the swap space is available by running the following commands.
$ sudo swapon --show $ free -h
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 Ubuntu
To 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/fstab
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 Value
Swappiness is a Linux kernel parameter that 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/swappiness
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=10
Adjusting 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_pressure
The current value is set to 100, which 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 Ubuntu
To remove or delete the newly created swap file, run the following commands.
$ sudo swapoff -v /swap.img $ sudo rm -rf /swap.img
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.
Why do you use “
-rf
” in the rm command?This can be dangerous if someone misses types the command as:
(note space between the
"/"
and “swap.info”.I managed to do this on a production SCO server 25 years ago late on a Friday afternoon. Totally ruined my weekend, but learned to only use “rm -r” when you really mean it.
In my opinion, it is a bad idea to teach using “rm -rf” without explaining that if this command is miss-typed, you may remove your entire file system, because the command that you are issuing is: remove recursively, without bothering me with questions, because I definitely want to do this.
In your case, you are doing, so you have to write permission on the file (no need for
-f
), and it is a file, not a directory (no need for-r
).The safer command to teach is
sudo rm /swap.img
Why not just set up a swap partition?
@Dragonmouth,
Yes, you can setup the Swap partition, only if there is a Free space available on the filesystem. What if there is no free space? in this case adding swap space is useful when you running out of memory..
“What if there is no free space?”
Straw man argument. If there is no free space available on the system you cannot set up swap space, either. It’s time to get a bigger disk or another disk.
@Dragonmouth,
Yes, if no space is left on the disk, you must add a new hard disk and create a space for a swap.