Skip to main content

Change SWAP memory in Linux/Debian

Introduction​

To safely increase your swap size on Ubuntu without affecting your existing files, follow the method of creating a new swap file (instead of touching existing partitions). This is safe, easy, and reversible.

Let’s say you want to increase swap from 2 GB to 4 GB.

1️⃣ Step 1: Check current swap​

sudo swapon --show
free -h

πŸ“¦ Step 2: Turn off current swap (temporarily)​

sudo swapoff -a

🧹 Step 3: Remove old swap file (only if you're replacing /swapfile)​

Skip this if you plan to keep the old one.

sudo rm /swapfile

🧱 Step 4: Create a new swap file (e.g., 4 GB)​

Here instead of 4G you can change the size, For e.g.: 6G, 8G, 16G and so on.

sudo fallocate -l 4G /swapfile
# If fallocate fails (e.g., on some file systems), use:
# sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

πŸ” Step 5: Set proper permissions​

sudo chmod 600 /swapfile

πŸŒ€ Step 6: Make it swap​

sudo mkswap /swapfile

βœ… Step 7: Enable swap​

sudo swapon /swapfile

Verify:

sudo swapon --show
free -h

πŸ’Ύ Step 8: Make it permanent (survives reboots)​

Edit /etc/fstab:

sudo nano /etc/fstab

Make sure you have this line:

/swapfile none swap sw 0 0

βœ… All Done! Your server now has a bigger swap space.

πŸ“Œ Safety Tips

  • This method doesn't touch existing partitions β€” so your data is safe.
  • Don’t use a swap file on SSDs heavily unless absolutely needed (wear).
  • Don’t make swap too large (e.g., > 8 GB) unless running memory-heavy apps.

Congratulations! πŸŽ‰β€‹

You have successfully change the size of swap memory in your system. βœ