avatar

ShīnChvën ✨

Effective Accelerationism

Powered by Druid

Comprehensive Guide to Setting Up a Swap File on Ubuntu: A Step-by-Step Approach

Fri Aug 18 2023

Introduction: Exhausting memory on your Ubuntu system can result in sluggish applications or even unexpected crashes. To maintain optimal performance, particularly on systems with limited RAM, setting up a swap space proves invaluable. This tutorial will guide you through the process of crafting and mounting a swap file on your Ubuntu system using a Bash script.

Prerequisites:

  • A system running Ubuntu (verified on Ubuntu 20.04 and subsequent versions).
  • Fundamental understanding of the Linux command line.

Step 1: Establish the Bash Script:

Launch your preferred text editor and generate a new file named create_swap.sh.

Step 2: Fill the Bash Script:

Copy and integrate the following code into your create_swap.sh file:

#!/bin/bash

# Verify if the script is executed as root
if [ "$EUID" -ne 0 ]; then
  echo "This script requires root privileges. Please run as root."
  exit 1
fi

# Specify the swap file location and size (in megabytes)
SWAP_FILE="/swapfile"
SWAP_SIZE_MB=2048  # Modify this size as necessary

# Construct the swap file
fallocate -l "${SWAP_SIZE_MB}M" "$SWAP_FILE"
chmod 600 "$SWAP_FILE"
mkswap "$SWAP_FILE"

# Activate the swap file
swapon "$SWAP_FILE"

# Append an entry to /etc/fstab to mount the swap file upon boot
echo "$SWAP_FILE none swap sw 0 0" >> /etc/fstab

echo "Swap file successfully created and mounted. Use 'swapon -s' to verify the swap usage."

Step 3: Render the Script Executable:

Open a terminal and navigate to the directory where you stored create_swap.sh. Execute the following command to render the script executable:

chmod +x create_swap.sh

Step 4: Launch the Script:

Run the script using the command below:

sudo ./create_swap.sh

Step 5: Verification:

To verify the successful creation and mounting of the swap file, use the following command:

swapon -s

This command will yield information about the currently active swap devices.


Step 6: Adjusting the Swap Usage Rate (Swappiness):

The "swappiness" parameter in Linux controls the frequency at which your system swaps data out of RAM to the swap space. This value ranges between 0 and 100, with higher values indicating more frequent swapping.

Step 6.1: Check Current Swappiness Value:

You can check the current swappiness value with the following command:

cat /proc/sys/vm/swappiness

Step 6.2: Modify Swappiness Value:

If you want to change the swappiness value, you can use the sysctl command. For example, to set the swappiness value to 10, you would use:

sudo sysctl vm.swappiness=10

This change will take effect immediately, but it will not persist after a reboot.

Step 6.3: Make Swappiness Change Permanent:

To make a change permanent, you need to edit the /etc/sysctl.conf file. You can use a text editor to open this file:

sudo nano /etc/sysctl.conf

Then, add or modify the following line at the end of the file:

vm.swappiness=10

Press CTRL+X, then Y, and then ENTER to save the file and exit. The change will persist after a reboot.

Conclusion:

Bravo! You've successfully set up and mounted a swap file on your Ubuntu system using a Bash script. This extra swap space will enhance system performance, particularly when your applications demand more memory than your physical RAM can supply.

Remember, you can adjust the swap file size (SWAP_SIZE_MB) based on your system's needs. However, it's generally advised to maintain a swap space approximately 1.5 to 2 times the size of your physical RAM.

For any questions or issues, don't hesitate to reach out to the Ubuntu community forums or seek guidance from seasoned Linux users.

Disclaimer: Exercise caution when dealing with system-level operations such as creating and modifying swap files. Improper alteration of system files can result in data loss or system instability. Always ensure your important data is backed up and proceed with caution.