Set Up a Swap File (Optional)
If your server doesn’t have enough RAM, you might encounter issues, especially when building Docker images. Instead of purchasing more RAM, you can create a swap file, which acts as virtual memory. Here’s how you can add 1GB of swap to your server (you can adjust the size by replacing 1G
with 2G
for 2GB, etc.):
Video Guide
In your server SSH Terminal, paste the following commands to create SWAP.
Steps to Add 1GB Swap File:
- Create the Swap File:
sudo fallocate -l 1G /swapfile
If you get an error with fallocate, use this alternative command:
sudo dd if=/dev/zero of=/swapfile bs=1024 count=1048576
- Set the Correct Permissions (only root should access it):
sudo chmod 600 /swapfile
- Set Up the Swap Area:
sudo mkswap /swapfile
- Enable the Swap:
sudo swapon /swapfile
To make the change permanent open the /etc/fstab file and append the following line:
/swapfile swap swap defaults 0 0
- Verify Swap Activation:
sudo swapon --show
Example output:
NAME TYPE SIZE USED PRIO
/swapfile file 1024M 507M -1
You can also use:
sudo free -h
Example output:
total used free shared buff/cache available
Mem: 488M 158M 83M 2.3M 246M 217M
Swap: 1.0G 506M 517M
This step is optional, but it helps avoid memory-related issues, especially on servers with limited RAM.