How to Add Swap Space on Your Ubuntu 16.04 Server

How to Add Swap Space on Your Ubuntu 16.04 Server

Step 1:
Checking for swap information in the system

First of all you need to see if there is some swap space available in your operating system. However, you may be having more than on swap partitions or files but only one is sufficient.

Type the following to check whether the system has any configured swap:

sudo swapon -s
Filename                Type Size Used    Priority

You don’t have any swap space enable if you get only the header of the hable as show above.

Alternatively, you can check for swap space using the free utility. Now, type the following the check your current memory and swap usage:

free -m
            total used       free shared buffers     

Mem:          3950 328       3799 102 3311         

Swap:            0 0     0

Step 2:
Checking space available on hard drive

We recommend you to use a completely separate partition for swap but sometimes you cannot alter the partition scheme.

But one thing that you need to know before starting is your current disk usage. Type this to know your current disk usage:

df -h
Filesystem     Size Used Avail   Use% Mounted on

udev           2.0G 0 2.0G    0% /dev

tmpfs          396M 11M 385M    3% /run

/dev/xvda1      25G 3.8G 21G   16% /

tmpfs          2.0G 0 2.0G    0% /dev/shm

tmpfs          5.0M 0 5.0M    0% /run/lock

tmpfs          2.0G 0 2.0G    0% /sys/fs/cgroup

tmpfs          396M 0 396M    0% /run/user/1000

You will see something similar to the information shown above. In this case, you may see that the third line shows the space available in our hard drive partition which is 21 GB.

The proper size of a swap space depends on the personal preference of the users and their application requirements. As a starting point, you can go with space that is double the amount of RAM on the system.

Step 3:
Creating swap file

I am going to create a swap space of 4 GB which is the same amount of space as my RAM. I am not making the space double because of my personal preferences.
First of all create a file in your root (/) directory and name it ‘swapfile’.
We have two methods for doing this.

The slow method

This method is rather a slow method. You will have to create a file using the dd command which has preallocated space.

This can be used to write zeros to file from a special device in Linux systems located a /dev/zero which can just split out as many zeros as requested.

To specify a file size a combination of bs for block size and count for the number of blocks is to be used. We can arbitrarily assign anything to each parameter but something that is significant is their product.

Since we are creating a file of space 4 GB. So, we can create it by specifying block size of 1 GB and a count of 4.

sudo dd if=/dev/zero of=/swapfile bs=1G count=4
4+0 records in

4+0 records out

4294967296 bytes (4.3 GB) copied, 18.6227 s, 231 MB/s

You have to be very precise about the location of in your command as it can even destroy your data if it is at the wrong place.

To check if 4 GB have been allocated or not, you can simply type the following:

ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Apr 18 16:25 /swapfile

This method is a bit time consuming.

But if you want to see how the faster method works you need to remove the file by typing:

sudo rm /swapfile

The faster method

We also have a faster method which uses the fallocate program to create the same file. This command will give you the same output without having to write dummy contents.

Type the following to create a 4 GB file:

sudo fallocate -l 4G /swapfile

Soon after the prompt returns you can check if the write amount of space has been reserved by typing:

ls -lh /swapfile
-rw-r--r-- 1 root root 4.0G Apr 18 16:29 /swapfile

Step 4:
Enabling swap file

We have created the required file but our system doesn’t know what it is meant for. In this step we will have to tell our system to format this file as swap. Then this needs to be enabled.

To avoid the security risk of other users being able to read or write to this file some adjustments have to be made on the permissions of our file. The adjustments need to be in such a way that no one besides root is able to read this file. Type the following to lock down the permissions:

sudo chmod 600 /swapfile

Type the following to verify if the file has correct permissions now:

ls -lh /swapfile
-rw------- 1 root root 4.0G Apr 18 16:30 /swapfile

If you observe on the root columns have the read and write flags enabled. Now you need to type the following to let your system create the swap space:

