If you already have created a separate swap partition then you don't need to read this. But remember by having a virtual partition that can be used as a swap file do have its advantageous.
For instance, let's say that you upgraded from 512MB to 1GB of your RAM recently. If you have a swap partition (real one) then you have to delete it or "resize" it which is not that easy sometimes (especially if you have a less user-freindly GNU/Linux distro).
But when you have a "virtual" partition or a swap file, then all you gotta do is open your Terminal and resize it which can be done in seconds!. Although some say by having a separate real partition for your Swap space enhances the security (I really don't know if it's true or not) + speed up the PC in general which in my experience is simply not true. Maybe it does but I certainly didn't "detect" it, anyway.
So if you're like me, still want a virtual swap partition in Ubuntu 11.04 or in any other Linux distribution, then let's do it!. I assume that you don't have a swap partition already (which should be true if you received a warning while installing Ubuntu saying that you "couldn't detect a swap partition" but you ignore creating one and headed for installation).
If you already have a real swap partition then make sure to properly unmount it before proceeding.
How much Swap space do I need?
If you have "B" amount of RAM then you need at least 2B of swap space. So that's two times the amount of your RAM (if you have 512MB of RAM then you need 1GB of swap space).
If you have a mammoth of 3GB or more of RAM in your PC, then most probably you don't need a swap. But if it's a server or any other heavy duty PC... then having a swap wouldn't hurt (not sure if I'm totally correct about that one).
You need swap if you do a lot of video editing, use Firefox or other major web browsers, LibreOffice, etc... because these apps are known for their RAM "eating" features.
Enough talking... let's do it!
1. Open your Terminal and enter the below command to create you Swap file which is about 512MB (megabytes) in this example.
sudo dd if=/dev/zero of=/mnt/512Mb.swap bs=1M count=512
Now depending on your RAM size, calculate the swap file size you need according to the above paragraph (2times the RAM size) and replace the value in: "count=512". Do not change anything other than the value 512.
You can also change the "512Mb" (shown in Red above) part to your desired name... if you do so you'll have to change all the below commands to suit that name ... so it's better you don't change anything other than the actual size.
Since "512Mb" is just a name it has nothing to do with your real value that you replace the "512" as mentioned above. So just, please leave it alone.
2. Let's format the swap file using the below command.
sudo mkswap /mnt/512Mb.swap3. Now we can add the swap to the operating system by using the below command.
sudo swapon /mnt/512Mb.swap4. But if we stop from here, the swap will be used by OS until we do a reboot. This is added temporarily, if we want to mount this every time the OS boots (which we should), then again use the below command in your Terminal.
sudo gedit /etc/fstabThis should bring a graphical text editor as shown below (the codes in the opened text might be slightly different depending on your PC configuration). Just copy and paste the below code and add it inside of the text, find a blank space.
Update: If you had created a swap partition before, then make sure to remove its entry before pasting the below one.
/mnt/512Mb.swap none swap sw 0 0Now simply save the file and do a reboot. Now we've successfully created a virtual swap file in Ubuntu 11.04 (in this example) or these command should work in other GNU/Linux distributions as well.