How to Create a RAM Disk on macOS (APFS)
A RAM disk is one of the simplest ways to unlock extreme performance on macOS. This guide explains what a RAM disk is, why you might want one, and walks through a Bash script that creates a temporary APFS RAM disk in seconds.
What Is a RAM Disk?
A RAM disk is a virtual storage volume that lives entirely in system memory (RAM) instead of on a physical SSD or hard drive. Because RAM is dramatically faster than any disk, read and write operations on a RAM disk are nearly instant.
The tradeoff is simple: data stored on a RAM disk is temporary. When you reboot or shut down your Mac, the RAM disk disappears.
Why Use a RAM Disk?
- Ultra-fast build directories for Xcode or other compilers
- Temporary cache or scratch space
- Video, image, or audio processing pipelines
- Reducing wear on SSDs during heavy write operations
- Performance testing and benchmarking
The Script
Below is a Bash script that allocates a fixed amount of RAM, formats it as an APFS volume, and mounts it like a normal disk.
#!/usr/bin/env bash
set -euo pipefail
NAME="RAMDISK"
MB=4096
SECTORS=$(( MB * 2048 ))
echo "Creating ${MB}MB RAM disk..."
DEV="$(hdiutil attach -nomount ram://${SECTORS} | awk 'END{print $1}')"
if [[ -z "$DEV" ]]; then
echo "❌ Failed to allocate RAM disk"
exit 1
fi
diskutil eraseVolume APFS "$NAME" "$DEV" >/dev/null 2>&1
echo "✅ RAM disk mounted at /Volumes/$NAME"
How the Script Works (Step by Step)
1. Strict Error Handling
set -euo pipefail ensures the script exits immediately if something
goes wrong, preventing partial or unsafe states.
2. Define Disk Size
The script defines a 4096MB (4GB) RAM disk. macOS RAM disks are created using sector counts, so the script converts megabytes into sectors.
3. Allocate RAM
hdiutil attach -nomount ram:// asks macOS to allocate memory
and expose it as a block device without mounting it yet.
4. Format as APFS
Once allocated, the script formats the device using APFS and gives it a friendly name. This makes the RAM disk behave like a modern macOS volume.
5. Mount the Disk
The disk is mounted automatically at /Volumes/RAMDISK and can be used
immediately like any other drive.
Important Notes
⚠ Data is temporary. Everything stored on a RAM disk is erased when your Mac restarts or shuts down. Never use it for important or irreplaceable data.Also remember that allocating large RAM disks reduces the memory available to macOS and running applications. Choose a size that fits your system.
When to Use This
This approach is ideal for developers, creators, and power users who want maximum performance for temporary workflows without installing extra tools or modifying system settings.
When you’re done, simply reboot — the RAM disk disappears automatically.