Hi
How to create a swap file on Ubuntu
Apr 2025
Sometimes we run apps on a server that isn't quite up to spec.
In cases like these, simple tasks like npm install
can freeze or crash, leaving you wondering how you're going to get it working.
Luckily we can use a 'swap file' to virtually increase our RAM. This uses disk storage as virtual RAM in a pinch, allowing you to run jobs that would otherwise freeze.
How to do it:
# Create a 2GB swap file
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
# To make it permanent:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Where '/swapfile' can be any path on your system; it's just an identifier for the swap file.
Back to top