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.
You can either have a swap file or a dedicated swap partition on the system’s disk.
If you’re a student or new user looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.
Ubuntu is an open-source Linux operating system that runs on desktops, laptops, servers, and other devices.
If you don’t already have a swap file or want to create one, follow the steps below:
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 currently have a swapfile configured.
A system with swap space enabled and configure 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.
For this tutorial, we will create a new swapfile of 2G space.
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 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 you’re done, run the commands below to make sure only the root user can read and write to the swap file.
sudo chmod 600 /swapfile
Next, use 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:
This post shows you how to create or increase a swap file on Ubuntu 18.04 | 16.04 or other Linux systems. If you find any errors above, please use the comment form to report them.
You may also like the post below:
After updating swapfile my system is in a login loop. It is booting to loogin page and not starting after successful login.
I tried login on command prompt and was able to login but UI login is failing.
Ubuntu 18.0.4.