Tools: How to Add Swap Space on Ubuntu (5GB Example)

Tools: How to Add Swap Space on Ubuntu (5GB Example)

Description

Disclamer

Why Swap Matters

Step 1: Create the Swap File

Step 2: Lock Down Permissions

Step 3: Format as Swap

Step 4: Enable the Swap

Step 5: Make It Permanent

Step 6: Verify

Bonus: Tune Swappiness

Swappiness reference

Quick Summary A step-by-step guide to creating a swap file on Ubuntu, making it persistent across reboots, and tuning swappiness for better performance. This article was made with Ai, but I have tested all these commands since I needed swap too Swap is disk space that your OS uses as overflow when physical RAM fills up. Without it, the kernel may start killing processes (the dreaded OOM killer) when memory is exhausted. With it, your system degrades gracefully instead of crashing. My setup: 7.57 GB RAM, 0 KB swap. Adding 5 GB swap gives the system room to breathe under heavy loads. fallocate instantly allocates the space on disk without writing zeroes - fast and efficient. If for some reason fallocate isn't available, use dd as a fallback: This ensures only root can read/write the swap file - important for security. A world-readable swap file is a potential data leak. You should see output like: Without this step, your swap disappears after a reboot. This appends the swap entry to /etc/fstab, which is read at boot time. You should see something like: Swap is now active. ✅ By default, Ubuntu sets vm.swappiness=60, which tells the kernel to start using swap when RAM usage hits ~40%. For a desktop or workstation with plenty of RAM, that's too aggressive — you want to stay in RAM as long as possible. Check your current value: Set it to 10 (apply immediately): Make it permanent across reboots: For most desktop users with 4GB+ RAM, 10 is the sweet spot. Templates let you quickly answer FAQs or store snippets for re-use. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Command

Copy

$ -weight: 600;">sudo fallocate -l 5G /swapfile -weight: 600;">sudo fallocate -l 5G /swapfile -weight: 600;">sudo fallocate -l 5G /swapfile -weight: 600;">sudo dd if=/dev/zero of=/swapfile bs=1M count=5120 -weight: 600;">sudo dd if=/dev/zero of=/swapfile bs=1M count=5120 -weight: 600;">sudo dd if=/dev/zero of=/swapfile bs=1M count=5120 -weight: 600;">sudo chmod 600 /swapfile -weight: 600;">sudo chmod 600 /swapfile -weight: 600;">sudo chmod 600 /swapfile -weight: 600;">sudo mkswap /swapfile -weight: 600;">sudo mkswap /swapfile -weight: 600;">sudo mkswap /swapfile Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Setting up swapspace version 1, size = 5 GiB (5368705024 bytes) no label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx -weight: 600;">sudo swapon /swapfile -weight: 600;">sudo swapon /swapfile -weight: 600;">sudo swapon /swapfile echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab total used free shared buff/cache available Mem: 7.5Gi 3.2Gi 1.1Gi 512Mi 3.2Gi 3.6Gi Swap: 5.0Gi 0B 5.0Gi total used free shared buff/cache available Mem: 7.5Gi 3.2Gi 1.1Gi 512Mi 3.2Gi 3.6Gi Swap: 5.0Gi 0B 5.0Gi total used free shared buff/cache available Mem: 7.5Gi 3.2Gi 1.1Gi 512Mi 3.2Gi 3.6Gi Swap: 5.0Gi 0B 5.0Gi cat /proc/sys/vm/swappiness cat /proc/sys/vm/swappiness cat /proc/sys/vm/swappiness -weight: 600;">sudo sysctl vm.swappiness=10 -weight: 600;">sudo sysctl vm.swappiness=10 -weight: 600;">sudo sysctl vm.swappiness=10 echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf # 1. Create -weight: 600;">sudo fallocate -l 5G /swapfile # 2. Secure -weight: 600;">sudo chmod 600 /swapfile # 3. Format -weight: 600;">sudo mkswap /swapfile # 4. Enable -weight: 600;">sudo swapon /swapfile # 5. Persist echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab # 6. Verify free -h # Bonus: tune swappiness -weight: 600;">sudo sysctl vm.swappiness=10 echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf # 1. Create -weight: 600;">sudo fallocate -l 5G /swapfile # 2. Secure -weight: 600;">sudo chmod 600 /swapfile # 3. Format -weight: 600;">sudo mkswap /swapfile # 4. Enable -weight: 600;">sudo swapon /swapfile # 5. Persist echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab # 6. Verify free -h # Bonus: tune swappiness -weight: 600;">sudo sysctl vm.swappiness=10 echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf # 1. Create -weight: 600;">sudo fallocate -l 5G /swapfile # 2. Secure -weight: 600;">sudo chmod 600 /swapfile # 3. Format -weight: 600;">sudo mkswap /swapfile # 4. Enable -weight: 600;">sudo swapon /swapfile # 5. Persist echo '/swapfile none swap sw 0 0' | -weight: 600;">sudo tee -a /etc/fstab # 6. Verify free -h # Bonus: tune swappiness -weight: 600;">sudo sysctl vm.swappiness=10 echo 'vm.swappiness=10' | -weight: 600;">sudo tee -a /etc/sysctl.conf