Setting Up Swap Without Actual Swap Partition in Ubuntu 10

I urgently needed Swap space but i hadn’t created a swap partition before. Then I Googled about the problem and found a useful solution which allowed me to add swap space without the need of creating swap partition.

1. First, create a large empty file:
sudo dd if=/dev/zero of=/swap_file bs=1M count=1000

Replace 1000 with the size of the swap file desired, in MB. You can also put the swap_file in a different location if desired.

2. Next, we’ll secure the swapspace, so ordinary users cannot read the contents (potential security breach):
sudo chown root:root /swap_file
sudo chmod 600 /swap_file
3. Then, turn it into swap space:
sudo mkswap /swap_file
4. Next, turn it on:
sudo swapon /swap_file

5. To make it turn on at every bootup, open up /etc/fstab:
sudo nano /etc/fstab
6. Add this line to the end of the file:
/swap_file none swap sw 0 0
Common ErrorsĀ 

If u get Error swapon: Access denied at step 4 then there is some problem with your installation or you are usings a VPS. Most of the VPS providers disable this option. Contact them

Source: Ubuntu Forums