Install Ccache on Ubuntu 22.04

Install Ccache on Ubuntu 22.04

Ccache is a powerful tool that speeds up the recompilation process by caching previously compiled objects. This can be especially beneficial for developers working on large projects or frequently building and rebuilding code. This tutorial explains how to install Ccache on Ubuntu 22.04.

Install Ccache

Retrieve the most recent release version of Ccache from the official GitHub repository:

CCACHE_VERSION=$(curl -s "https://api.github.com/repos/ccache/ccache/releases/latest" | grep -Po '"tag_name": "v\K[0-9.]+')

Download Ccache based on the previously obtained version:

wget -qO ccache.tar.xz https://github.com/ccache/ccache/releases/latest/download/ccache-$CCACHE_VERSION-linux-x86_64.tar.xz

Create a temporary directory and extract the contents of the tar.xz file into it:

mkdir ccache-temp
tar xf ccache.tar.xz --strip-components=1 -C ccache-temp

Move ccache command to /usr/local/bin directory:

sudo mv ccache-temp/ccache /usr/local/bin

Set execute permission:

sudo chmod a+x /usr/local/bin/ccache

Now, ccache is accessible as a system-wide command for all users. Verify the version using the following command:

ccache --version

Finally, remove temporary directory and downloaded file:

rm -rf ccache.tar.xz ccache-temp

Testing Ccache

One of the ways to use Ccache, prefix the compilation command with ccache.

For demonstration purpose, create a main.cpp file:

nano main.cpp

Add the following lines of code:

#include <iostream>

int main() {
    std::cout << "Hello world" << std::endl;

    return 0;
}

Compile a code by prefixing the command with ccache as follows:

ccache g++ main.cpp -o test

Uninstall Ccache

To uninstall Ccache, delete the executable file:

sudo rm -rf /usr/local/bin/ccache

Leave a Comment

Cancel reply

Your email address will not be published.