How to Easily Create RAM Disk on Debian, Ubuntu, Linux Mint, CentOS

This tutorial will show you how to quickly create a RAM disk in any Linux distro (Debian, Ubuntu, Linux, Fedora, Arch Linux, CentOS, etc). 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 to a directory on your Linux system and use it as a disk partition.

Why use RAM disk?

RAM is ultra-fast compared to even the fastest solid state drive (SSD). As you may know, the main performance bottleneck in today’s computer is the speed of hard drive, so moving programs and files to the RAM disk yields super fast computing experience.

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. However, this can be a pro in some situations, if you use it wisely.
  • RAM is expensive so it has limited capacity. You need to make sure not allocate too much space for RAM disk, or the operating system would run out of RAM.

You can do a lot of interesting things with RAM disk.

  • RAM disk is best suited for temporary data or caching directories, such as Nginx FastCGI cache, and Debian package downloads (/var/cache/apt/archive/). 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 to temporarily store screenshots when writing articles on this blog, so when my computer shut down, those screenshots will automatically be deleted on my computer.
  • You might not believe it, but I use RAM disk to run virtual machines inside VirtualBox. My SSD is about 250G. I can’t run many VMs directly on the SSD and I’m not happy about the speed of my 2TB mechanical hard drive (HDD). I can move the VM from HDD to RAM disk before starting the VM, so the VM can run much faster.  After shutting down the VM, I move the VM files back to HDD, which takes less than 1 minute. This of course requires your computer to have a large capacity RAM.

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 user on your Linux system use the RAM disk, then change its permission to 777.

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

easily create ram disk linux

Then all left to do is to specify the file system type, RAM disk size, 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

To allocate 10G for the RAM disk, run this instead.

sudo mount -t tmpfs -o size=10G myramdisk /tmp/ramdisk

If we issue the following command

mount | tail -n 1

We can see it’s successfully mounted.

create linux ramdisk

Now if I copy my VirtualBox machines file (5.8G) into the RAM disk, my RAM usage suddenly goes up to 9.22G.

linux automount ramdisk

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 file system

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=100000

Which gave me 2.8GB/s write speed.

linux ramdisk speed test

To test read speed, run:

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

Which gave me 3.1 GB/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

x-gvfs-show will let you see your RAM disk in file manager. Save and close the file. Your Linux system will automatically mount the RAM disk when your computer boots up.

To mount it immediately without reboot, run the following command.

sudo mount -a

Using RAM Disk to Reduce SSD Wear Out

To increase the lifespan of your SSD, you should avoid write and delete operations on the SSD as much as possible. The Linux operating system has two activities that make up the most write and delete operations.

  • Software installation and updates
  • Logging

If you use a Debian-based Linux distribution, the temporary download directory for software packages is /var/cache/apt/archives/. Once the package is installed, APT package manager automatically deletes the .deb files in this directory. You can add the following line in /etc/fstab file to mount this directory in RAM.

package_archive   /var/cache/apt/archives   tmpfs   defaults,size=6G   0   0

Fedora stores temporary package downloads under /var/cache/dnf/ directory, so you can add the following line in /etc/fstab file to mount this directory in RAM.

package_archive   /var/cache/dnf  tmpfs    defaults,size=6G   0   0

Some folks might wonder if mounting this directory will prevent Fedora from upgrading to a new release, because Fedora requires reboot to install system upgrade packages.  The answer is No. It won’t prevent system upgrades, as Fedora stores system upgrade pacakges under a different directory /var/lib/dnf/system-upgrade/.

The logging directory on Linux is usually /var/log/. It’s not safe to mount the entire /var/log/ directory in RAM.  Change to this directory.

cd /var/log/

And check the size.

sudo du -h

You will find that the /var/log/journal/ sub-directory uses the most disk space. It’s used for logging systemd services and safe to mount in RAM, so you can add the following in in the /etc/fstab file.

systemd_journal   /var/log/journal       tmpfs      defaults,size=6G    0    0

If you run Nginx web server, you might want to mount the /var/log/nginx/ directory into RAM. On one of my servers, this directory is 2.2G in size.

nginx_logs     /var/log/nginx/      tmpfs     defaults,size=6G    0    0

After saving the /etc/fstab file, run the following command to mount all file systems.

sudo mount -a

How to Run VirtualBox VM on RAM Disk

Note that this requires a large-capacity RAM.

