How to Create a RAM Disk in Ubuntu Linux (15.04, 15.10…)

RAM disk is a virtual disk drive that’s mounted in your RAM. And because it’s located in RAM (Random Access Memory), the main benefit is its staggering speed. In situations like video rendering or editing and gaming etc, by having your files put inside a RAM disk can greatly speed things up, provided that you have a large amount of RAM.

RAM disks can also be used not only to deliver better performance but to offload (read/write requests) the main storage device, thus prolonging its life as well (specially in a server environment). But for the average computer user having a RAM disk might not make a huge difference, still, if you frequently do things like compress large number of files etc, then you too can benefit by having a ram disk because if the files are in RAM, they can processed quite fast.

Anyhow, for whatever reasons, if you’re in need of a ram disk managing software for GNU/Linux, then this guide is intended for you. For setting things up, you’ll have to use the command-line a bit, but I assure you that this is extremely easy, if you follow my instructions.

RAM disk opened in Ubuntu 15.10

You don’t have to be a ‘linux expert’ to do any of this. If you know how to run a command with administrative privileges and know how to copy and paste commands from a webpage to your terminal window, then you already have all the qualifications!. If all you need a RAM disk, then you don’t even have to follow all the steps either.

The ‘Basic Setup‘ should work any on GNU/Linux distribution, however, the steps laid out in the ‘Enhancing the Functionality‘ will only work on Ubuntu 15.04 & later versions (I’ve tested this  on Ubuntu 15.10), though, with minor changes (or maybe without any changes at all) they should work on other newer distributions, as long as they use systemd (a core part of ‘Linux’ that manages applications that run in the background, aka ‘daemons’).

Basic Setup…

Step 1: Let’s a create folder for the RAM drive. I’ve chosen to create it in the ‘/mnt’ directory, but you can create it anywhere you like (if you do that however, then make sure to change the folder path accordingly). I’ll call this folder ‘RAM_disk’ which will be mounted into the RAM. To create the folder, open up the terminal and enter the below command (When asked, enter the root or the administrator’s password):

sudo mkdir /mnt/RAM_disk

Step 2: Now let’s open the ‘/etc/fstab’ configuration file (this file contains all the file systems that should be mounted upon the operating system’s boot-up) using the default text editor of Ubuntu (it’s called Gedit), but first, let’s make a backup using the below command:

sudo cp /etc/fstab /etc/fstab.bak

Now use the below command to open the original file:

sudo gedit /etc/fstab

Step 3: Now let’s add an entry to the end of this configuration file so that the ‘/mnt/RAM_disk’ folder is mounted every time the operating system boots. Copy the below code and paste it in a new line, at the end of the ‘/etc/fstab’ configuration file (shown below):

tmpfs     /mnt/RAM_disk     tmpfs     rw,size=1G,x-gvfs-show     0 0

Adding RAM_disk entry to fstab (Ubuntu 15.10)

Using this code, we can easily define the desired size of the RAM disk, and as you might have guessed, I’ve highlighted that section of the code in Red (size=1G -- Note that there’s no space between the number ‘1‘ and the letter ‘G‘). Here I’ve set the size to 1 GB. If you want to set it to 2 GB, then change ‘1’ to ‘2’, as simple as that. If you want to define it in Megabytes, say you want it to be 500 Megabytes, then the code should be: size=500M , where the ‘G‘ is replaced by ‘M‘ (and the number too). Again, pretty intuitive right?

P.S: After making your changes, make sure to save the changes, before closing the text editor.

Step 4: Now enter the below command to mount the newly created ram disk:

sudo mount -a

As soon as you run this command, if it doesn’t popup automatically, open your file manager and you should see on its side panel, ‘RAM_disk’ has an entry which you can use to open it just like any other mounted media. Well’ that’s it!.

Enhancing the functionality…

Enhancing the RAM_disk (Ubuntu 15.10)

Now, one of the main disadvantages of a RAM disk is that it can only hold data temporarily, in the sense that, if a power failure occurs or if you shutdown or reboot the computer, the data of the RAM disk gets lost, permanently.

Luckily, this can easily be overcome (to a certain degree) by making the operating system save the data on the RAM disk to your actual hard disk, before shutting down or rebooting, and also making it restore the data when booting-up (this step though is to make the user’s life easy). Unfortunately, this will slow down the boot-up speed of your operating system (depending on the size of the data that needs to be loaded to the RAM drive), though not necessarily the shutdown speed.

That’s because the utility that copies the data (the one I’ve used here) is a quite smart one as once it’s done loading the data to the RAM drive (even if it’s as big as couple of Gigabytes) during system boot-up, when shutting down it only saves files & folders that have been changed. In other words, even if you had a RAM disk filled with big files, if no changes had occurred to the files, then the utility (unlike the conventional file copy tools) will not save any data to the hard disk!. But if lots of files have been altered (say a few hundreds of Megabytes of new data has been written into them), then obviously it will affect the shutdown speed negatively.

