How to Create a RAM Disk in Linux Using tmpfs for High-Speed Storage

When you need to compile code, process large temporary datasets, or run read/write-heavy scripts, traditional hard drives (even NVMe SSDs) can act as a significant bottleneck. The fastest storage medium available on your Linux machine is your system memory (RAM). By using the built-in tmpfs file system, you can easily create a RAM disk—a dedicated portion of memory that behaves exactly like a physical hard drive but operates at blistering speeds.

Why Use tmpfs for a RAM Disk?

Unlike older RAM disk implementations (such as ramfs), tmpfs is dynamic. If you create a 2GB tmpfs partition but only store 100MB of files inside it, it will only consume 100MB of actual RAM. Furthermore, because RAM is volatile memory, all data stored within a tmpfs partition is automatically destroyed the moment your computer loses power or reboots. This makes it exceptionally useful for storing sensitive temporary files or caching data that does not need to be saved permanently.

What You Need Before Starting

Before allocating a RAM disk, ensure your Linux system has enough free physical memory to accommodate it. You can check your available RAM by opening the terminal and typing free -h. Never allocate more than 50% of your total RAM to a tmpfs partition, as this could starve your operating system and cause severe instability or kernel crashes.

Creating a Temporary RAM Disk

You can mount a RAM disk instantly using standard terminal commands. This method creates a temporary disk that will vanish upon reboot.

  1. Open your terminal application.
  2. Create a directory that will serve as the mount point. For example, to create a folder named “ramdisk” inside the /mnt directory, run:

sudo mkdir -p /mnt/ramdisk

  1. Mount the tmpfs file system to this new directory. To create a 1GB RAM disk, use the following command:

sudo mount -t tmpfs -o size=1G tmpfs /mnt/ramdisk

Your RAM disk is now active. You can verify that it has been mounted correctly and check its allocated size by running the df -h command.

Configuring File Permissions

By default, the new RAM disk is owned by the root user, meaning standard users cannot write files to it. To fix this, change the ownership of the mount point to your own user account.

Replace your_username with your actual Linux username:

sudo chown your_username:your_username /mnt/ramdisk

You can now seamlessly drag and drop files into the /mnt/ramdisk directory using your graphical file manager, or interact with it via the command line.

Making the RAM Disk Persistent Across Reboots

If you want the RAM disk to be available every time you start your computer (though the contents will still be erased), you must add it to your fstab file.

  1. Open the file systems table in a text editor with root privileges:

sudo nano /etc/fstab

  1. Add the following line to the very bottom of the file to mount a 1GB tmpfs partition on boot:
tmpfs  /mnt/ramdisk  tmpfs  rw,size=1G  0  0
  1. Save the file (Ctrl + O, Enter) and exit (Ctrl + X).

To test that the configuration is correct without rebooting, run sudo mount -a. If no errors are returned, your RAM disk is successfully configured to mount automatically.

Safely Unmounting the RAM Disk

If you need to reclaim the allocated memory while the system is running, ensure you have backed up any important data from the RAM disk, as unmounting it will instantly destroy its contents.

Run the following command to unmount it safely:

sudo umount /mnt/ramdisk

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.