articles » current » information-technology » linux » setup-and-configure » swap-file-ubuntu-18-04

Swap File for Ubuntu 18.04

Setup swap file on Ubuntu Linux 18.04

Download Ubuntu 18.04 (Bionic Beaver) from here and install it.

Check If Swap File Is Already Installed

Some installations may already have a swap file setup. Issue this command in the Terminal:

sudo free -h

The output should look something like this if one is not configured:

              total        used        free      shared  buff/cache   available
Mem:           475M        207M        6.4M        996K        261M        256M
Swap:            0B          0B          0B

Create a Swap File

Create a 2 GiB swap file in the root directory with fallocate:

sudo fallocate -l 2G /swapfile

Should fallocate not be available, use dd.

sudo dd if=/dev/zero of=/swapfile bs=1k count=2048k

Allow Only ROOT User Access to the Swap File

Only the root user should be accessing the newly created swap file. Issue command:

sudo chmod 600 /swapfile

Make the New File the Swap File

Use mkswap as follows:

sudo mkswap /swapfile

Activate the Swap File

Use swapon as follows:

sudo swapon /swapfile

Edit the /etc/fstab File

Use nano as follows to modify this file to only have one line:

sudo nano /etc/fstab

This file should have a line like this:

/swapfile swap swap defaults 0 0

Verify the Installation

Use free to check the swapfile

sudo free -h

The output should look something like this:

              total        used        free      shared  buff/cache   available
Mem:           475M        207M        6.4M        996K        261M        256M
Swap:          2.0G        256K        2.0G

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE