Quickly add Swap to an Ubuntu Linux VM

Whether you are on Digital Ocean, Azure, or Scaleway (and others), you do not get swap space setup for free. Use this little script below to quickly and easily add some space.

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo echo "/swapfile   none    swap    sw    0   0" >> /etc/fstab;

This will give you 1Gig of swap which should be enough for many applications. There are, of course, many factors which affect your needs. Generally the rule-of-thumb is to allocate one-half the amount of physical RAM on the machine. The above example assumes 2Gig of physical RAM.

Show Comments