How to add swap on Ubuntu

Recently I had to add swap on Ubuntu, due to a memory limit while ran npm install on a 512MB RAM VPS. So here is how to add swap to deal with it. You can also add swap to improve your machine performance. 📈

💡 Keep in mind that swap allows us to allocate disk space to the operating system for use as RAM.

Adding swap

👨‍💻 Just run in the console:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

This will create a 1Gb of swap space and that's all. 👏

✅ You can check if it's created and ready running:

free -m

👁 You should see a Swap row 👇

          total      used      free    shared  buff/cache   available
Mem:        488        43        71         2         373         415
Swap:      1023         0      1023

We're done! Leave a comment with your doubts or experience 👋

David Burgos

Read more posts by this author.