Anyhow, if you don’t have a UPS or a battery, even that method of only saving the data on the RAM drive to the hard disk when shutting down won’t provide much protection against a sudden power failure, if the data in the RAM disk gets changed quite often. In such a situation, as a countermeasure, we can make the OS save the RAM disk’s data to the hard disk, once every minute.

This too however, as explained above, can be costly because writing a big chuck of data (again, assuming such changes have occurred) to your hard disk once every minute is going to negatively affect the responsiveness of the operating system.

I honestly don’t think most users would ever need to go to this extreme, thus if all you want is to load the data to the RAM drive when booting and then save it back when shutting down, then you can follow from ‘Step 1‘ to ‘Step 5‘ below, and skip the rest.

So if you’re ready, let’s begin.

Step 1: Let’s first create a folder where the RAM disk’s data gets saved to. I’ll create it in the same ‘/mnt/’ directory as earlier, but again, you can create this anywhere you like:

sudo mkdir /mnt/RAM_disk_backup

Step 2: Now just like we previously did, let’s give the your preferred user the ability to read, write and execute the content of this folder. Enter the below command for that (remember to replace ‘gayan‘ with the proper username):

sudo setfacl -m user:gayan:rwx /mnt/RAM_disk_backup

Step 3: Now let’s create a configuration file containing simple instructions telling the operating system to save data from the RAM disk when turning off (and rebooting) and also to restore it back when booting-up. We’ll call the file ‘ram_disk_load_save.service’ and we’ll create it in ‘/lib/systemd/system/’ (mandatory location) folder.

To make it happen, open a terminal window and enter the below command (again, when asked, enter the administrator’s or the ‘root’ password):

sudo gedit /lib/systemd/system/ram_disk_load_save.service

This should open up the text editor.

Step 4: Now copy the whole code below, and paste it into the text editor window (make sure to replace ‘gayan‘ with the correct username):

[Unit]
Description=A script that save and restore the data on the RAM disk
Before=umount.target

[Service]
Type=oneshot
User=gayan
ExecStart=/usr/bin/rsync -ar /mnt/RAM_disk_backup/ /mnt/RAM_disk/
ExecStop=/usr/bin/rsync -ar /mnt/RAM_disk/ /mnt/RAM_disk_backup/
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Then click on the ‘Save’ button to save the changes and exit the text editor.

Step 5: Now let’s make the OS load this configuration file every time it boots-up so these instructions get executed. For that, enter the below command:

sudo systemctl enable ram_disk_load_save.service

Now simply reboot the computer for the change to take effect. And as mentioned above, you can stop here if you like, though, if required, we can further enhance the integrity of your data on the RAM disk by making the OS save those data every minute, and please keep in mind the drawback I mentioned.

Step 6: For this task we’re going to use a built-in task scheduler utility of GNU/Linux called ‘cron’. ‘cron’ includes a sub program called ‘crontab’ that enables users to easily add (& edit) new entries to its scheduled tasks list. So if ready, open a new terminal window and enter the below command to add a new entry for the RAM disk:

crontab -e

Now you’ll be asked (as shown below) which editor you prefer to use, simply hit the Enter key.

Selecting the editor to add an entry to cron (Ubuntu 15.10)

Step 7: Then scroll-down (use the Down-Arrow on your keyboard) and on a new line, copy and paste the below code:

* * * * * rsync -ar /mnt/RAM_disk/ /mnt/RAM_disk_backup/

crontab opened in nano (Ubuntu 15.10)

Once done, press Ctrl + O keys and when asked hit Enter, and then again press Ctrl + X keys to exit.

Well, now you’re done!, good luck.

