What is a swap space
I ran into an issue where building a NextJS app on Docker would take forever. Then, I found out I could solve this by adding a swap space to my Ubuntu VPS.
So what is a swap space?
Swap space is a designated area on a hard drive (HDD or SSD) that acts as an extension of a computer’s physical RAM. When the RAM is full, the operating system moves inactive or less frequently used data from RAM to this swap space to free up memory for active tasks. This process is known as “swapping.”
How to create a swap space
To create a swap file on an Ubuntu Linux VPS for building a Docker and NextJS application, follow these steps:
-
Check existing swap space:
sudo swapon --showIf this command returns no output, you don’t have any swap space configured.
-
Create swap file
sudo fallocate -l 2G /swapfileThis creates a 2GB swap file (it worked in my case). Adjust the size as needed.
-
Set permissions
sudo chmod 600 /swapfileThis restricts access to root only, enhancing security.
-
Set up swap area
sudo mkswap /swapfileThis formats the file as swap space.
-
Activate swap
sudo swapon /swapfileThis enables the swap file.
-
Make swap permanent Edit
/etc/fstab:sudo nano /etc/fstabAdd this line:
/swapfile none swap sw 0 0This ensures the swap file is mounted on reboot.
-
Verify swap:
sudo swapon --showThis should now display information about your new swap space.
After that, running my
docker buildscript worked just fine!