Blog Archives

Ubuntu: Make a swap file.

The main question why would you need a swap file when you can opt for a swap partition while installing…? Well people like me who normally screw up their hard disks with multiple partitions and formats normally end up not being able to create any more primary or logical extensions to the existing partitions and hence is unable to make a dedicated swap partition. This, or you forgot to set it up. Having a swap is good even when you are not going to use even half your RAM. You might never need it, but if you need it, this is how you do it.

Any way, we at hirir, have solution!!

Problem: Need swap space but cant (dont want to) make a swap partition.

Solution: Make a swap file.

Step 1: Decide location
Well, decide on a location to fix your swap partition. Ive decided on “/home/swap

Step 2: Become root!
This is becoming a habit now.

Step 3: Type into terminal: fallocate -l 4g /home/swap/4g.swap
This will create a file by the name 4g.swap in /home/swap. The file name is immaterial. Im just trying to indicate that Im making a 4GB swap. -l 4g specifies the length of the file 4g.swap. Suffixes like m g t p e are also possible. ie, mb,gb,tb and so on.

Step 4: Type into terminal: chmod 600 /home/swap/4g.swap
Here we are making it non world readable. Just to make sure no-one reads stuff from it.

Step 5: Type into terminal all over:

mkswap /home/swap/4g.swap
swapon /home/swap/4g.swap

Type into terminal: cat /proc/meminfo and see the swap there and cry out “Yippeee”

Step 6: Make it all permanent
To make it all permanent, we edit /etc/fstab
To do that type into terminal

gksudo gedit /etc/fstab

And add into it, at the end, the following line:

/home/swap/4g.swap none swap sw 0 0

All done!!

Type “free” into terminal and you will see swap space that you’ve just allocated.

Reboot and from then on you may hibernate your system. :) which is why I did all this.