Creating or adding a swap partition in Ubuntu or Linux Mint can significantly improve your system’s performance, especially if you have limited RAM. Swap space acts as an overflow area where the operating system can temporarily move inactive pages from RAM to disk storage, helping prevent crashes and slowdowns when memory demands are high. Whether you want to add swap to an existing installation or create a dedicated swap partition during setup, this guide will walk you through the process step-by-step.
What You’ll Need
- A computer running Ubuntu or Linux Mint with administrative (sudo) access
- At least some free unallocated disk space or a partition that can be reformatted
- Basic familiarity with the terminal and command-line tools
- Backup of important data (recommended before modifying partitions)
Step-by-Step
- Check Current Swap Status
Open a terminal and run:swapon --show
This command shows existing swap areas. If nothing appears, no active swap is configured.
- Check Available Disk Space
Use the following command to see your disk partitions and free space:sudo fdisk -l
or
lsblk
Identify a partition to convert to swap or note free unallocated space for creating a new partition.
- Backup Your Data
Before making any partition changes, back up important files. Partitioning can risk data loss if mistakes occur. - Create a Swap Partition
Use a partitioning tool likefdiskorgparted. For command line:sudo fdisk /dev/sdX
Replace
sdXwith your drive (e.g.,sda).- Press
nto create a new partition. - Choose
pfor primary orlfor logical. - Specify the size (e.g., +4G for 4GB swap).
- Press
tto change the partition type. - Enter the partition number you created.
- Type
82to set the partition type to Linux swap. - Press
wto write changes and exit.
Alternatively, use
gpartedfor a graphical interface:- Launch
gpartedwith sudo:sudo gparted. - Right-click unallocated space, select “New”.
- Choose “linux-swap” as the file system type and set desired size.
- Apply the changes.
- Press
- Format the Swap Partition
Once created, format it as swap:sudo mkswap /dev/sdXn
Replace
sdXnwith your new swap partition identifier (e.g.,sda3). - Enable the Swap Partition Temporarily
Activate the swap immediately without rebooting:sudo swapon /dev/sdXn
Verify with:
swapon --show
- Make the Swap Permanent
Edit/etc/fstabto enable swap at boot:sudo nano /etc/fstab
Add the following line at the end:
/dev/sdXn none swap sw 0 0
Save and exit (Ctrl+O, Enter, Ctrl+X).
- Adjust Swapiness (Optional)
Swapiness controls how aggressively Linux uses swap space. To check current value:cat /proc/sys/vm/swappiness
Default is usually 60. To change temporarily:
sudo sysctl vm.swappiness=10
To make permanent, add this line to
/etc/sysctl.conf:vm.swappiness=10
- Verify Swap is Active
Run:free -h
or
swapon --show
You should see your swap partition listed with its size.
- Optional: Create a Swap File Instead
If you cannot create a dedicated partition, a swap file is an alternative:sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile
Then add to
/etc/fstab:/swapfile none swap sw 0 0
Common Mistakes
- Not Backing Up Data Before Partitioning: Partitioning can lead to data loss if done incorrectly. Always back up first.
- Using Wrong Partition Identifier: Applying commands to the wrong partition can corrupt data or system files.
- Not Updating /etc/fstab: Forgetting to add the swap partition to
/etc/fstabmeans the swap won’t activate on reboot. - Improper Permissions on Swap File: Swap files must have restricted permissions (600); otherwise, swap won’t activate.
- Choosing Inadequate Swap Size: Too small swap might not help performance; too large wastes disk space. General recommendation is 1-2 times RAM size for typical use.
Tips
- Use GParted for Ease: If you prefer graphical tools,
gpartedis user-friendly and less error-prone for partitioning. - Consider Swap File if Disk Partitioning is Difficult: Swap files are easier to create and resize without repartitioning your disk.
- Adjust Swappiness Based on Use Case: Lower swappiness values reduce swap usage, beneficial for SSD longevity and responsiveness.
- Keep Swap on Fast Storage: If possible, place swap on SSD for better performance compared to traditional HDD.
- Monitor Swap Usage: Use
htoporfree -hregularly to check if swap is being used and adjust accordingly. - Use UUIDs in /etc/fstab: Instead of device names like
/dev/sda3, use UUIDs to avoid issues if device names change:sudo blkid
to find UUIDs.
Troubleshooting
- Swap Not Activating on Boot:
- Check
/etc/fstabfor correct entry and syntax errors. - Verify partition or swap file exists and is correctly formatted.
- Try manually activating swap with
sudo swapon -aand check for error messages.
- Check
- Swap File or Partition Not Showing in swapon:
- Confirm swap formatted properly with
mkswap. - Check permissions for swap file (must be 600).
- Look at system logs (
dmesg) for errors related to swap.
- Confirm swap formatted properly with
- System Slows Down Despite Swap:
- Swap is slower than RAM; if swap usage is high, consider adding more physical RAM.
- Adjust swappiness to reduce swap usage.
- Check running processes for memory leaks or heavy usage.
- Partitioning Errors or Disk Not Recognized:
- Ensure disk is not mounted when partitioning.
- Use live USB session if root partition needs resizing or repartitioning.
- Double-check disk device names to avoid mistakes.
Frequently Asked Questions
- How much swap space do I need?
- For most users, swap equal to your RAM size up to twice the RAM is sufficient. For systems with large RAM (>16GB), smaller swap (4GB) often suffices unless hibernation is used.
- Can I use a swap file instead of a partition?
- Yes, swap files are easier to create and resize and work just as well on modern Linux systems.
- Is swap necessary if I have a lot of RAM?
- Not always, but having some swap is recommended to handle unexpected memory spikes and enable hibernation.
- Will swap wear out my SSD?
- Swap does cause additional writes to SSDs, but modern SSDs have high endurance. Adjust swappiness to minimize excessive swapping.
- How do I remove or disable swap?
- Deactivate with
sudo swapoff /dev/sdXnorsudo swapoff /swapfile, then remove or comment out the entry in/etc/fstab. - Can I resize swap after creation?
- Yes, but you need to deactivate swap, delete and recreate the partition or swap file at the new size, then reactivate it.
Adding or creating a swap partition in Ubuntu or Linux Mint is a straightforward process that can greatly improve system stability under heavy memory load. Whether you choose a dedicated partition or a swap file, following these steps carefully and keeping backups will ensure a smooth experience.
Remember that swap is a complement, not a substitute, for physical RAM. Regularly monitoring your system’s memory usage and adjusting swap settings will help keep your Linux system running efficiently.
Monitoring Swap Usage
Once your swap partition or swap file is active, it’s important to monitor its usage to understand how your system is utilizing virtual memory. This can help you make informed decisions about whether you need to adjust swap size or swappiness settings.
free -h: Displays the total, used, and free memory including swap in a human-readable format.swapon --show: Lists all active swap areas along with their size and priority.vmstat 1: Continuously reports system memory and swap statistics every second.toporhtop: Interactive tools that show real-time swap usage per process.
By keeping an eye on these metrics, you can detect if your system is swapping excessively, which might indicate insufficient RAM or a need to tweak swap settings.
Adjusting Swappiness for Optimal Performance
Swappiness is a Linux kernel parameter that controls the tendency of the system to use swap space. It can have a significant impact on performance, especially on systems with SSDs or limited RAM.
- The swappiness value ranges from 0 to 100, where lower values reduce swap usage and higher values increase it.
- Default swappiness value is usually 60.
- To check the current swappiness value, run:
cat /proc/sys/vm/swappiness - To temporarily change swappiness (until next reboot), run:
sudo sysctl vm.swappiness=VALUE - To permanently change swappiness, edit
/etc/sysctl.confand add or modify the line:vm.swappiness=VALUE
For desktop systems with plenty of RAM, a swappiness of 10 or 20 often reduces unnecessary swapping, leading to smoother performance. Servers or systems with limited RAM might benefit from higher values to avoid running out of memory.
Swap and Hibernation Compatibility
If you plan to use the hibernation feature on your system, your swap space needs to be at least as large as your RAM size. This is because the contents of the RAM are written to swap during hibernation, allowing the system to resume exactly where it left off.
- Ensure your swap partition or file is large enough to hold the entire RAM content.
- Verify that the swap device is configured in the
GRUB_CMDLINE_LINUX_DEFAULTparameter via/etc/default/grubwith the correctresume=UUID=pointing to your swap. - After editing GRUB, update it with
sudo update-grub. - Test hibernation carefully as hardware support varies, and some systems may experience issues.
Using Swap on Encrypted Systems
On encrypted disks or systems using full disk encryption (LUKS), swap space should also be encrypted to maintain security. Ubuntu and Linux Mint handle this by setting up encrypted swap automatically during installation if encryption is enabled.
- Encrypted swap uses a random key generated at boot, so the swap contents are not persistent and are protected from unauthorized access.
- To check if your swap is encrypted, use
cat /proc/swapsand look for device mapper entries like/dev/mapper/cryptswap1. - If you add a new swap partition on an encrypted system, consider setting up encrypted swap manually using
cryptsetupand configuring/etc/crypttabaccordingly.
Swap on Systems with SSD Drives
Swap usage on SSDs is generally faster than traditional HDDs, reducing the performance penalty of swapping. However, there are some considerations to keep in mind:
- SSD wear: Although modern SSDs have high endurance, excessive swapping can accelerate wear. Monitoring and adjusting swappiness helps mitigate this.
- TRIM support: Make sure TRIM is enabled on your SSD to maintain performance over time.
- Swap file vs partition: Using a swap file on SSDs is generally as effective as a swap partition and easier to manage.
- Disabling swap on SSDs is not recommended unless you have abundant RAM and no need for hibernation.
Creating Swap on LVM Volumes
If your system uses Logical Volume Manager (LVM), creating swap space can be done easily within an existing volume group without repartitioning the disk.
- To create a swap logical volume, use:
sudo lvcreate -L 4G -n swap_vol vg_name(replace4Gandvg_namewith desired size and volume group name)- Format the logical volume as swap:
sudo mkswap /dev/vg_name/swap_vol- Enable the swap:
sudo swapon /dev/vg_name/swap_vol- Add the entry to
/etc/fstabfor persistence.
This method is flexible and allows resizing swap logical volumes without dealing with physical partitions.
Swap Priority and Multiple Swap Areas
Linux supports multiple swap areas simultaneously, such as a combination of swap partitions and swap files. Managing their priority determines the order in which swap spaces are used.
- Swap priority ranges from -1 (lowest) to 32767 (highest).
- To set or change swap priority when enabling swap, use:
sudo swapon --priority PRIORITY /dev/sdXnorsudo swapon --priority PRIORITY /swapfile- Check priorities with
cat /proc/swaps. - Swap with higher priority is used first until full, then the system moves to lower priority swaps.
- Priorities are set in
/etc/fstabby adding thepri=option; for example:UUID=xxxx none swap sw,pri=10 0 0.
Related
- Current Members of Killing Joke: Who’s in the Band Today?
- Current Members of King Crimson: Who’s in the Band Today?
- Complete List of The Tubes Band Members