Most part of public cloud providers use official disk images to deploy their cloud instances, but when your public cloud instance is deployed, there is no swap partition available. It can be an issue if you have a server without a lot of RAM, or if you launch too many applications.
In this short tutorial, we will see how to add swap memory on a cloud instance. In our example, we will use Ubuntu 18.04 LTS.
Creating swap directory
At first we have to create a directory and to prepare it to be used a swap. In our example, we will add 1GB swap
Create a swap directory of 1GB
dd if=/dev/zero of=/var/swap bs=1k count=1024k
Prepare the directory /var/swap to use it as swap
mkswap /var/swap
swapon /var/swap
Mounting the swap
To add our swap directory in our server configuration, we will add it in the file /etc/fstab. This way, it will be persistent after a reboot
echo '/var/swap swap swap defaults 0 0' | sudo tee -a /etc/fstab