Skip to content
Go back

Compiling on Linux With Low RAM

By SumGuy 4 min read
Compiling on Linux With Low RAM

You clone the repo. You type make. The lights on your Raspberry Pi blink frantically for three seconds. Then—silence. The OOM killer just executed your build process with zero mercy.

Welcome to the joy of compiling on low-RAM hardware. That $4/month VPS you’re proud of, the Pi in your garage, the ancient laptop gathering dust—they can compile things. They just need a little coaxing. Here’s how to stop losing the RAM wars.

Check What You’re Actually Working With

First, see where you stand:

Terminal window
free -h
Terminal window
swapon --show

Real talk: if you’ve got 512MB physical RAM and zero swap, you’re essentially using a toaster. A very frustrating toaster. Your kernel will start killing random processes before you even finish configuring ./configure.

Add More Swap (The Actual Solution)

Swap is your safety net. Yeah, it’s slower than RAM—disk I/O versus memory access—but slow and working beats fast and dead. For one-time compiles, disk swap is perfectly fine.

Create a 2GB swapfile:

Terminal window
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Verify it’s active:

Terminal window
free -h

To make it permanent (survives reboot), add this line to /etc/fstab:

Terminal window
/swapfile none swap sw 0 0

Done. You’ve just given yourself 2 extra gigs of breathing room. Is it slow? Yes. Is it better than watching your build die at 47%? Also yes.

Tune vm.swappiness (Avoid the Swap Thrashing)

By default, Linux aggressively swaps—it’ll use swap even when RAM is available, which turns your compile into a grinding, disk-thrashing nightmare.

Lower vm.swappiness:

Terminal window
sudo sysctl vm.swappiness=10

This tells the kernel: “Use swap only when you absolutely have to.” Your 2 AM self building Rust on a Pi will appreciate the responsiveness.

Make it permanent:

Terminal window
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

Limit Parallel Build Jobs

Here’s the thing: each compiler process consumes RAM. Spawn 8 in parallel, and you’ve just multiplied your memory pressure. Don’t do that.

For make:

Terminal window
make -j2

Or even -j1 if you’re really tight. Yes, it’ll be slower. But it’ll finish.

For Cargo (Rust):

Terminal window
CARGO_BUILD_JOBS=1 cargo build --release

For other build systems, check their docs—most have a jobs flag. The overhead is negligible compared to actually completing the build instead of getting OOM-killed at the finish line.

Use ccache (Cache Those Objects)

If you’re iterating on a build—tweaking a config, fixing a compile error, rebuilding—ccache is a lifesaver. It caches compiled object files so you never recompile the same source twice.

Install it:

Terminal window
sudo apt install ccache

Tell your compiler to use it:

Terminal window
CC="ccache gcc" make -j2

Or for C++:

Terminal window
CC="ccache gcc" CXX="ccache g++" make -j2

First build is normal speed. Second build? Dramatically faster. On iterative development, this cuts your total time in half or better.

tmpfs for /tmp (Conditional)

Some build systems dump temporary objects into /tmp. If you’ve got at least 1GB of spare RAM after swap setup, you can mount /tmp as tmpfs for a speed boost:

Terminal window
sudo mount -t tmpfs -o size=1G tmpfs /tmp

Make it permanent in /etc/fstab:

Terminal window
tmpfs /tmp tmpfs defaults,size=1G 0 0

Fair warning: this burns RAM that could otherwise be used for the actual compile. Only worth it if you’re RAM-comfortable and you know your build thrashes /tmp a lot. Test it first.

Bonus: zram (Compressed Swap in RAM)

Ubuntu and Debian have zram-config, which compresses swap in RAM instead of writing to disk. It’s slower than pure RAM but faster than disk swap. One-liner:

Terminal window
sudo apt install zram-config

Restart and you’re done. Great for Pis where disk speed is painful anyway.

Putting It All Together

Your low-RAM compile command, all tuned:

Terminal window
CARGO_BUILD_JOBS=1 CC="ccache gcc" CXX="ccache g++" make -j2

or

Terminal window
CARGO_BUILD_JOBS=1 cargo build --release

Will it be slow? Absolutely. The laws of physics haven’t changed. But your 512MB VPS or your overclocked Pi will now compile that project instead of crashing halfway through.

Now go compile your thing. It’ll be slow, but it’ll finish. And your 2 AM self will be grateful.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Bash One-Liners Worth Remembering
Next Post
Escaping Zim: Migrate to Obsidian

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts