These are the procedures I followed for Ubuntu 14.04 LTS but should work with other Debian distros.

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon -s

Once you’ve done this you could make this permanent by editing the /etc/fstab file:

/swapfile none swap sw 0 0

If you’re using CentOS 7, Patrick in comments points out:

  1. The fallocate failed if you did not use ‘sudo’.
  2. the ‘swapon’ command failed on the root xfs, moved swap to ext works fine.