Count Lines of Source Code using cloc on Ubuntu 22.04

Count Lines of Source Code using cloc on Ubuntu 22.04

The cloc is a command line tool that can be used to count lines of code in software projects. It analyzes the files and produces a report that includes the number of blank lines, comment lines, and physical lines of source code. This tool supports many programming languages.

This tutorial shows how to install and use cloc on Ubuntu 22.04.

Install cloc

Get the latest version tag of cloc release from GitHub and assign it to variable:

CLOC_VERSION=$(curl -s "" | grep -Po '"tag_name": "v\K[0-9.]+')

The cloc is a single Perl script. Download it from releases page of the cloc repository:

sudo curl -Lo /usr/local/bin/cloc "${CLOC_VERSION}.pl"

Set execute permissions:

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

Check cloc version:

cloc --version

Testing cloc

Create a main.c file for testing:

nano main.c

Add the following lines of code:

#include <stdio.h>

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

    return 0;

Run cloc command to count lines of code:

cloc main.c


       1 text file.
       1 unique file.
       0 files ignored. v 1.96  T=0.03 s (32.7 files/s, 261.4 lines/s)
Language                     files          blank        comment           code
C                                1              2              1              5

Compressed files can be used as well. Download the tar.gz file of the source code from GitHub:

sudo curl -Lo cloc.tar.gz ""

Execute cloc command by providing tar.gz file as argument:

cloc cloc.tar.gz

Directory that contains a source code can be provided as argument as well:

tar xf cloc.tar.gz
cloc cloc-master

Uninstall cloc

If cloc is no longer necessary, remove a script file:

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

Leave a Comment

Cancel reply

Your email address will not be published.