How to install Armadillo library in Ubuntu

Armadillo is a high-quality linear algebra library for the C++ language. The good thing is that it follows similar syntax with MATLAB. It provides classes for vectors, matrices and cubes. It is easy to install. There are three ways to do it:

a. Install pre-build armadillo package

First, we need to install the dependencies since various matrix decompositions are provided through integration with LAPACK, or one of its high-performance drop-in replacements

sudo apt install libopenblas-dev liblapack-dev

in official documentation, it also recommends installing cmake, libarpack2-dev, libsuperlu-dev

b. Install from source

If we want the most recent version, for example, Ubuntu18 provides 8.4 and the current version is 9.8, we need to build it from the source. We need to download it from the official repository and most likely do the followings

wget http://sourceforge.net/projects/arma/files/armadillo-9.880.1.tar.xz
tar -xvf armadillo-9.880.1.tar.gz
cd armadillo-9.880.1
./configure
make
sudo make install

c. Install from source

This is an alternative to b where we use checkinstall install of make install. The reason is mainly to avoid a cluttered /usr/local folder. It is advised first to understand how the utility works (check references) and then apply it in our case.

Now when it comes to using with CMake for example, just use

cmake_minimum_required(VERSION 3.16)
project(arma)

set(CMAKE_CXX_STANDARD 17)

add_executable(arma_test main.cpp)
target_link_libraries(arma_test armadillo)

with a sample file like:

#include <armadillo>
#include <iostream>

using namespace arma;
using namespace std;

int main() {

    mat A(2, 3);// directly specify the matrix size (elements are uninitialised)

    A << 1 << 2 << 3 << 3 << endr
      << 2 << 0 << 6 << 2 << endr
      << 3 << 4 << 9 << 7 << endr;

    A.print("A:");

    mat B = orth(A);

    B.print("B:");

    cout << "The rank of A is " << arma::rank(A) << endl;

    return 0;
}

References (last accessed 24/05/2020)
Official Documentation
How to Install Armadillo
Getting started with Armadillo a C++ Linear Algebra Library on Windows, Mac and Linux
Installing Armadillo
How To Build Packages From Source Using CheckInstall