sudo mkswap /swapfile
Setting up swapspace version 1, size = 4194300 KiB

no label, UUID=ehfsd0-c090-4fd4-c9nb-265b9a7d9526

Now you need to enable the swap file by typing:

sudo swapon /swapfile

Type the following to check if our system reports swap space now:

sudo swapon -s
Filename                Type Size Used    Priority

/swapfile               file 4194300 0    -1

Now we have to use the free utility again to collaborate our findings:

free -m
total       used free     shared buffers  cached

Mem:        3950 328      3799 102 3311

Swap:       4095 0       4095

Step 5:
Making swap file permanent

We have already enabled our swap file but do not depend on the server to enable the file when we reboot. Modify thefstab file to change that.

In the text editor, edit the file with root privileges:

sudo nano /etc/fstab

Add the following line too at the bottom of the file:

/swapfile   none swap    sw 0 0

This will let the operating system know that the file that you created has to be used automatically. Then close the file when you have finished working.

Step 6:
Adjusting swap settings

When dealing with swap we have a few parameters that can be configured to effect the performance of your system.

There is a feature called swappiness. This parameter can configure how many times does your system swaps data out of the RAM to the swap space. This value can range between 0 to 100.

If the value of this parameter is near to zero then no data, unless extremely necessary, will be swapped by the kernel. Keep in mind that swap files can cause a reduction in the performance of the system.

If the value of this parameter is neat to 100 then to keep more amount of RAM space free more data is swapped.

To check your current swappiness type the following:

cat /proc/sys/vm/swappiness

We recommend that a desktop should have swappiness around 60 and a VPS should have it closer to zero.

The sysctl command can be used to change the value of swappiness.
For example, if you wish to set the value of swappiness to 15 then type the following:

sudo sysctl vm.swappiness=15
vm.swappiness = 15

Until you reboot, this setting will persist. Add the following line to your /etc/sysctl.conf file to set this value automatically at restart:

sudo nano /etc/sysctl.conf

Also add at the bottom:


Then save and close the file when you have finished working.

You will also need to configure the vfs_cache_pressure. This setting will help you change the amount of cache inode and dentry information above other data.

Query the proc filesystem again to see the current value:

cat /proc/sys/vm/vfs_cache_pressure

Our system removes inode information from cache quickly as it is currently configured.

To set this to a more conservative setting, say 50, type the following:

sudo sysctl vm.vfs_cache_pressure=50
vm.vfs_cache_pressure = 50

But all this is only valid for our current session. But don’t worry. It can be changed by dding it to our configuration file:

sudo nano /etc/sysctl.conf

At the bottom add the following line. It will specify a new value.

vm.vfs_cache_pressure = 50

That’s all. Now you need to save and close you file and you are done.

We hope our article helped you.

Special note: HostAdvice’s hosting reviews allow you to consult with thousands of users before purchasing a hosting plan. If you are looking to purchase a Ubuntu VPS plan, use our reviews to find the best VPS hosting services and Linux Hosting services.

Check out the top 3 Linux hosting services

AU$2.65 /mo
Starting price
Visit FastComet
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
AU$5.92 /mo
Starting price
Visit Kamatera
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing
AU$3.68 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
  • Support
  • Features
  • Reliability
  • Pricing

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How To Change the Hostname on an Ubuntu 18.04 VPS or Dedicated Server

Ubuntu 18.04 has been described as awesome by Mark Shuttleworth
3 min read
Vladimir Rakov
Vladimir Rakov
Hosting Expert

How to Install the MEAN Stack on Your Ubuntu 17.10 Server

The article is step-to-step instruction that shows how to install the MEAN (Mong
3 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Configure Nginx and Apache on the same Ubuntu VPS or Dedicated Server

Nginx and Apache are great and powerful web servers. However, they both have dra
2 min read
Idan Cohen
Idan Cohen
Marketing Expert provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top