Install Ninja Build System on Ubuntu 22.04

Install Ninja Build System on Ubuntu 22.04

Ninja is a build system that allows to build binaries from source code. It can be used as alternative to the Make. Ninja is focused on speed. To achieve high performance, Ninja doesn't have all features that Make build system provides, such as string manipulation.

This tutorial explains how to install Ninja on Ubuntu 22.04.

Install Ninja

Download the latest version of Ninja from GitHub:

sudo wget -qO /usr/local/bin/ninja.gz https://github.com/ninja-build/ninja/releases/latest/download/ninja-linux.zip

Extract executable file from archive:

sudo gunzip /usr/local/bin/ninja.gz

Next, set execute permission:

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

Now, the ninja command is available for all users as a system-wide command.

We can check Ninja version:

ninja --version

Testing Ninja

For testing purpose, we will build an executable from C source code. Make sure you have installed GNU C compiler in your system:

sudo apt install -y gcc

Create a new directory to store project files and navigate to it:

mkdir helloworld && cd helloworld

Create a main.c file:

nano main.c

Once the file is opened, add the following code:

helloworld/main.c

#include <stdio.h>

int main() {
    printf("Hello world\n");

    return 0;
}

By default, configuration file of the Ninja is called build.ninja. Create it:

nano build.ninja

Add the following code:

helloworld/build.ninja

rule compile
  command = gcc -Wall -c $in -o $out

rule link
  command = gcc $in -o $out

build hello.o: compile main.c
build hello: link hello.o

Execute the ninja command to build program:

ninja

Run a program:

./hello

Uninstall Ninja

If Ninja is no longer needed, remove executable file:

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

Leave a Comment

Cancel reply

Your email address will not be published.