Tag Archives forubuntu

Add swap space to Ubuntu VPS

logo-ubuntuA few days ago a cronjob did not finish. After some digging through the log files I found out it was killed by the kernel because the server ran out of memory.

I did not want to upgrade to a plan with more memory for this VPS, because the current amount of memory is more than enough for normal operation. So I decided to add a swap file for this server. I found a good explanation on how to do this in this DigitalOcean article.

However, if you are looking to quickly add a swap file yourself without reading the complete article, here are the commands I used to create a swap file of 4 gigabyte for my Ubuntu 14.04 VPS:


sudo fallocate -l 4G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo "/swapfile   none    swap    sw    0   0" | sudo tee /etc/fstab -a
sudo sysctl vm.swappiness=10
echo "vm.swappiness=10" | sudo tee /etc/sysctl.conf -a
sudo sysctl vm.vfs_cache_pressure=50
echo "vm.vfs_cache_pressure=50" | sudo tee /etc/sysctl.conf -a

If you want to create a swap file of some other size you should change the call to fallocate in the first line. For more details on the other commands in the example above you should read the DigitalOcean article, it explains them all quite well.

January 14, 2016