Saturday, September 24, 2011

Installing a 1 GB swap 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

6 comments:

  1. Hi, thanks for this post.

    In step 2, I have to change

    dd if=/dev/null of=/swapfile bs=1024 count=1048576

    for

    dd if=/dev/zero of=/swapfile bs=1024 count=1048576

    ReplyDelete
    Replies
    1. Thanks a ton for pointing out the mistake.

      I have updated the post to reflect the same.

      Actually /dev/null and /dev/zero for can be used for dumping output of commands but when it comes to reading , there is a difference between the two.You can't read anything from /dev/null.

      Delete
  2. I have 10GB space in my home directory.Is it compulsary to create swap file to me.can I install without creating swap file(through the upper command).

    ReplyDelete
    Replies
    1. How much RAM do you have and what are you trying to install ??

      Delete
  3. I have 8GiB of RAM, can i install ubuntu 12.10 without sawp?

    ReplyDelete
    Replies
    1. The above post was mainly regarding installation of Oracle 11g XE R2 on Ubuntu. So, if you are asking about its installation, then yes in-spite of having 8GB RAM , you need to create minimum 1GB swap at least for installation purpose . You can delete it later on as I have found no impact of deleting the swap space after installation of Oracle 11g XE R2. It continues to work properly.
      But, if you are asking about installation of Ubuntu 12.10, then it is not at all necessary to create the swap space as you have more than sufficient RAM ( 8GB ... that's a really good configuration ) . Creation of swap space is not a pre-requisite for any version of Ubuntu. It is just meant to provide extra memory buffer for the programs you will running after installation of Ubuntu.If you have sufficient RAM ( say.. 4GB or more ) , then you don't need to create the swap area.

      Delete