12 thoughts on “How to Create a RAM Disk in Ubuntu Linux (15.04, 15.10…)

  1. got a problem, got this far, ramdisk is working w/16G
    and then an error:
    Step 1: sudo mkdir /media/joe/ramdisk
    Step 2: sudo setfacl -m user:joe:rwx /media/joe/ramdisk
    Step 3: sudo cp /etc/fstab /etc/fstab.bak
    Now use the below command to open the origihttp://www.hecticgeek.com/2015/12/create-ram-disk-ubuntu-linux/nal file:
    sudo gedit /etc/fstab
    Step 4: Now let’s add an entry to fstab bottom line
    tmpfs /media/joe/ramdisk tmpfs rw,size=16G,x-gvfs-show 0 0
    Step 5: Now enter the below command to mount the newly created ram disk:
    sudo mount -a

    Step 1: Let’s first create a folder where the RAM disk’s data gets saved to.
    sudo mkdir /media/joe/ramdisk_backup
    Step 2: Now just like we previously did, let’s give the your preferred user the ability to read, write and execute
    sudo setfacl -m user:joe:rwx /media/joe/ramdisk_backup
    Step 3: Now let’s create a configuration file containing simple instructions telling the operating system to save data from the RAM disk when turning off (and rebooting) and also to restore it back when booting-up.
    sudo gedit /lib/systemd/system/ramdisk_load_save.service
    Step 4: Now copy the whole code below, and paste it into the text editor window:

    [Unit] Description=A script that save and restore the data on the RAM disk
    Before=umount.target

    [Service] Type=oneshot
    User=joe
    ExecStart=/usr/bin/rsync -ar /media/joe/ramdisk_backup/ /media/joe/ramdisk/
    ExecStop=/usr/bin/rsync -ar /media/joe/ramdisk/ /media/joe/ramdisk_backup/
    RemainAfterExit=yes

    [Install] WantedBy=multi-user.target

    Step 5: Now let’s make the OS load this configuration file every time it boots-up so these instructions get executed. For that, enter the below command:
    sudo systemctl enable ramdisk_load_save.service
    Failed to execute operation: Bad message
    /etc/systemd/system/multi-user.target.wants/ramdisk_load_save.service
    is not being created at this point.

    any help would be apriciated
    PopSicle

  2. Hello, thank you for this article.

    But I have similar problem likes PopSicle said.
    At first, we should insert LF(Ofcourse, just hit Enter key), every close bracket, such as [Unit], [Service] and [Install] written in /lib/systemd/system/ramdisk_load_save.service .

    But I got another problem as below. Please help!! (2月 means Febrary in Japanese, and another 月 means Monday):

    suzumizaki@xxxxx:~$ sudo systemctl status ram_disk_load_save.service -l
    ● ram_disk_load_save.service – A script that save and restore the data on the RAM disk
    Loaded: loaded (/lib/systemd/system/ram_disk_load_save.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since 月 2016-02-15 16:59:46 JST; 5s ago
    Process: 2464 ExecStart=/usr/bin/rsync -ar /mnt/RAM_disk_backup/ /mnt/RAM_disk/ (code=exited, status=23)
    Main PID: 2464 (code=exited, status=23)

    2月 15 16:59:46 xxxxx systemd[1]: Starting A script that save and restore the data on the RAM disk…
    2月 15 16:59:46 xxxxx rsync[2464]: rsync: failed to set times on “/mnt/RAM_disk/.”: Operation not permitted (1)
    2月 15 16:59:46 xxxxx systemd[1]: ram_disk_load_save.service: Main process exited, code=exited, status=23/n/a
    2月 15 16:59:46 xxxxx systemd[1]: Failed to start A script that save and restore the data on the RAM disk.
    2月 15 16:59:46 xxxxx systemd[1]: ram_disk_load_save.service: Unit entered failed state.
    2月 15 16:59:46 xxxxx systemd[1]: ram_disk_load_save.service: Failed with result ‘exit-code’.

    • Hi Suzumizaki,

      I just went through all these Steps on a freshly installed Ubuntu 15.10 and when enabling & running the service, I too got two errors. But then I rebooted the computer and the next time I logged into the desktop, the RAM Disk was automatically mounted. So the solution is to simply reboot your computer (assuming you’re too running Ubuntu). I’ve updated the article as well.

      Let me know if this doesn’t work for you.

      P.S: You don’t actually have to change anything when copying the code. Just paste it as it is. Although, it might help, if you have disabled ‘text wrapping’ of your text editor.

      • Hi Gayan,

        Hmmm, RAM Disk itself looks working fine, but how about saving and restoring RAM Disk? My machine looks saving/restoring nothing.

        I wonder why does rsync say ‘failed to set times on “/mnt/RAM_disk/.”: Operation not permitted (1)’, while RAM Disk works fine.

        • Hi Suzuizaki,

          The post actually had few typos. I thoroughly check all my articles before posting, but this one somehow had few issues. I also fixed some issues with the systemd Unit file, because the way WordPress output it, it was breaking the script. So now it’s fixed. However, what you mentioned is still there. In other words, whenever running, rsync will throw an error. It’s a permission related issue, and I haven’t been able to find a solution yet.

          As a fix, you should edit the Unit file and remove the ‘a’ option from rsync so it’ll look like /usr/bin/rsync -r /mnt/RAM_disk_backup/ /mnt/RAM_disk/ (do this to the other command as well), but the adverse effect is that, without the ‘a’ option, rsync will be copying all the files, not just the ones that are changed, from the Ram Disk to the hard drive when shutting down. If your Ram Disk is large, this will slow the shutdown times. Until I find what’s causing it (I’m a bit busy these days, which is also why I haven’t added reviews for instance, on the blog), that’s my temporary fix.

          But I do remember of having a Ram Disk with rsync copying only the changed files on Ubuntu when writing this article originally. I’m sure it’s technical mix up, but until I find a solution, this is all I have for you (I might also take the post off line until I find a fix). Thank you again. Let me know what happened.

  3. Nice article….Your explanation of every command is awesome..Helped me to learn great things about Linux …Thank u …Cheers !!

Leave a Comment