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.

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.

Subscribe to Candid and colorful thoughts on enterprise readiness

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe