Increase Swap Size on Ubuntu (Complete Guide)

There are two common ways to increase swap space on Ubuntu: swap file (recommended) and swap partition. This guide explains the swap file method, which is safe and easy for laptops and desktops.

1. Check current swap

swapon --show
free -h

If there is no output from swapon --show, it means no swap is currently active.

2. Turn off existing swap

sudo swapoff -a

3. Create or resize the swap file

Example: create an 8GB swap file:

sudo fallocate -l 8G /swapfile

If fallocate is not available or causes an error, use:

sudo dd if=/dev/zero of=/swapfile bs=1M count=8192 status=progress

4. Set permissions and format swap

sudo chmod 600 /swapfile
sudo mkswap /swapfile

5. Enable the new swap

sudo swapon /swapfile

6. Verify the swap space

swapon --show
free -h

7. Make it permanent after reboot

Edit /etc/fstab and add the following line (if it’s not already there):

/swapfile none swap sw 0 0

Example using the nano editor:

sudo nano /etc/fstab
# then add: /swapfile none swap sw 0 0
# save with Ctrl+O Enter, then exit with Ctrl+X

Important Notes:
  • The recommended swap size depends on your RAM and workload. For laptops with 8GB RAM, 8GB to 16GB swap is often ideal.
  • Using a swap file is generally easier and safer than resizing disk partitions.
  • Swap can help prevent crashes when running heavy apps like Android Studio or emulators.

0 Comments:

Post a Comment