{"id":899,"date":"2026-10-22T13:00:00","date_gmt":"2026-10-22T13:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=899"},"modified":"2026-10-22T13:00:00","modified_gmt":"2026-10-22T13:00:00","slug":"add-swap-space-ubuntu-24-04","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/22\/add-swap-space-ubuntu-24-04\/","title":{"rendered":"How to Add Swap Space on Ubuntu 24.04 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on Ubuntu 24.04 by creating a protected 1 GiB swap file and enabling it for the current session and future boots. You can normally finish in five to ten minutes, giving a small VPS a disk-backed buffer during short memory spikes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n\n<li>Ubuntu Server 24.04 LTS (Noble Numbat) on a VPS or dedicated server, with at least 1.3 GiB of free space on the filesystem that will hold <code>\/swapfile<\/code>.<\/li>\n\n\n<li>An SSH connection as <code>root<\/code>, or a normal user with working <code>sudo<\/code> privileges.<\/li>\n\n\n<li>The path <code>\/swapfile<\/code> must be unused; do not overwrite an existing application file or active swap area.<\/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, so it is a safety margin rather than a substitute for enough memory. This guide uses 1 GiB; choose a different size only when you have measured memory pressure and enough free disk space.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Ubuntu, the root filesystem, and current swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the release, filesystem type, free disk, and existing swap before creating anything so you do not fill the system volume or replace a working configuration.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grep -E '^(PRETTY_NAME|VERSION_ID)=' \/etc\/os-release\nfindmnt -no FSTYPE \/\nfree -h\nswapon --show\ndf -h \/\nif [ -e \/swapfile ]; then\n  ls -lh \/swapfile\nelse\n  echo \"\/swapfile is available\"\nfi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On a fresh Ubuntu 24.04 VPS without swap, the version identifies Noble, <code>swapon --show<\/code> has no data rows, and the final check confirms that the target path is free:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Ubuntu 24.04.3 LTS\"\nVERSION_ID=\"24.04\"\next4\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       420Mi       1.0Gi        12Mi       580Mi       1.4Gi\nSwap:              0B          0B          0B\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.4G   32G  17% \/\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Your device names, minor release, and memory values will differ. If <code>swapon --show<\/code> already lists a partition or file, keep it unless you have a specific reason to add more. If <code>\/swapfile<\/code> exists, stop here and choose another unused filename rather than replacing it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The filesystem line matters because Btrfs handles swap files differently from ext4 and XFS. Use the matching creation method in the next step.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and protect the swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Prepare a fully allocated file and restrict it to root before enabling it; the permission check prevents ordinary local users from reading memory pages that the kernel writes to disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ext4 or XFS<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For the common ext4 or XFS root filesystem, allocate 1 GiB with <code>fallocate<\/code> and give it a swap signature:<\/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 new UUID and a size close to 1 GiB. The UUID is informational here because the persistent configuration below uses the file path:<\/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<h3 class=\"wp-block-heading\">Btrfs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If Step 1 reported <code>btrfs<\/code>, use Ubuntu 24.04\u2019s Btrfs-aware helper. It creates the non-copy-on-write, preallocated file that the kernel requires:<\/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 chown root:root \/swapfile\nsudo chmod 0600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Do not run both creation blocks. For the command behavior and swap-file restrictions, see Ubuntu\u2019s <a href=\"https:\/\/manpages.ubuntu.com\/manpages\/noble\/en\/man8\/mkswap.8.html\" target=\"_blank\" rel=\"noopener\"><code>mkswap<\/code> manual<\/a> and <a href=\"https:\/\/manpages.ubuntu.com\/manpages\/noble\/en\/man8\/swapon.8.html\" target=\"_blank\" rel=\"noopener\"><code>swapon<\/code> manual<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Enable swap now and at boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating the signature does not make the kernel use the file; activate it now and add one guarded <code>\/etc\/fstab<\/code> entry for later boots.<\/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 sw 0 0' | sudo tee -a \/etc\/fstab\nfi\nsudo systemctl daemon-reload\nsudo swapon \/swapfile\nsudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The final command should include a row similar to this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>NAME      TYPE  SIZE USED PRIO\n\/swapfile file    1G   0B   -2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The priority can be different when Ubuntu already has another swap area. The important results are that <code>\/swapfile<\/code> appears and its size is close to 1 GiB. The conditional append prevents this procedure from adding the same boot entry twice.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Adjust swappiness only for a measured workload<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu\u2019s <code>vm.swappiness<\/code> value controls how readily the kernel moves inactive pages from RAM to swap, so inspect it before changing a setting that affects the whole server.<\/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 VPS that should prefer RAM and use swap mainly when memory pressure rises, test 10 during 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 your workload, save it in a separate file and apply it immediately:<\/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 just to eliminate disk activity. Frequent swapping usually means the server needs more RAM or the application needs tighter memory limits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the active and persistent configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run these checks together to prove that Ubuntu is using the file, that it is root-only, and that the same path will be enabled after a reboot:<\/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 includes an active 1 GiB file, a nonzero swap total, mode <code>-rw-------<\/code>, and one matching boot entry:<\/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 sw 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 disk space 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 is often sparse, partly allocated, or a copy-on-write file on Btrfs. First run <code>findmnt -no FSTYPE \/<\/code> and <code>sudo swapon --show<\/code>. If the failed file is not active, remove it and repeat the Btrfs block in Step 2 for Btrfs; on ext4 or XFS, recreate it fully with <code>dd<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/swapfile\nsudo dd if=\/dev\/zero of=\/swapfile bs=1M count=1024 status=progress\nsudo chown root:root \/swapfile\nsudo chmod 0600 \/swapfile\nsudo mkswap \/swapfile\nsudo swapon \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only remove the file after <code>sudo swapon --show<\/code> confirms that it is not active. After the fallback succeeds, rerun the persistence commands in Step 3 if the <code>\/etc\/fstab<\/code> entry was not already added.<\/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 filesystem containing <code>\/swapfile<\/code> cannot provide 1 GiB. Check <code>df -h \/<\/code>, free only data you recognize as unnecessary, or use a smaller inactive file such as 512 MiB with <code>sudo fallocate -l 512M \/swapfile<\/code>; keep enough space for updates, logs, and application data.<\/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 VPS may be running inside a container whose host does not allow guests to manage swap. This is a virtualization policy, not a missing Ubuntu package; ask the provider for host-managed swap or use a virtualization plan that permits guest swap files.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><code>swapon: \/swapfile: swapon failed: Device or resource busy<\/code><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The path is already active or another process has it open. Check <code>sudo swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if the file is listed as active, keep it and remove any duplicate <code>\/etc\/fstab<\/code> line instead of overwriting it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQ<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How much swap should an Ubuntu 24.04 VPS have?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no universal RAM-to-swap formula. A 1 GiB file is a reasonable starting point for a small VPS with enough free disk, while a memory-heavy service may need more headroom. Leave space for Ubuntu updates, logs, and application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does swap increase the physical RAM in Ubuntu?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Swap is disk-backed virtual memory and is much slower than RAM. It can give a process time to survive a short memory spike, but sustained swap use points to a memory or workload-sizing problem.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use a swap partition instead of <code>\/swapfile<\/code>?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A dedicated partition can be initialized with <code>mkswap<\/code> and referenced by UUID in <code>\/etc\/fstab<\/code>. A file is usually easier on a VPS because you can resize it without repartitioning the virtual disk.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I remove the swap file without rebooting?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, provided the server has enough free RAM to stop using it. Confirm the exact path with <code>sudo swapon --show<\/code>, run <code>sudo swapoff \/swapfile<\/code>, remove only its line from <code>\/etc\/fstab<\/code>, and delete the inactive file; do not 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\": \"How much swap should an Ubuntu 24.04 VPS have?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"There is no universal RAM-to-swap formula. A 1 GiB file is a reasonable starting point for a small VPS with enough free disk, while a memory-heavy service may need more headroom. Leave space for Ubuntu updates, logs, and application data.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap increase the physical RAM in Ubuntu?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. Swap is disk-backed virtual memory and is much slower than RAM. It can give a process time to survive a short memory spike, but sustained swap use points to a memory or workload-sizing problem.\"}}, {\"@type\": \"Question\", \"name\": \"Can I use a swap partition instead of `\/swapfile`?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes. A dedicated partition can be initialized with `mkswap` and referenced by UUID in `\/etc\/fstab`. A file is usually easier on a VPS because you can resize it without repartitioning the virtual disk.\"}}, {\"@type\": \"Question\", \"name\": \"Can I remove the swap file without rebooting?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes, provided the server has enough free RAM to stop using it. Confirm the exact path with `sudo swapon --show`, run `sudo swapoff \/swapfile`, remove only its line from `\/etc\/fstab`, and delete the inactive file; do not remove an active swap file.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Add swap space on Ubuntu 24.04 with a protected 1 GiB file, Btrfs-safe setup, boot persistence, verification, and common VPS fixes for memory headroom.","protected":false},"author":1,"featured_media":900,"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,102],"class_list":["post-899","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-linux-vps","tag-swap","tag-ubuntu-24-04","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/899","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=899"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/899\/revisions"}],"predecessor-version":[{"id":3687,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/899\/revisions\/3687"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/900"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=899"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=899"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=899"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}