When you create a brand new virtual machine, you should set the machine folder to the RAM disk directory (/tmp/ramdisk/). If you can’t find the Machine folder option, then you need to install the latest version of Virtualbox on your system.

Run VirtualBox VM on RAM Disk

If you have an existing VM, then select the VM in the main VirtualBox Manager window and go to the menu bar and select Machine -> Move, or right-click the VM and select Move from the context menu. You will be prompted to choose a new folder for the virtual machine. Select /tmp/ramdisk/ as the new folder.

Virtualbox move virtual machine folder

Remember to move your VM back to the original folder before shutting down your computer, or your VM will be deleted.

Wrapping Up

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 Twitter or like our Facebook page. Thanks for visiting!

Rate this tutorial
[Total: 47 Average: 4.2]

23 Responses to “How to Easily Create RAM Disk on Debian, Ubuntu, Linux Mint, CentOS

  • Peter Chubb
    4 years ago

    What you’ve described isn’t really a ramdisk, but a tmpfs instance. tmpfs is a ram-backed pseudo-filesystem, that you can mount directly. To create a ramdisk (e.g., for testing filesystem performance without spinning rust in the way, perturbing the results), you need to use the brd.ko module; you create the ram disk at module insertion time, then format it with a file system such as ext4, and then mount it.

    • Patrick
      4 years ago

      Peter,

      Great point and exactly where I am, needing a “real” filesystem on my ram-allocated space, as it takes about 10-15 minutes to copy the VM file (around 25Gb) from the HDD to the RAM space created as a tempfs. It works, and I try to avoid reboots as much as possible. This on an HP Z620 I got used on Amazon with dual Xeon’s for a total of 16 cores and 96 GB of RAM, for $560. Couldn’t argue that one. 🙂

      I understand the gist of what you’re talking about, but don’t quite have enough Linux knowledge to translate your recommendations into specifics. If you’d kindly explain how to add this module to Linux Mint 19.3, (ubuntu-based distro) then invoke the formatting and mounting of same, that will be much appreciated. Thanks in advance.

  • Thanks for writing this up – interestingly i have a fancy SSD: Samsung (MZ-V7E500BW) 970 EVO SSD 500GB – M.2 NVMe and it has the same read speed as DDR4 RAM but still slower write which makes sense

    ram:
    $ sudo dd if=/dev/zero of=/mnt/ram/zero bs=4k count=100000
    409600000 bytes (410 MB, 391 MiB) copied, 0.152319 s, 2.7 GB/s
    $ sudo dd if=/mnt/ram/zero of=/dev/null bs=4k count=100000
    409600000 bytes (410 MB, 391 MiB) copied, 0.11662 s, 3.5 GB/s

    SSD M.2 NVMe
    $ sudo dd if=/dev/zero of=/tmp/zero bs=4k count=100000
    409600000 bytes (410 MB, 391 MiB) copied, 0.234962 s, 1.7 GB/s
    $ sudo dd if=/tmp/zero of=/dev/null bs=4k count=100000
    409600000 bytes (410 MB, 391 MiB) copied, 0.111227 s, 3.7 GB/s

    • Hi Ben — RAM should actually be faster for both reading and writing. It’s very likely that in your final dd call that reads from /tmp/zero on “NVMe”, you’re actually reading from your OS’s file cache, since you just wrote that file one command earlier (?), and your OS has incentive to cache it. This is why it appears to be as fast as RAM — it *is* RAM!

      If you’re using Linux, an experiment to confirm would be the following: write the file to NVMe, then run `echo 3 > /proc/sys/vm/drop_caches` to flush the filesystem cache, and then read the file from NVMe.

  • Is using 777 is safe when creating from Root on Debian machine? Only I have access to this machine.

    • If only yourself has physical access to the machine there is no need to make the RAM disk accessible to other user accounts. If creating your RAM disk as root is absolutely necessary, give your user account permissions for the disk and then finish your other root work and change user accounts before you use the disk.

      There are few use cases to remain as root; it isn’t safe, but if your question is limited to the group and owner attributes of the filesystem mounted on the RAM disk, you should check the documentation of the mount program. You could always change the owner and group to your user account as well after creation, then log out of root.

  • Hi! Very nice article – I notice that you can create the default ramdisk after computer reboot. But can you also make folders in it? For example I have noticed that I could write torrents to ramdisk and save SSD “wear off”. But I need additional folder to be created on ramdisk to this can be available for transmision or qbittorrent. Any idea how to do that?

    • patrick
      3 years ago

      Thanks Peter for pointing out that there is a difference on Linux in configuring a ram disk. As I know it from the good old AmigaOS days, the early models had this feature already implemented. Automatically and dynamically allocated in real RAM workspace at every boot and with its specific filesystem formated.
      But there also was a mountable, (warm) reset-proof ram disk (Rad: drive) available to be mounted if needed which could be formated with different filesystems. Anyway as both types were located in RAM there hardly was a performance difference to be noticed.

    • Xiao Guoan (Admin)
      2 years ago

      You can use systemd to create a task that automatically creates the fold in the RAM disk.

      For example, create a systemd service called create-folder.service.

      sudo nano /etc/systemd/system/create-folder.service

      Content in this file.

      [Unit]
        Description=Creates a folder in the RAM disk
      
      [Service]
        Type=oneshot
        ExecStart=/bin/mkdir /tmp/ramdisk/folder
      
      [Install]
        WantedBy=multi-user.target
      

      Save and close the file. Then enable this service.

      sudo systemctl enable --now create-folder.service
    • Try taking the ram chips out of the computer itself using a small screw driver then sprinkle some dried banana skins over them,next take some glue,anytype will do an place a drop on every chip and then before it dries place some newspaper over them (just small pieces) then place the chips int a microwave
      for 30 seconds on a high setting…dont worry about any smoke coming out of the microwaver….plae chips back into computer and there your done.

  • Thanks for this article. Personally, I have a problem that I would appreciate if you will help me solve it.

    My computer has 4G of RAM. On the other hand, I work a lot with Jupyter Lab open in the FireFox browser, opening several Notebooks at the same time. This ends up locking up the computer.

    I need to expand the RAM, which I do not want to do in this old friend, waiting for better times to acquire a computer with more memory. Creating a virtual RAM and making it work with FireFox could be a temporary solution.

    My question, after reading this article, is how many gigabytes should this virtual memory be? Is there a limit? If the partner to FireFox, this application will no longer use the 4G of the current memory of the computer, in which case, my virtual memory should be More than the current 4G ?.
    I will appreciate help. Best regards

    • Xiao Guoan (Admin)
      2 years ago

      RAM disk doesn’t apply to your situation. Instead, you can create swap space to extend the memory. The following tutorial also applies to personal computers running Linux OS.
      Create Swap File on Cloud Linux Server to Prevent Out of Memory

      As a rule of thumb, the swap space should not be larger than the physical memory.

    • Try taking the ram chips out of the computer itself using a small screw driver then sprinkle some dried banana skins over them,next take some glue,anytype will do an place a drop on every chip and then before it dries place some newspaper over them (just small pieces) then place the chips int a microwave
      for 30 seconds on a high setting…dont worry about any smoke coming out of the microwaver….plae chips back into computer and there your done.

  • Excuse me Xiao. I did not realize that I had already raised the problem and you had answered. Thanks for the help.

  • Duffman
    2 years ago

    Thank you LinuxBabe!!!

  • organicnz
    1 year ago

    Thank you mate 🙂

  • RobertOpemn
    11 months ago

    Hola, quería saber tu precio..

  • RobertOpemn
    11 months ago

    Dia duit, theastaigh uaim do phraghas a fháil.

  • RobertOpemn
    10 months ago

    Sawubona, bengifuna ukwazi intengo yakho.

  • IDMarvin
    8 months ago

    hello everyone

  • To see the difference and real speed of your devices just change file size.
    sudo dd if=/dev/zero of=/tmp/ramdisk/zero bs=40k count=100000

    This will change file size x10 , so to test it with your ram , ram folder must be more than 4GB

  • systemd-journald natively supports using temporary storage without making additional mountpoints manually.

    In the journald.conf file, if you set Storage=volatile, it’ll hand it all for you.

Leave a Comment

  • Comments with links are moderated by admin before published.
  • Your email address will not be published.
  • Use <pre> ... </pre> HTML tag to quote the output from your terminal/console.
  • Please use the community (https://community.linuxbabe.com) for questions unrelated to this article.
  • I don't have time to answer every question. Making a donation would incentivize me to spend more time answering questions.

The maximum upload file size: 2 MB. You can upload: image. Links to YouTube, Facebook, Twitter and other services inserted in the comment text will be automatically embedded. Drop file here