How to enable Swap on a Linux VPS
This page shows you how to add swap to a Linux server that has none. You can do it while the server is running, without stopping Hiddify.
Swap is space on the disk that Linux uses as extra memory when the RAM is full. It is much slower than RAM, but it stops programs from crashing when memory runs out.
When you need this
- Symptom: installing or updating Hiddify Manager stops with
Building the admin UI ran out of memory (needs ~1.2 GB free RAM or swap), or services on a small server stop or restart for no clear reason. - Likely cause: the server has little RAM (for example 1 GB) and no swap.
- Fix: add a swap file using the steps below, then run the install or update again.
Before you start
- You can connect to your server over SSH. See How to connect to the server via SSH.
- The disk has at least a few GB free. This page creates a 2 GB swap file as an example.
Steps
Run all commands on your server, over SSH.
-
Check whether the server already has swap:
If the output is empty, there is no swap; continue. If it lists a swap file or partition, you already have swap and do not need this page (you can check its size with
free -h). -
Check the free disk space:
Look at the Avail column. You need more free space than the swap size you want, plus some room to spare.
-
Create a 2 GB file for swap:
If this prints an error such as
fallocate failed: Operation not supported, use this command instead:If it says the file already exists, run
sudo swapon --showagain: the file may already be in use. -
Allow only the system administrator to read the file:
-
Prepare the file as swap:
-
Turn the swap on:
-
Check that it worked:
It worked if you see
/swapfilewith a size of about2G. -
Keep the swap after a reboot by adding it to
/etc/fstab(the file that lists what Linux turns on at startup). This command adds the line only if it is not there yet:
Notes
- Size: pick the swap size based on your server's RAM and free disk space. 2 GB is enough for the Hiddify installer on a small server.
- Watch usage: check memory and swap use at any time with
free -horhtop. If swap is always heavily used, the server needs more RAM. See How to monitor server resources. - Some servers cannot use swap. On some container-based VPS types (such as OpenVZ or LXC),
swaponfails withOperation not permitted. In that case, ask your provider or choose a plan with more RAM.