Perhaps you've already installed your Ubuntu operating system, and for some reason or the other you forgot swap, or deleted it among other things that may happen. In my case I had it on a different drive which I needed. I simply didn't want to reformat my installation, and certainly didn't want to add another drive for a simple swap. Thus, one way to get around this without recreating a swap partion is to create a swap partition "file". Keep in mind it will do the job just so as long as you don't end up with a really full disk drive through which the swap file may be created fragmented. Below you'll find how to accomplish this.
1. Firstly, you must create an empty file, I decided I wanted a 5GB swap file. (1K * 5M = 5GB). You can change the 5 to whatever number of gigs you want to create.
2. Second, we need to convert this newly created file into the swap space file.
3. This file needs to be enabled for paging and swaping by using the following command.
Results should be similar to the image below:
4. We need to make this remain on system by adding it to our fstab file, this enables it to remain upon reboots.
5. Finally, we need to make final checks after reboot. Retest swap file on startup by using the following commands:
published by Alberto J. Matus