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 --show
If this command returns no output, you don’t have any swap space configured.
-
Create swap file
sudo fallocate -l 2G /swapfile
This creates a 2GB swap file (it worked in my case). Adjust the size as needed.
-
Set permissions
sudo chmod 600 /swapfile
This restricts access to root only, enhancing security.
-
Set up swap area
sudo mkswap /swapfile
This formats the file as swap space.
-
Activate swap
sudo swapon /swapfile
This enables the swap file.
-
Make swap permanent Edit
/etc/fstab
:sudo nano /etc/fstab
Add this line:
/swapfile none swap sw 0 0
This ensures the swap file is mounted on reboot.
-
Verify swap:
sudo swapon --show
This should now display information about your new swap space.
After that, running my
docker build
script worked just fine!