Skip to main content

How to Easily Create RAM Disk in Linux

create ramdisk linux

This tutorial will show you how to quickly create a RAM disk in any Linux distro. Compared to commercial Windows RAM disk software that costs money, Linux can utilize this cool feature 100% free of charge.

What is RAM Disk?

RAM disk is also known as RAM drive. It’s a portion of your RAM that are formated with a file system. You can mount it on a directory and use it as a disk partition.

Why use RAM disk?

RAM is ultra-fast compared to even the fastest solid state drive. The main bottleneck in today’s computer is the speed of hard drive.

Pros of RAM disk:

  • Ultra-fast
  • Can sustain countless reads and writes

Cons of RAM disk:

  • RAM is volatile which means all data in RAM disk will be lost when the computer shutdowns or reboots. This can be a pro in some situations, though.
  • RAM is expensive so it has limited capacity. You need to make sure not allocating too much RAM for RAM disk.

RAM disk is best suited for temporary data or caching directories, such as Nginx FastCGI cache. If you use a SSD and there will be a lot of writes to a particular directory, you can mount that directory as a RAM disk to reduce wear out of SSD. I also use RAM disk for a directory which contains my site screenshots on my local Linux computer so next time it boots up, those screenshots will automatically be cleared. You can do a lot of interesting things with RAM disk.

How to Create a RAM Disk in Any Linux Distro

First make a directory which can be anywhere in the file system such as

sudo mkdir /tmp/ramdisk

If you want to let every one to use the RAM disk, then change its permission.

sudo chmod 777 /tmp/ramdisk

Next, check how much free RAM are left on your system with htop command line utility because we don’t want to use too much RAM.

htop check memory usage on linux

Then all left to do is to specify the RAM disk size,  file system, device name and mount it to the above directory. You can see from the screenshot above that I have plenty of free RAM, so I can easily allocate 1GB for my RAM disk. This can be done with the following one-liner. It will be using tmpfs file system and its size is set to 1024MB. myramdisk is the device name I gave to it.

sudo mount -t tmpfs -o size=1024m myramdisk /tmp/ramdisk

If we issue the following command

mount | tail -n 1

We can see it’s successfully mounted.

linux ramdisk

Now if I copy a 530.7MB video file into the RAM disk, my RAM usage suddenly goes up to 3.41G.

linux ramdisk tmpfs

If I unmount RAM disk

sudo umount /tmp/ramdisk/

Everything in that directory will be lost and RAM usage goes down to original.

Linux ramdisk performance

This is how you can test if your RAM disk is working.

Test RAM Disk Speed

To test write speed of RAM disk, you can use dd utility.

sudo dd if=/dev/zero of=/tmp/ramdisk/zero bs=4k count=10000

Which gave me 1.3GB/s write speed. To test read speed, run:

sudo dd if=/tmp/ramdisk/zero of=/dev/null bs=4k count=10000

Which gave me 2.5GB/s read speed.

I also did a speed test on my SSD. The write speed is 534MB/s and read speed 1.6GB/s.

Auto-mount on System Boot

Edit /etc/fstab file.

sudo nano /etc/fstab

Add an entry like this:

myramdisk  /tmp/ramdisk  tmpfs  defaults,size=1G,x-gvfs-show  0  0

Save and close the file. x-gvfs-show will let you see your RAM disk in file manager.

And that’s the basics of creating RAM disk in Linux. If you found this post useful, then subscribe to our free newsletter or follow us on Google+, Twitter or like our Facebook page. Thanks for visiting!

Rate this tutorial
[Total: 0 Average: 0]