This brief tutorial shows students and new users how to add swap space on Ubuntu 18.04 | 16.04.
Swap space is a location on the system’s disk drive when the system’s physical memory (RAM) is full. When this happens, inactive pages in this system’s RAM are moved to swap space to make room for active pages.
Adding or increasing swap space on Ubuntu Linux is important because it can help improve system performance and prevent crashes caused by running out of physical memory (RAM). When your system’s RAM is full, inactive pages in RAM are moved to the swap space to make room for active pages.
Without enough swap space, your system may become unresponsive or even crash. By learning how to add or increase swap space on Ubuntu Linux, you can ensure that your system has enough to handle demanding tasks and avoid potential issues.
View Current Swapfile
First, use the commands below to show your system’s current file swapfile settings.
sudo swapon --show
If you run the command above and the output empties, your system doesn’t have a swapfile configured.
A system with swap space enabled and configured should display similar lines as below:
Output: NAME TYPE SIZE USED PRIO /swapfile file 1.4G 0B -2
If your system has a swapfile but wants to increase it, you can use the same steps below.
First, turn off the current swapfile by running the commands below:
sudo swapoff -v /swapfile
After turning it off, remove its entry from the /etc/fstab file.
Run the commands below to open the /etc/fstab file.
sudo nano /etc/fstab
Then comment out the highlighted line or remove it entirely.
# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
#
# / was on /dev/sda1 during installation
UUID=b08fba81-8766-4c77-a965-05dd2224c125 / ext4 errors=remount-ro 0 1
#/swapfile none swap sw 0 0
After that, save the file and exit.
When you’re done, run the commands below to delete the current swap file from your system.
sudo rm /swapfile
Create New Swapfile
Now that the system’s current swapfile is removed, run the commands below to create a new one.
We will create a new swapfile of 2G space for this tutorial.
To do that, run the commands below:
sudo fallocate -l 2G /swapfile
Next, run the commands below to create a new swapfile.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152
You should see a similar output as below when successful:
Output: 2097152+0 records in 2097152+0 records out 2147483648 bytes (2.1 GB, 2.0 GiB) copied, 7.10013 s, 302 MB/s
When done, run the commands below to ensure only the root user can read and write to the swap file.
sudo chmod 600 /swapfile
Next, use the Linux mkswap command utility to make that location on the disk a swapfile.
sudo mkswap /swapfile
You should then see a similar output as below:
Output: Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=cc5d179d-4c4c-497a-82f2-c1dc8eb68e26
Finally, activate the swapfile by running the commands below:
sudo swapon /swapfile
Next, go back to the /etc/fstab file and un-comment the line to enable the new swapfile or add a new line:
sudo nano /etc/fstab
Next, verify that the new swapfile is active by running the commands below:
sudo swapon --show
If everything is successful, you should see a new swapfile as below:
Output: NAME TYPE SIZE USED PRIO /swapfile file 2G 0B -2
That’s how one creates a new or increases a swapfile on Ubuntu.
Conclusion:
In conclusion, adding or increasing swap space on Ubuntu is a straightforward process that enhances system performance and stability. Here are the key takeaways:
- Swap space is an overflow for RAM, preventing system crashes when physical memory is full.
- Regularly checking your swap file configuration can help maintain optimal system performance.
- Increasing swap space can be done safely by turning off the existing swap file, removing it, and creating a larger one.
- Proper permissions and file system configurations are essential for the swap file to function effectively.
- Verifying the new swap file setup ensures your system can handle demanding tasks without issues.
Leave a Reply to Dheeraj Cancel reply