{"id":897,"date":"2026-10-22T09:00:00","date_gmt":"2026-10-22T09:00:00","guid":{"rendered":"https:\/\/vpslake.com\/blog\/?p=897"},"modified":"2026-10-22T09:00:00","modified_gmt":"2026-10-22T09:00:00","slug":"add-swap-space-ubuntu-22-04","status":"publish","type":"post","link":"https:\/\/vpslake.com\/blog\/2026\/10\/22\/add-swap-space-ubuntu-22-04\/","title":{"rendered":"How to Add Swap Space on Ubuntu 22.04 (Step-by-Step)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Add swap space on Ubuntu 22.04 by creating a protected 1 GiB swap file and enabling it for the current session and future boots. You can finish in about five to ten minutes, giving a small VPS extra disk-backed headroom 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 22.04 LTS (Jammy Jellyfish) on a KVM VPS or dedicated server.<\/li>\n\n\n<li>At least 1.2 GiB of free space on the filesystem that contains <code>\/swapfile<\/code>; leave additional room for updates, logs, and application data.<\/li>\n\n\n<li>An SSH connection as <code>root<\/code>, or a regular 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 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 much slower than physical RAM. Treat it as a buffer for occasional pressure, not as a replacement for choosing a VPS with enough memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Inspect Ubuntu, storage, and existing swap<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Check the release, root filesystem, free space, and active swap first so you do not create a duplicate area or fill the system disk.<\/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\nif [ -e \/swapfile ]; then ls -lh \/swapfile; else echo \"\/swapfile is available\"; fi<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On an Ubuntu 22.04 VPS with no existing swap, the output will resemble this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PRETTY_NAME=\"Ubuntu 22.04.5 LTS\"\nVERSION_ID=\"22.04\"\next4\nFilesystem      Size  Used Avail Use% Mounted on\n\/dev\/vda1        40G  6.5G   34G  17% \/\n               total        used        free      shared  buff\/cache   available\nMem:            2.0Gi       410Mi       1.1Gi        12Mi       480Mi       1.4Gi\nSwap:              0B          0B          0B\n\/swapfile is available<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The minor release, device name, memory values, and filesystem type can 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, choose another unused path such as <code>\/swapfile-1g<\/code> instead of replacing it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ubuntu\u2019s usual VPS filesystem is ext4, for which the standard method below is appropriate. If <code>findmnt<\/code> reports <code>btrfs<\/code>, use the Btrfs-specific creation block in Step 2 rather than <code>fallocate<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create and protect a 1 GiB swap file<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Allocate the file and restrict it to root before adding the swap signature; this prevents ordinary local users from reading memory pages that the kernel writes to disk.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an ext4 or XFS root filesystem, run:<\/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 swapspace UUID and a size close to 1 GiB:<\/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 the <code>fallocate<\/code> block. Ubuntu 22.04\u2019s Btrfs tools can create the preallocated, non-copy-on-write 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 -s 1G \/swapfile\nsudo chmod 0600 \/swapfile<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Btrfs command creates and formats the file in one operation. The <a href=\"https:\/\/manpages.ubuntu.com\/manpages\/jammy\/man8\/btrfs-filesystem.8.html\" target=\"_blank\" rel=\"noopener\">Ubuntu Btrfs filesystem manual<\/a> documents this helper, while the <a href=\"https:\/\/help.ubuntu.com\/community\/SwapFaq\" target=\"_blank\" rel=\"noopener\">Ubuntu swap FAQ<\/a> explains the permissions and activation sequence.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Activate swap for the current boot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Creating the swap signature does not turn it on; <code>swapon<\/code> registers the finished file with the running kernel without a reboot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon \/swapfile\nsudo swapon --show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should see 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 differ if Ubuntu already has another swap area. The important checks are that <code>\/swapfile<\/code> appears and its size is close to 1 GiB. If activation fails, leave the file in place and use the matching troubleshooting entry rather than creating a second one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Persist the swap file across reboots<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Add one guarded <code>\/etc\/fstab<\/code> entry so Ubuntu enables the same file during boot, while avoiding duplicate lines if the command is run twice.<\/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 grep -nE '^[[:space:]]*\/swapfile[[:space:]]' \/etc\/fstab<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The final command should show one active entry:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/swapfile none swap sw 0 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>swapon<\/code> command from Step 3 handles the current boot. The <code>fstab<\/code> record is what makes the file available after a restart; <code>systemctl daemon-reload<\/code> refreshes systemd\u2019s generated configuration after the file changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Review swappiness before changing it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>vm.swappiness<\/code> setting influences how readily Ubuntu moves inactive pages to swap, so inspect the current value and change it only when the workload justifies it.<\/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\">If the VPS should prefer RAM and use swap mainly during pressure, 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\nsysctl vm.swappiness<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If monitoring shows that setting suits the workload, persist it 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<\/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. Constant swapping means the server needs more RAM or the application needs tighter memory limits; it is not solved by hiding the symptom.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify the persistent Ubuntu 22.04 swap setup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Run one final check for activation, capacity, permissions, and the boot entry. Together, these checks prove the file is usable now and configured for the next restart.<\/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 <code>Swap<\/code> 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 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 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 may be sparse, partly allocated, or a copy-on-write file on Btrfs. Check the filesystem with <code>findmnt -no FSTYPE \/<\/code>. On Btrfs, remove only an inactive failed file and recreate it with the Btrfs command in Step 2. On ext4 or XFS, recreate an inactive file with a fully written <code>dd<\/code> file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nsudo 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\">Run the <code>rm<\/code> line only when <code>swapon --show<\/code> does not list <code>\/swapfile<\/code>. The <code>dd<\/code> fallback writes every block instead of relying on filesystem allocation behavior.<\/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 holding <code>\/swapfile<\/code> cannot provide 1 GiB. Check <code>df -h \/<\/code>, preserve room for Ubuntu updates and logs, and use a smaller 512 MiB file only after confirming that a failed file is inactive:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo swapon --show\nsudo rm \/swapfile\nsudo fallocate -l 512M \/swapfile\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\">The <code>\/etc\/fstab<\/code> path remains valid because it does not store the file size. Never remove the file while it appears in <code>swapon --show<\/code>.<\/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. Run <code>sudo swapon --show<\/code> and <code>ls -l \/swapfile<\/code>; if it is already listed, keep the active file and remove any duplicate <code>fstab<\/code> entry instead of overwriting it.<\/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\">Some container-based VPS platforms do not let the guest manage swap. This is a virtualization policy, not a missing Ubuntu package, so ask the provider for host-managed swap or use a virtualization plan that permits guest swap files.<\/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 22.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 when the disk has room; size it around short memory spikes and leave storage for the OS, logs, and application data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does swap increase physical RAM?<\/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 delay an out-of-memory failure during a brief spike, but sustained swap activity means the workload needs more memory or tighter limits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I need to reboot after adding swap on Ubuntu 22.04?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>swapon \/swapfile<\/code> activates it immediately, and the <code>\/etc\/fstab<\/code> entry handles future boots. Reboot only if you want to test the boot configuration during a maintenance window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can Ubuntu 22.04 use a swap file on Btrfs?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, but a normal sparse or copy-on-write file can be rejected. Use <code>btrfs filesystem mkswapfile<\/code> as shown in Step 2, and remember that an active Btrfs swap file can affect snapshots of its containing subvolume.<\/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 22.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 when the disk has room; size it around short memory spikes and leave storage for the OS, logs, and application data.\"}}, {\"@type\": \"Question\", \"name\": \"Does swap increase physical RAM?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. Swap is disk-backed virtual memory and is much slower than RAM. It can delay an out-of-memory failure during a brief spike, but sustained swap activity means the workload needs more memory or tighter limits.\"}}, {\"@type\": \"Question\", \"name\": \"Do I need to reboot after adding swap on Ubuntu 22.04?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"No. `swapon \/swapfile` activates it immediately, and the `\/etc\/fstab` entry handles future boots. Reboot only if you want to test the boot configuration during a maintenance window.\"}}, {\"@type\": \"Question\", \"name\": \"Can Ubuntu 22.04 use a swap file on Btrfs?\", \"acceptedAnswer\": {\"@type\": \"Answer\", \"text\": \"Yes, but a normal sparse or copy-on-write file can be rejected. Use `btrfs filesystem mkswapfile` as shown in Step 2, and remember that an active Btrfs swap file can affect snapshots of its containing subvolume.\"}}]}<\/script>\n\n","protected":false},"excerpt":{"rendered":"Learn how to add swap space on Ubuntu 22.04 with a protected 1 GiB file, persistent boot setup, safe tuning, verification, and fixes for common errors.","protected":false},"author":1,"featured_media":898,"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,101],"class_list":["post-897","post","type-post","status-publish","format-standard","has-post-thumbnail","category-guides","category-vps-hosting","tag-linux-vps","tag-swap","tag-ubuntu-22-04","cs-entry"],"_links":{"self":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/897","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=897"}],"version-history":[{"count":1,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/897\/revisions"}],"predecessor-version":[{"id":3686,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/posts\/897\/revisions\/3686"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media\/898"}],"wp:attachment":[{"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/media?parent=897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/categories?post=897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vpslake.com\/blog\/wp-json\/wp\/v2\/tags?post=897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}