Wednesday, October 20, 2021

Installing a 1 GB swap in Ubuntu without partitioning

Some software that you install e.g. oracle database XE requires you to have greater than one GB swap space. So, if you have not created a swap partition of greater than 1GB just because you have lot of RAM, you may land in a fix so as what do now . Although  , in ubuntu , you can easily use GParted to create a new swap space but some times you don't have enough free space or don't want to take risk of using GParted . Also , GParted is not installed by default ( u can get it from Ubuntu Live Disk ). So, we need some process by which we can have some extra swap space without using GParted.


To install a 1 GB swapfile named swapfile in /  ( root directory) , use the following steps in the terminal :

1) sudo -s 
   ( to gain root privilage, its optional , but if you don't do this then you have to append sudo to each of the following commands )

2) dd if=/dev/zero of=/swapfile bs=1024 count=1048576  
   (this may take a while)  ( 1048576 == 1 x 1024 x 1024 , 1GB == 1048576 KB)

3) mkswap /swapfile
    ( ignore any error )

4) swapon /swapfile
     ( to turn on the swap )
 
5) cp /etc/fstab /etc/fstab.orig
    ( back-up the fstab before editing )

6) echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
    ( adds the entry of  new swap to fstab for mounting the file at boot time )

7) To  Verify:

   swapon -a ; swapon -s

If at anytime u want to delete the swap then :

   sudo swapoff -a
   sudo rm /swapfile
   sudo cp /etc/fstab /etc/fstab.changed ; sudo cp /etc/fstab.orig /etc/fstab
   sudo swapon -a ; swapon -s