How To Create a Virtual Swap Partition in Ubuntu (or GNU/Linux in General)

Swap partition in GNU/Linux serves the same purpose as the "page file" (that's what Microsoft call it) in Microsoft Windows, and unless you have 2.5-3GB or more RAM, you should have a Swap partition in GNU/Linux.

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.swap
3. Now we can add the swap to the operating system by using the below command. 
sudo swapon /mnt/512Mb.swap
4. 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/fstab 
This 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 0
Now 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.


Lukasz said...

Thank you for this article! I got many problems with forgotten swap partition and now it is easy to make it better than usual. THX!

Gayan said...


You are welcome!.

Situ said...

If your fstab already has a line for the swap partition created during installation (remember, not everybody did it like you by not creating a swap during installation), then shouldn't that line have to be commented out?

Gayan said...


You are right, I updated the post. Thank you.

Nour said...

Please Help me. I have pasted on terminal the first line, exactly as you put it but substituting 512 with 2M. It created a swap file of 16.6 GB!!!! The process was even interrupted before I could format it, and now I have a 16.6 GB file (saved in home/nmt) which is not doing anything. How do I undo this?

Nour said...

I solved the problem by completing the formatting and adding it to the OS, and then using the line:
sudo swapoff /mnt/512Mb.swap && sudo rm /mnt/512Mb.swap
This undid the change and removed the swap file from the hard disk.
Thank you anyway. Somehow posting this comment before helped to find a solution right away...

Gayan said...


No problem :).

Post a Comment