How To Add Swap Space on Ubuntu 18.04 VPS

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.

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.

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

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.

[simterm]
NAME TYPE SIZE USED PRIO
/swapfile file 2G 93.7M -2
[/simterm]

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

$ 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-

[simterm]
$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 211M 0 211M 0% /dev
tmpfs 49M 620K 48M 2% /run
/dev/vda1 9.8G 2.6G 6.8G 28% /
tmpfs 241M 0 241M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 241M 0 241M 0% /sys/fs/cgroup
tmpfs 49M 0 49M 0% /run/user/0[/simterm]

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-

[simterm]
$ ls -lh /swapfile
-rw-r–r– 1 root root 500M Nov 15 21:42 /swapfile[/simterm]

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

[simterm]
$ ls -lh /swapfile
-rw——- 1 root root 500M Nov 15 21:42 /swapfile [/simterm]

You can see the permission is changed from -rw-r--r-- to -rw-------.

Now time to mark the file as swap space by typing:

sudo mkswap /swapfile

Output-

[simterm]
$ sudo mkswap /swapfile
Setting up swapspace version 1, size = 500 MiB (524283904 bytes)
no label, UUID=e8332ed3-ac7f-4804-b949-3b4e8485ecef[/simterm]

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

Output-

[simterm]
$ sudo swapon –show
NAME TYPE SIZE USED PRIO
/swapfile file 500M 0B -2[/simterm]

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

Output-

[simterm]
$ echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab
/swapfile none swap sw 0 0[/simterm]

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-

cat /proc/sys/vm/swappiness

Output

[simterm]
$ cat /proc/sys/vm/swappiness
60[/simterm]

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-

[simterm]
$ sudo sysctl vm.swappiness=10
vm.swappiness = 10[/simterm]

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 CTRL+X, then Y, Then Press Enter .

vm.swappiness=10

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

Now remove /swapfile swap swap defaults 0 0 from the file, then enter CTRL+X, then Y, Then Press Enter .

Now Remove the actual file:

sudo rm /swapfile

Conclusion

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.

I am a no-code WP Developer. I am obsessed with Faster Loading Websites, Design, #SEO, #WordPress, #SaaS, and #WebPerformance.

4 thoughts on “How To Add Swap Space on Ubuntu 18.04 VPS”

Leave a Comment

recent blogarticles

[widget id="wp_coupons_widget-3"]
Malcare WordPress Security