{"id":903,"date":"2026-10-23T03:00:00","date_gmt":"2026-10-23T03:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=903"},"modified":"2026-10-23T03:00:00","modified_gmt":"2026-10-23T03:00:00","slug":"add-swap-space-ubuntu-26-04","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/23\/add-swap-space-ubuntu-26-04\/","title":{"rendered":"How to Add Swap Space on Ubuntu 26.04 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on Ubuntu 26.04 with a root-only 1 GiB file that the kernel can use when RAM briefly runs short. You can finish in about five to ten minutes, without rebooting the server during setup.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>An Ubuntu 26.04 LTS VPS or dedicated server with at least 1.3 GiB of free space on the filesystem mounted at <code>\/<\/code>; keep additional room for updates and logs.<\/li>\n\n\n<li>SSH access as <code>root<\/code>, or a regular user with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The <code>\/swapfile<\/code> path must be unused. Ubuntu images may already have <code>\/swap.img<\/code> or another swap device, so check before creating anything.<\/li>\n\n\n<li>A <a href=\"https:\/\/vpslake.com\/buy-vps\">VPSLake Linux VPS<\/a> if you need an Ubuntu server for this procedure.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Swap is slower than RAM and does not increase physical memory. It is a safety margin for temporary pressure, not a fix for a workload that continuously exhausts the server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Ubuntu 26.04 and the current storage state<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the release, root filesystem, free disk space, and active swap first so you do not overwrite an existing file or fill the root volume.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^(PRETTY_NAME|VERSION_ID)=' \/etc\/os-release\nfindmnt -no FSTYPE \/\ndf -h \/\nfree -h\nswapon --show\ntest -e \/swapfile &amp;&amp; ls -lh \/swapfile || echo \"\/swapfile is available\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a typical fresh Ubuntu 26.04 VPS without an active swap file, you will see <code>VERSION_ID=\"26.04\"<\/code>, a filesystem such as <code>ext4<\/code>, an empty <code>swapon --show<\/code> table, and this final message. Canonical identifies this LTS release as Resolute Raccoon in the <a href=\"https:\/\/documentation.ubuntu.com\/release-notes\/26.04\/summary-for-lts-users\/\" target=\"_blank\" rel=\"noopener\">Ubuntu 26.04 release notes<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Ubuntu 26.04 LTS\"\nVERSION_ID=\"26.04\"\next4\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.4G    32G  17% \/\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       420Mi       1.0Gi        12Mi       580Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your device name, memory values, and filesystem can differ. If <code>swapon --show<\/code> already lists <code>\/swap.img<\/code> or a partition, you already have swap; add another file only if the workload needs more. If <code>\/swapfile<\/code> exists, choose another unused path rather than replacing it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and secure a 1 GiB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Allocate the file and restrict it to root before writing the swap signature; this prevents ordinary users from reading pages that the kernel moves to disk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an <code>ext4<\/code> or <code>xfs<\/code> root filesystem, run this block:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fallocate -l 1G \/swapfile\nsudo chown root:root \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mkswap<\/code> should report a swap area close to 1 GiB and print a generated UUID:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Setting up swapspace version 1, size = 1 GiB\nno label, UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If Step 1 reported <code>btrfs<\/code>, do not use <code>fallocate<\/code> alone. Install the Btrfs tools from Ubuntu\u2019s repositories and let their helper create a preallocated swap file that Btrfs can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install -y btrfs-progs\nsudo btrfs filesystem mkswapfile --size 1G \/swapfile\nsudo chmod 0600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Run only one of the two creation blocks. Ubuntu\u2019s <a href=\"https:\/\/manpages.ubuntu.com\/manpages\/resolute\/en\/man8\/swapon.8.html\" target=\"_blank\" rel=\"noopener\"><code>swapon<\/code> documentation<\/a> describes the kernel requirements for swap files, including why filesystem layout matters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable the new swap immediately<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Formatting a file prepares it but does not make the running kernel use it; <code>swapon<\/code> activates it without a restart.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon \/swapfile\nsudo swapon --show\nfree -h<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The new file should appear as an active row and the <code>Swap<\/code> total should be close to 1 GiB:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    1G   0B   -2\nSwap:          1.0Gi          0B     1.0Gi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The priority may differ when another swap area is already enabled. The important checks are that <code>\/swapfile<\/code> is listed and that its size is nonzero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Persist swap across reboots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one <code>\/etc\/fstab<\/code> record so Ubuntu enables this same file during boot, while the guard avoids adding a duplicate entry if you rerun the command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ! sudo grep -qE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab; then\n  echo '\/swapfile none swap defaults 0 0' | sudo tee -a \/etc\/fstab\nfi\nsudo systemctl daemon-reload\nsudo grep -nE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The last command should show one active line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Swap is already active for this session from Step 3. The <code>fstab<\/code> entry is what restores it after a reboot; <code>systemctl daemon-reload<\/code> refreshes systemd\u2019s generated view of the changed file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Review swappiness before tuning it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> value influences how readily Ubuntu moves inactive pages to swap, so inspect it before changing the kernel\u2019s default behavior.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For a small VPS where swap should mainly absorb brief memory pressure, you can test a value of 10 for the current boot:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl vm.swappiness=10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If monitoring shows that value suits the workload, make it persistent in a dedicated file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>printf '%s\\n' 'vm.swappiness = 10' | sudo tee \/etc\/sysctl.d\/60-swap.conf\nsudo sysctl -p \/etc\/sysctl.d\/60-swap.conf\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Expected output is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vm.swappiness = 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not set swappiness to zero simply to hide swap activity. If the server swaps constantly, reduce the application\u2019s memory use or move to a plan with more RAM.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Ubuntu 26.04 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run these checks together to prove that the kernel is using the file, that its permissions are safe, and that Ubuntu has a boot-time entry for it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nfree -h\nsudo stat -c '%A %U:%G %n' \/swapfile\nsudo grep -E '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Successful output contains an active file, a nonzero swap total, root-only permissions, and one matching <code>fstab<\/code> line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    1G   0B   -2\nSwap:          1.0Gi          0B     1.0Gi\n-rw------- root:root \/swapfile\n\/swapfile none swap defaults 0 0<\/code><\/pre>\n\n\n\n<aside class=\"vl-element vl-cta-banner\" aria-label=\"Add memory headroom\">\n\t<div class=\"vl-cta-banner__content\">\n\t\t<p class=\"vl-cta-banner__eyebrow\">Linux VPS<\/p>\n\t\t<h3 class=\"vl-cta-banner__title\">Add memory headroom<\/h3>\n\t\t<p class=\"vl-cta-banner__text\">Deploy an Ubuntu VPS with room for applications and a protected swap file.<\/p>\n\t<\/div>\n\t<a class=\"vl-cta-banner__button\" href=\"\/buy-vps\" aria-label=\"View Linux VPS plans\">View Linux VPS plans<\/a>\n<\/aside>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Invalid argument<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The file may be sparse, partially allocated, or on Btrfs without the required swap-file layout. Confirm the filesystem with <code>findmnt -no FSTYPE \/<\/code>; if it is Btrfs, recreate the inactive file with <code>btrfs filesystem mkswapfile<\/code> from Step 2. On <code>ext4<\/code> or <code>xfs<\/code>, recreate an inactive file with a fully written allocation:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dd if=\/dev\/zero of=\/swapfile bs=1M count=1024 status=progress\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Before running that block, confirm <code>\/swapfile<\/code> is not listed by <code>sudo swapon --show<\/code>. If it is active, do not overwrite it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>fallocate: fallocate failed: No space left on device<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The root filesystem cannot provide 1 GiB. Check <code>df -h \/<\/code>, preserve room for Ubuntu updates and logs, and use a smaller inactive file such as 512 MiB. Remove a failed file only after <code>sudo swapon --show<\/code> confirms it is not active, then repeat Step 2 with <code>512M<\/code> and <code>count=512<\/code> where applicable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>mount -a<\/code> reports a bad <code>\/etc\/fstab<\/code> entry<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An incorrectly edited <code>fstab<\/code> line can prevent the boot-time activation from being parsed. Check the line with <code>sudo grep -nE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code>, keep exactly <code>\/swapfile none swap defaults 0 0<\/code>, and run <code>sudo systemctl daemon-reload<\/code> before testing <code>sudo swapon --all --verbose<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Operation not permitted<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The virtualization layer may prohibit guest-managed swap, which cannot be fixed by reinstalling Ubuntu packages. Keep the file inactive and contact VPSLake support or use a VPS configuration that permits swap files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Why does Ubuntu 26.04 show <code>\/swap.img<\/code> instead of <code>\/swapfile<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The filename is chosen by the image or installer, and the name does not change how swap works. If <code>\/swap.img<\/code> appears in <code>swapon --show<\/code>, it is already active; keep it and size any additional swap based on actual memory pressure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How large should the Ubuntu 26.04 swap file be?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Choose a size for the workload\u2019s short-lived memory peaks rather than applying a fixed RAM multiplier. One GiB is a sensible starting point on a small VPS if the root filesystem has room; leave storage for updates, logs, and application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Will swap increase my VPS\u2019s physical RAM?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Swap uses storage as slower virtual memory and can postpone an out-of-memory failure during a brief spike. Sustained swapping usually means the workload needs more RAM or stricter memory limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I remove the swap file later?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but first confirm its exact path with <code>sudo swapon --show<\/code>, run <code>sudo swapoff \/swapfile<\/code>, and remove only its <code>\/etc\/fstab<\/code> line. Delete the file after it is inactive; never remove an active swap file.<\/p>\n\n\n\n<script type=\"application\/ld+json\">{\"@context\": \"https:\/\/schema.org\", \"@type\": \"FAQPage\", \"mainEntity\": [{\"@type\": \"Question\", \"name\": \"Why does Ubuntu 26.04 show `\/swap.img` instead of `\/swapfile`?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"The filename is chosen by the image or installer, and the name does not change how swap works. If `\/swap.img` appears in `swapon --show`, it is already active; keep it and size any additional swap based on actual memory pressure.\"}}, {\"@type\": \"Question\", \"name\": \"How large should the Ubuntu 26.04 swap file be?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Choose a size for the workload\u2019s short-lived memory peaks rather than applying a fixed RAM multiplier. One GiB is a sensible starting point on a small VPS if the root filesystem has room; leave storage for updates, logs, and application data.\"}}, {\"@type\": \"Question\", \"name\": \"Will swap increase my VPS\u2019s physical RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. Swap uses storage as slower virtual memory and can postpone an out-of-memory failure during a brief spike. Sustained swapping usually means the workload needs more RAM or stricter memory limits.\"}}, {\"@type\": \"Question\", \"name\": \"Can I remove the swap file later?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes, but first confirm its exact path with `sudo swapon --show`, run `sudo swapoff \/swapfile`, and remove only its `\/etc\/fstab` line. Delete the file after it is inactive; never remove an active swap file.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Learn how to add swap space on Ubuntu 26.04 with a secure 1 GiB file, boot persistence, Btrfs handling, verification, tuning, and fixes.","protected":false},"author":1,"featured_media":904,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","footnotes":""},"categories":[5,9],"tags":[93,92,104],"class_list":["post-903","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-linux-vps","tag-swap","tag-ubuntu-26-04","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/comments?post=903"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/903\/revisions"}],"predecessor-version":[{"id":3695,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/903\/revisions\/3695"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/904"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}