Skip to content

Instantly share code, notes, and snippets.

@iambryancs
Last active February 1, 2025 23:35
Show Gist options
  • Save iambryancs/42d25d761fe7e0d11c5d75d11887a365 to your computer and use it in GitHub Desktop.
Save iambryancs/42d25d761fe7e0d11c5d75d11887a365 to your computer and use it in GitHub Desktop.
Create SWAP in Ubuntu
#!/bin/bash
set -e
# $1 - arg1, swap size in G (e.g 1G)
if [ -z $1 ]; then
echo "Error! Please provide swap size in G as argument."
echo "Example: create-swap-ubuntu.sh 1G"
exit 1
else
sudo fallocate -l $1 /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
sudo swapon --show
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
# Adjust cache pressure setting
sudo sysctl vm.swappiness=10
sudo sysctl vm.vfs_cache_pressure=50
# Make changes permanent
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf
echo "vm.vfs_cache_pressure=50" | sudo tee -a /etc/sysctl.conf
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment