{
  "slug": "How-to-increase-the-size-of-the-swap-in-Ubuntu-6fdeeef9851b",
  "title": "How to increase the size of the swap in Ubuntu",
  "subtitle": "To increase the swap size in Ubuntu, you begin by disabling the current swap with sudo swapoff -a, optionally, removing the existing swap…",
  "excerpt": "To increase the swap size in Ubuntu, you begin by disabling the current swap with sudo swapoff -a, optionally, removing the existing swap…",
  "date": "2025-07-19",
  "tags": [
    "Linux"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/how-to-increase-the-size-of-the-swap-in-ubuntu-6fdeeef9851b",
  "hero": "https://cdn-images-1.medium.com/max/800/1*gdGRfll7icFAKYbQREQW5A.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "How to increase the size of the swap in Ubuntu"
    },
    {
      "type": "paragraph",
      "html": "To increase the swap size in Ubuntu, you begin by disabling the current swap with <code>sudo swapoff -a</code>, optionally, removing the existing swap file if you plan to replace it. Then, a new swap file is created, typically <code>fallocate</code> for speed (<code>sudo fallocate -l 40G /swapfile</code>) or <code>dd</code> for broader compatibility. Once the file is created, it must be secured with the correct permissions (<code>chmod 600</code>) to ensure only the root can access it. Next, the file is formatted as swap using <code>mkswap</code> and enabled with <code>swapon</code>."
    },
    {
      "type": "paragraph",
      "html": "To make the change persistent after reboot, you need to edit the <code>/etc/fstab</code> file and add the line <code>/swapfile none swap sw 0 0</code>. This ensures the swap file is automatically activated on boot. You can verify that the new swap size is active using commands like <code>swapon --show</code> or <code>free -h</code>. This approach is effective for improving system performance, especially on low-memory machines or when running memory-intensive applications, without needing to repartition your disk."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Resize the existing swap file"
    },
    {
      "type": "paragraph",
      "html": "<strong>1. Turn off swap</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo swapoff -a"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*gdGRfll7icFAKYbQREQW5A.jpeg",
      "alt": "How to increase the size of the swap in Ubuntu",
      "caption": "",
      "width": 2304,
      "height": 1792
    },
    {
      "type": "paragraph",
      "html": "<strong>2. Remove Old Swap File (if you want to replace it</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo rm /swapfile"
    },
    {
      "type": "paragraph",
      "html": "<strong>3. Create a New 40 GB Swap File</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo fallocate -l 40G /swapfile\nsudo dd if=/dev/zero of=/swapfile bs=1G count=40"
    },
    {
      "type": "paragraph",
      "html": "<strong>4. Secure the File</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\nchmod 600 /swapfile"
    },
    {
      "type": "paragraph",
      "html": "<strong>5. Format as Swap</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo mkswap /swapfile"
    },
    {
      "type": "paragraph",
      "html": "<strong>6. Enable the Swap</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo swapon /swapfile"
    },
    {
      "type": "paragraph",
      "html": "<strong>7. Make It Permanent</strong>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo nano /etc/fstab/swapfile none swap sw 0 0"
    },
    {
      "type": "paragraph",
      "html": "8. Verify Swap Size"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "swapon --showfree -h"
    }
  ]
}
