How can we help you?
How to Create a Swap File on Linux
A swap file is extra virtual memory on your disk. When the RAM of your VPS fills up, Linux uses the swap, so processes such as your database are not killed. Swap is slower than real memory. If your server uses swap structurally, a larger VPS plan is the better solution.
Step 1: check whether there is already swap
swapon --show
free -h Step 2: create the swap file
This creates a swap file of 2 GB. Adjust the size as desired:
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile Step 3: make the swap permanent
Add the swap file to /etc/fstab, so it is active again after a reboot:
echo '/swapfile none swap sw 0 0' >> /etc/fstab Step 4: set swappiness
Swappiness determines how quickly Linux uses swap. For a server a low value such as 10 is usually best:
sysctl vm.swappiness=10
echo 'vm.swappiness=10' >> /etc/sysctl.conf How large should swap be?
- Up to 2 GB of RAM: as much swap as RAM.
- More than 2 GB of RAM: 2 to 4 GB of swap is usually enough.
Related articles
Linux VPS at Xitens
Rent a Linux VPS with root access
Ubuntu, Debian or AlmaLinux with full root access via SSH, enterprise Ceph storage with high availability and hosting in Amsterdam.