Increase Linux Ubuntu Swap file for faster performance

YazanBakour
2 min readApr 27, 2021

Many developers are facing high ram usage on their Linux machine, and instead of upgrading your ram!! You can borrow space from your SSD and add it t the Swap to act as additional ram.

Swap file for ubuntu is alternative to what RAM does for your system, For instance if your RAM is 4G, you can create a SWAP file with another 4G to add more space to your RAM.

Note : use command line terminal.

1. Check your system for SWAP information :

$ sudo swapon — show
If you get no output means your system doesn’t have swap space available.

$ free -h

Swap: 0B 0B 0B

If your swap file is been used then you have to stop it and remove it in order to create new one with higher capacity desired.

$ sudo swapoff /swapfile

$sudo rm /swapfile

2. Check available space in your hard drive :

$ df -h

/dev/vda1 20G 1.1G 18G 6% /

3. Create swap file :

$ sudo fallocate -l 4G /swapfile

4.Verify the file :

$ ls -lh /swapfile

-rw-r — r — 1 root root 1.0G Apr 25 11:14 /swapfile

5. Enable the swap file :

$ sudo chmod 600 /swapfile

Verify permission:

$ ls -lh /swapfile

-rw — — — — 1 root root 1.0G Apr 25 11:14 /swapfile

Mark the file as swap :

$ sudo mkswap /swapfile

6 Allow the system to utlize the file :

$ sudo swapon /swapfile

Check if the file now available :

$ sudo swapon — show

NAME TYPE SIZE USED PRIO

/swapfile file 1024M 0B -1

$ free -h

Swap: 1.0G 0B 1.0G

7 Make swap file permanent:

Optional Backup the original file first incase

$ sudo cp /etc/fstab /etc/fstab.bak

Optional add swap file the information to /etc/fstab.

Sometimes the file system fail to boot when you restart because of bad echo, so when you boot it will redirect you to safe mode terminal, go to nano /etc/fstab and remove the added line, then reboot -n.

$ echo ‘/swapfile none swap sw 0 0’ | sudo tee -a /etc/fstab

--

--

YazanBakour

Web developer, who loves to create new features for users