<- Back to the HOWTO section

How to enable ccache

  • Audience: All users
  • Requires: MacPorts >=1.6.0


This HOWTO covers how to enable building with ccache. It is a compiler cache. It uses the gcc -E switch and a hash to detect when a compilation can be satisfied from cache. The effect is that packages frequently compile 5-10 times faster than they would otherwise.


Step 1: Install ccache

$ sudo port install ccache

Step 2: Edit macports.conf

Open /opt/local/etc/macports/macports.conf in your favorite editor. Find the line

configureccache     no

and change it to

configureccache     yes

And you are done. From now on, MacPorts will use ccache for building.


Please note that cache files are stored in .ccache in your home directory. To see some statistics how useful ccache is for you and how much disk space it takes, use

$ ccache -s

If you believe ccache takes up too much disk space for you, there is the possibility to set a maximum by using something like

$ ccache -M 2G

This makes ccache occupy a maximum of 2 GiB disk space to store cached files.

If you want to free some space, do a cleanup with

$ ccache -c

Optional Parts

Use ccache outside MacPorts

MacPorts does not create symlinks to the local compiler therefore you need to create them manually.

One way could be to use the following script that finds the available compilers and create symlinks on /opt/local/libexec/ccache.


## run with sudo or adjust prefix/dest_links

# adjust if needed
bin_paths="/usr/bin /opt/local/bin /usr/local/bin"
ccache_bin=$(which ccache)

# set umask to avoid strict starting shell
umask 022

if [ ! -d ${dest_links} ]; then
  install -d ${dest_links}

find $bin_paths \( -name "gcc*" -or -name "cc*" \
  -or -name "g++*" -or -name "c++*" \
  -or -name "clang++-mp*" -or -name "clang-mp*" \) | \
  egrep -v "cmake*|ccache*|ccomps*|c\+\+filt*" | \
while read file; do
  base=$(basename $file)
  ln -s ${ccache_bin} ${dest_links}/${base}

echo ">> add $dest_links in front of your \$PATH"

Now to use ccache also outside MacPorts you have to edit your PATH which is set in your .profile or .bash_profile.

First, locate the file you are using. If there is a .bash_profile then edit this file, if there is only .profile then you want to edit this.

There will be this line in there:

export PATH=/opt/local/bin:/opt/local/sbin:$PATH

Now add the ccache binary path /opt/local/ccache/bin in front of already existing paths:

export PATH=/opt/local/libexec/ccache:/opt/local/bin:/opt/local/sbin:$PATH

Important: Reopen your Terminal afterwards.

If you now use commands like gcc or clang they automatically go through ccache.

<- Back to the HOWTO section

Last modified 22 months ago Last modified on May 23, 2019, 4:24:48 PM