If your server is running into OOM (out of memory) errors or occasional spike ram usage taking your site down then, adding a swap space can save from this kind of errors. In this guide, we will cover how to add a Swap Space On Ubuntu 18.04 server.
For starters, Swap space works as virtual RAM, when the physical ram is full. When an Ubuntu system runs out of memory/RAM, it moves the inactive or older cached process to swap space to free up RAM for a new process. One of the easiest ways to stop your site going down duet out-of-memory errors is to add some swap space to your server.
Normally SWAP is added by adding a dedicated swap partition, but in most cases for VPS or virtual machines, the swap partition isn't available so the only way is to use a swap file instead.
The data on written to swap space will be slower than information kept in memory as RAMs have significantly high read and write speed compared to the hard disk but Ubuntu will keep running application data in RAM and use swap space to store/archive for the older data or processer. In short, having swap space works as a fallback option for when your VPS runs out as RAM, which comes useful in an emergency situation. It isn't a permanent replacement or RAM.
In this post, I will cover how to add a swap file to an Ubuntu 18.04 server
Table of contents
Checking if the SWAP file exists.
Some provider like Linode provides free swap space, so before we start, we can check if your VPS already has swap space available or not. If you have SWAP space then you don't need to do anything, you are good to go.
You can check if your VPS has any swap by typing this command in terminal:
$ sudo swapon --show
If you don't have any swap space you won't see any output. If you have a swap space you will get output like below.
You can verify that there is no active swap using the
$ free -h
If you don't have swap space you will see the output like below otherwise it will be shown as
Swap: 2.0G 93M 1.9G
As you can above in the Swap row of the output, no swap is active on the system.
Making, sure enough, hard disk space is available
First thing first, we should check if there is enough available space to create the swap file. To do that enter this command in the terminal.
$ df -h
The output should be like this-
Here our drive is
/dev/vda1. It has enough space available for us to create a swap.
Creating Swap File
Now that we know we have enough space for the swap file, we will create a swap file in
/ the directory. In this post, I will add
0.5G of swap, if you need more swap, replace
0.5G with the size of the swap space you need. Normally the swap size should be equal to the size of RAM. But anything over 4gb is useless. Since my server has 512MB of ram I will be creating a swap of 0.5GB. Follow these steps to add swap space on Ubuntu 18.04.
Start creating the file that will be used as swap space.
$ sudo fallocate -l 0.5G /swapfile
You can verify the swap file with typing command bellow
$ ls -lh /swapfile
The output should be this-
To fix the permission of the file, so that only the root user will be able to read and write it, type the command below.
$ sudo chmod 600 /swapfile
Verify the permission again using the code-
$ ls -lh /swapfile
You can see the permission is changed from
Now time to mark the file as swap space by typing:
sudo mkswap /swapfile
Now time to turn it on so that the system will start using it.
sudo swapon /swapfile
Verify that swap file is on and working typing
sudo swapon --show
Making swap file permanent
Now that we have successfully enabled the swapfile in the system, the next step will be to make the changes permanent. Otherwise, it will be reset after the next reboot. We can stop resetting it by moving the swap file to
/etc/fstab . To do that follow the below steps.
First lets backup
/etc/fstab as a precaution, by typing this-
sudo cp /etc/fstab /etc/fstab.bak
Now add swap file information to
/etc/fstab by typing this.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Adjusting swappiness value
Swappiness value tells the OS how to use the swap space, especially how aggressively it swap. It can be between 0 and 100. By default it is 60, In a lot of spaces you will read suggesting to change the swap value to 10 as it is best for servers, But in my opinion, there is no 'best for servers' value. there is 60, which is the default, and then there is "your value" which depends on usage and available memory.
If you have servers that sometimes use lots of memory, you could have them higher than the default, but I would say that some servers (database servers?) might want to keep them way lower than 60.
We can check your the swappiness value by typing this-
For me, I prefer it to keep the swappiness value to default, ie- 60. But if you want to change it you can do it by following mentioned steps.
sudo sysctl vm.swappiness=10
Change the value 10 to whatever value you plan to keep, the output should be-
However, this is temporary and resets after the reboot, in order to make it permanent we need to edit our
/etc/sysctl.conf and to do that enter the following codes:
sudo nano /etc/sysctl.conf
It will open a new screen inside the terminal, go down till the end and add
vm.swappiness=10 in the end, then enter
Y, Then Press
Removing a Swap File
You can disable the swap file by typing this command:
sudo swapoff -v /swapfile
Now we need to remove the swap file entry
/swapfile swap swap defaults 0 0 from the
/etc/fstab file, we added before. To do that type below command:
sudo nano /etc/fstab
/swapfile swap swap defaults 0 0 from the file, then enter
Y, Then Press
Now Remove the actual file:
sudo rm /swapfile
These are the steps to add a swap file and activate and configure swap space on your Ubuntu 18.04 system. If you face any problem or have feedback, let me know via a comment below, I will try to get back to as soon as possible.