Add swap space to a server

Adapted from


I asked How do you decide how much swap to create?, Malcolm says, Same size as memory


For a WordPress server, Malcolm suggests that you install a largeish swap, then if it uses too much memory it will just run slow rather than falling over… need to set the server params to make sure it doesn’t use swap unless absolutely necessary

Examine existing swap config:

sudo swapon --show

Check available space:

df -h

Allocate file for swap and configure:

sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile

Edit fstab and apply the setting:

sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
sudo swapon --all

View the swapiness and cache pressure settings:

cat /proc/sys/vm/swappiness
#  probably displays 60 - reduce this to 10 so that swap is only used if absolutely necessary
cat /proc/sys/vm/vfs_cache_pressure
#  probably displays 100 - reduce this to 50 ease the pressure on RAM resources (unless RAM is plentiful).

Edit /etc/sysctl.conf to set new values for these - add these 2 lines to the end of the file:


Apply these settings:

sudo sysctl -p

View the settings using cat commands above or:

sudo sysctl -a