How to Add Swap on VPS?

A swap file is extra space on the disk that the operating system uses for temporary storage of virtual memory data. This allows you to increase the amount of available memory and improve server performance. If you have a VPS with a limited amount of RAM and you need to increase it, adding swap can help solve this problem.

Let’s figure out how to add swap on a VPS.

1. Check Available Space

Before creating a swap file, make sure you have enough free disk space. You can check the available space using the command:

df -h

VPS Hosting

Virtual servers with guaranteed resources

Choose VPS

2. Create a Swap File

To create a swap file, execute the following commands:

sudo fallocate -l 1G /swapfile

sudo chmod 600 /swapfile

sudo mkswap /swapfile

sudo swapon /swapfile

To tell the system to use the swap file on boot, add it to the /etc/fstab file:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

3. Verify the Created Swap

To verify that the swap file has been successfully created and is being used, run the command:

sudo swapon --show

Now you have successfully added swap on your VPS! Use the additional virtual memory to improve server performance.

Remember that using swap can slow down the system, so it is recommended to use it only when necessary.