This is mostly a rewrite of http://www.cyberciti.biz/faq/linux-add-a-swap-file-howto/ , which is likely a better article than this. I'm adding this entry because it's a help to me to have it in my own files, and I wanted to condense it and add a couple minor comments.
As per the original article, a swap partition is preferred as it's likely to be slightly faster than a swap file. But this is a quick way to create more hard-drive-based memory (without needing to dedicate a partition to it), either temporarily or permanently. If you're using an SSD, this will be much faster ... and much harder on the disk. If you're using an SSD, this might be a good temporary solution, but SHOULD NOT be used permanently (SSDs have a lower mean-number-of-writes-to-failure than spinning disks, and a heavily used swap file could significantly shorten the life of your SSD).
# df -h ... # dd if=/dev/zero of=/swapfile01 bs=1024 count=1048576 # 1 Gibibyte # df -h ... # chmod 0600 /swapfile01 # mkswap /swapfile01 # swapon /swapfile01
If you want to make this permanent, edit /etc/fstab:
/swapfile1 none swap sw 0 0
Otherwise, this will stop working on reboot - which may be fine if all you want is a temporary boost of memory for a big compile.
If you use this for any length of time, you'll want to control how often the swap is used. The term in Linux is "swappiness:"