operating system<\/a> to temporarily store data that cannot be stored in RAM due to space.<\/p>\n\n\n\nThe problem with swap is that it is significantly slower than RAM so it is not ideal for the operating system to use it for running programs but rather it is usually used to hold older data that is accessed less frequently by other services.<\/p>\n\n\n\n
Nevertheless when your server RAM is depleted the operating system is more than happy to use swap space as a fallback.<\/p>\n\n\n\n
Warning: Using swap space on SSDs is not recommended as it can cause degradation of the disk over time. Therefore swap is only recommended on systems with traditional spinning hard disk drives.<\/em><\/p>\n\n\n\nGet Started With Adding Swap Space in Ubuntu 18.04 LTS<\/p>\n\n\n\n
Step 1: Check if Swap is Already Enabled<\/h2>\n\n\n\n The first thing you will want to do is to check if swap space has already been added to your system. You can check for swap by typing:<\/p>\n\n\n\n
$ sudo swapon --show<\/code><\/pre>\n\n\n\nIf you do not get any output it means your system does not have swap space otherwise you will get something similar to this:<\/p>\n\n\n\n
NAME TYPE SIZE USED PRIO\n\/dev\/dm-1 partition 1G \t364M -2<\/code><\/pre>\n\n\n\nYou can also use the free tool to check for active swap.<\/p>\n\n\n\n
$ free -h<\/code><\/pre>\n\n\n\nThe output will look something similar to this:<\/p>\n\n\n\n
total used free shared buff\/cache available\nMem: 981Mi 129Mi 640Mi 0.0Ki 211Mi 714Mi\nSwap: 1G 364M 0B<\/code><\/pre>\n\n\n\nStep 2 \u2013 Check for Potentially Available Space on the Hard Drive Partition<\/h2>\n\n\n\n Swap has to be created on the physical disk so it is important that you make sure the space exists in the first place. You can do so by entering:<\/p>\n\n\n\n
$ df -h<\/code><\/pre>\n\n\n\nYour output will look something similar to this.<\/p>\n\n\n\n
Filesystem Size Used Avail Use% Mounted on\nudev 474M 0 474M 0% \/dev\ntmpfs 99M 932K 98M 1% \/run\n\/dev\/vda1 40G 1.4G 38G 3% \/\ntmpfs 491M 0 491M 0% \/dev\/shm\ntmpfs 5.0M 0 5.0M 0% \/run\/lock\ntmpfs 473M 0 491M 0% \/sys\/fs\/cgroup\n\/dev\/vda15 104M 3.9M 101M 4% \/boot\/efi\n\/dev\/loop0 56M 55M 0 100% \/snap\/core18\/1705\ntmpfs 99M 0 99M 0% \/run\/user\/1000<\/code><\/pre>\n\n\n\nTake note of the device with \/<\/code> in the Mounted on<\/strong> the column. Ideally, that is where you will want to get your space from. Be diligent in determining how much space you will want to use for swap space.<\/p>\n\n\n\nA good starting point to the amount of swap you want is usually the same amount as RAM or double the amount up to 4G. If you need 4G of swap it might be a sign that you need to add physical RAM to your system instead of adding such a large amount of swap.<\/p>\n\n\n\n
Step 3: Add Swap Space<\/h2>\n\n\n\n Seeing it is possible to have multiple swap files or swap partitions, we will not be featuring such a setup. In this guide, we will stick to one swap file.<\/p>\n\n\n\n
We are going to use the fallocate <\/strong>program to create a swap file called swapfile in the root \/<\/code> directory.<\/p>\n\n\n\nIn this example, we are creating a 2G file by running this command.<\/p>\n\n\n\n
$ sudo fallocate -l 1G \/swapfile<\/code><\/pre>\n\n\n\nNext, we verify that the correct amount of space was reserved by typing:<\/p>\n\n\n\n
$ ls -lh \/swapfile<\/code><\/pre>\n\n\n\nThe output should be similar to this depending on the size you chose.<\/p>\n\n\n\n
$ -rw-r--r-- 1 root root 1.0G Jun 10 11:14 \/swapfile<\/code><\/pre>\n\n\n\nStep 4: Enable the Swap File<\/h2>\n\n\n\n Before we actually turn the file into swap space, we need to restrict permissions to the space to users with root privileges.<\/p>\n\n\n\n
$ sudo chmod 600 \/swapfile<\/code><\/pre>\n\n\n\nNext, we mark the file as swap space by running the following:<\/p>\n\n\n\n
$ sudo mkswap \/swapfile<\/code><\/pre>\n\n\n\nThe following is the output:<\/p>\n\n\n\n
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)\nno label, UUID=ce45582-0a2a-450f-cdd4-527e54026db7<\/code><\/pre>\n\n\n\nFinally, we enable the swap file so that our system can use it<\/p>\n\n\n\n
$ sudo swapon \/swapfile<\/code><\/pre>\n\n\n\nOnce again verify that the swap is available:<\/p>\n\n\n\n
$ sudo swapon --show<\/code><\/pre>\n\n\n\nHere’s the output:<\/p>\n\n\n\n
NAME TYPE SIZE USED PRIO\n\/swapfile file 1024M 0B -2<\/code><\/pre>\n\n\n\nUsing the free utility:<\/p>\n\n\n\n
$ free -h<\/code><\/pre>\n\n\n\nHere’s the confirmed output:<\/p>\n\n\n\n
total used free shared buff\/cache available\nMem: 981Mi 115Mi 652Mi 0.0Ki 213Mi 714Mi\nSwap: 1.0Gi 0B 1.0Gi<\/code><\/pre>\n\n\n\nStep 5: Make the Swap File Permanent<\/h2>\n\n\n\n It is important to note that the swap we created is only valid for the current session. Therefore if we reboot, the server will lose the swap settings. To resolve this we need to add the swap file to our \/etc\/fstab<\/code> file.<\/p>\n\n\n\nFirst thing, as good practice, back up the \/etc\/fstab<\/code> file.<\/p>\n\n\n\n$ sudo cp \/etc\/fstab \/etc\/fstab.bak<\/code><\/pre>\n\n\n\nAdd the swap information to the tail end of your \/etc\/fstab using this command:<\/p>\n\n\n\n
$ echo '\/swapfile none swap sw 0 0' | sudo tee -a \/etc\/fstab<\/code><\/pre>\n\n\n\nStep 6: Optimize your Swap Settings<\/h2>\n\n\n\n You will need to change the swappiness parameter. This parameter determines how often your system swaps data out of RAM to the swap space. It should be set as a percentage anywhere between 0 and 100. The default is 60.<\/p>\n\n\n\n
Values closer to zero, the system will not swap data to the disk unless absolutely necessary. Values that are closer to 100 put as much data as possible into swap in an effort to keep more RAM space free. You can test your swappiness by running the following:<\/p>\n\n\n\n
$ cat \/proc\/sys\/vm\/swappiness<\/code><\/pre>\n\n\n\nBy default it will output:<\/p>\n\n\n\n
60<\/code><\/pre>\n\n\n\nThe general rule is for a Desktop, a swappiness setting of 60 is a good value. For a server environment values closer to zero are ideal.<\/p>\n\n\n\n
You can change swappiness by running the following:<\/p>\n\n\n\n
$ sudo sysctl vm.swappiness=10<\/code><\/pre>\n\n\n\n$ sudo sysctl vm.swappiness=10<\/code><\/p>\n\n\n\nWith the successful output being:<\/p>\n\n\n\n
vm.swappiness = 10<\/code><\/pre>\n\n\n\nThis value is only valid for the current session until the server is rebooted. To make it more permanent you need to edit the \/etc\/sysctl.conf file by running:<\/p>\n\n\n\n
$ sudo nano \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\nThen add this line at the end of the file:<\/p>\n\n\n\n
vm.swappiness=10<\/code><\/pre>\n\n\n\nSave and close the file to make the changes permanent. Now, that is how to add swap in Ubuntu 18.04 LTS.<\/p>\n","protected":false},"excerpt":{"rendered":"
Here you will learn how to add swap space in Ubuntu 18.04 LTS. You may want to do this because you are getting more than your fair share of out-of-memory…<\/p>\n","protected":false},"author":1,"featured_media":8672,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,16],"tags":[193,354,431,433,449,531,573,591,598],"yoast_head":"\n
How to Add Swap Space in Ubuntu 18.04 LTS Server<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n