How to Create a Swap Partition on Ubuntu 12.04 Vps?

Create swap partition Ubuntu

What is a Swap Partition ?

A linux swap partition is like an overflow for your system's RAM. If your RAM gets full, then your swap partition will run the other applications. So does this mean this is a great alternative to having a large RAM ? No ! because a swap partition on a hard drive can never be as fast as a RAM from a hardware point of view. Even with the growing popularity of Solid State Drives (SSD), they still cannot match the hardware capabilities of a RAM.

Step 1 : Check for any existing swaps

sudo swapon -s

This should show an empty list like :

FileName                    Type             Size         Used           Priority

If you see an empty list like this, it means there is no swap currently on your system.

Step 2 : Create the Swap

Now that we have established that there is no previous Swap on the server, we will now check whether or not there is any space available for the swap on the server, by running the following command :

df -h

This command will show the memory stats for the server (in GB). Something like this :

Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        20G  9.6G  9.1G  52% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            235M  4.0K  235M   1% /dev
tmpfs            50M  356K   49M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            246M     0  246M   0% /run/shm
none            100M     0  100M   0% /run/user

After confirming that we have enough space on the server for a swap, we now create the swap using the 'dd' command :

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

This creates a swap of 1 GB on the server, in the file name 'swapfile', and should return an output like :

1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 4.35649 s, 246 MB/s

We need to now create a linux swap area

sudo mkswap /swapfile

This command should output like :

Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=3e7247a1-d330-4c34-833c-a3020d99d25c

Step 3 : Activate the Swap

To activate the swap we need to run the following command :

sudo swapon /swapfile

Now check if the swap has been succesfully created and activated

swapon -s

Should return

Filename                                Type            Size    Used    Priority
/swapfile                               file            1048572 0       -1
© 2024 WPOven Inc. All rights reserved. WPOven® and WordPress® are registered trademarks.
WPOven is the best Managed WordPress hosting for agencies and businesses that want to succeed. With Unlimited sites hosting option, its easy to use and manage, so you can focus on growing your business. 
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram