Creating additional swap space

What do you do if you need additional swap space?
Or you forgot to set up a swap partition during installation, and don't want to reinstall?
You don't want to repartition your hard disk, as it could be risky.
Or, you have a need for temporary swap space until you get some more memory installed. 

All is not lost.  You can set up some swap space easily without having to reinstall.  Without having to repartition to fit it in, or adding a new hard disk to the system.

Creating a virtual swap partition

First, we need to create a file to use for the swap.  Make sure this is on a partition where you have enough space free, and then do this:

dd if=/dev/zero of=/mnt/swapfile bs=1024 count=1024000

This will create a swapfile of 1GB in size.  Feel free to adjust the count value as to your requirements, as well as the block size (bs).

Activating the swap partition

Now, it's just a case of activating the swap partition:

mkswap /mnt/swapfile
swapon /mnt/swapfile

The first command makes it by formatting the file created with the dd command.  The second one activates it so that the system can use it.

Enabling swap each time the system restarts

Now, of course, you could do it manually after each restart of the system with the commands above, but it's far easier to just add it to the /etc/fstab file.  Here's what you need to add:

/mnt/swapfile swap swap defaults 0 0

and you're done.  Now you've got some additional swap space without too much effort.