1. Overview

Boost is a free and open-source software development library. It provides various tools and utilities for C++ programmers.

Many modern C++ libraries and frameworks, such as TensorFlow and Apache Thrift, rely on Boost components. Thus, developers widely use it to create high-quality, efficient, and portable C++ code.

In this tutorial, we’ll see how to install Boost on a Ubuntu 20.04 system.

2. Installation Using the Official Repository

Firstly, we can often use the official repositories to install the Boost libraries. Let’s use the apt command to install it on Ubuntu:

$ sudo apt install libboost-all-dev

This installs all of the Boost libraries and headers on our system.

Furthermore, we can verify the Boost installation using the dpkg command:

$ dpkg -l | grep boost
ii libboost-all-dev 1.71.0.0ubuntu2 amd64 Boost C++ Libraries...
ii libboost-atomic1.71.0:amd64 1.71.0-6ubuntu6 amd64 atomic data types, ...
...

Here, we list the main local Boost-related packages.

3. Installation Using the Source Package

Basically, using the source code to build Boost libraries has several advantages, as we can perform additional tasks:

  • configure the build
  • utilize different compilers and toolchains
  • change the default naming layout

Let’s move on to the installation steps.

3.1. Downloading and Extracting the Source Package

First, we’ll download the Boost source archives from the official website. Let’s download it to our home directory using the wget command:

$ wget https://boostorg.jfrog.io/artifactory/main/release/1.81.0/source/boost_1_81_0.tar.gz
...
Length: 140221178 (134M) ...
Saving to: ‘boost_1_81_0.tar.gz’
boost_1_81_0.tar.gz 0%[ ] 211.45K 48.8KB/s ...

Next, we create a separate directory to extract the source archive. Let’s create this directory and move into it:

$ mkdir boost-ver && cd boost-ver

Further, we’ll extract the archive in this directory using the tar command:

$ tar -xzf boost_*.tar.gz

Now, we have a new directory with the Boost source files.

3.2. Running the bootstrap Script

Next, we navigate to the extracted directory:

$ cd boost_*/

Here, we’ll run the bootstrap script:

$ ./bootstrap.sh
 Building B2 engine..
Using 'gcc' toolset.
Copyright (C) 2019 ...
...

Also, we can specify target libraries to be built with the –with-libraries option.

3.3. Building and Installing the Libraries

Boost has a Boost.Build system for managing C++ projects. Boost.Build facilitates the building and installation of the Boost C++ libraries using various compilers on each supported platform.

Basically, b2 is the executable that builds a Boost.Build managed project.

Notably, when using a b2 build, the default prefix is /usr/local. Further, we can change this prefix with the –prefix directive:

$ ./b2 install --prefix=<prefix>

As a result, this new path will now hold the libraries inside <prefix>/lib and the Boost header files inside <prefix>/include.

Generally, the Boost iostreams library requires the zlib and bzip2 libraries for working with gzip and bzip2 archives. Installing them manually is often optional, but it avoids common installation pitfalls. Thankfully, most native package management systems take care of this concern.

However, we can download, extract, and link these libraries manually:

$ ./b2 install -sZLIB_SOURCE=<path_to_zlib-source-dir> -sBZIP2_SOURCE=<path_to_bzip2-source-dir>

Here, we’re using the default gcc toolset. However, if we want to use a different toolset, we can use one from the official Boost webpage. With the toolset property, we can specify the toolset to use when building.

For now, let’s assume that zlib and bzip2 are already available and gcc is the default toolset.

Let’s run the b2 command with the prefix as /opt/boost to build the Boost libraries and headers:

$ sudo ./b2 install --prefix=/opt/boost
Performing configuration checks
    - default address-model: 64-bit ...
...

In essence, we build and install the Boost libraries and headers (.hpp files) in the /opt/boost/lib and /opt/boost/include system directories respectively.

3.4. Testing the Installation

Again, let’s verify the Boost installation on our system with the dpkg command:

$ dpkg -l | grep boost
ii libboost-iostreams1.71.0:amd64 1.71.0-6ubuntu6...
...

This shows us a list of all Boost-related packages installed on our system.

4. Basic Usage

Let’s take a basic example of using the Boost libraries in a C++ program. In this program, we’ll calculate the product of two numbers:

#include <iostream>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
int main()
{
  long long num1 = 1523844560192817464;
  long long num2= 598274671729184766;
  int128_t result = (int128_t) num1 * num2;
  cout << "The product of the two integers is:" << "\n" << result;
  return 0;
}

Specifically, in this example, we’re using the Boost.Multiprecision library. This library supports long-range and more precise integers, as well as floating-point and rational data types. These may be helpful since the standard C++ libraries don’t support these data types.

The two variables, num1 and num2, store two huge numbers. Finally, the variable result stores their product.

If we have installed Boost using the source package, we’ll need to link against the Boost libraries for compiling our test program. For example, let’s say we save the code in a file called demo.cpp. Then, we can compile it using the g++ compiler:

$ g++ test/demo.cpp -o output -I/opt/boost/include

Here, we’ve passed the path to the Boost .hpp files as /opt/boost/include to the g++ compiler. The -I option allows the preprocessor to locate the Boost header files. This creates an executable file called output. Finally, we can run this file to see the output:

$ ./output
The product of the two integers is:
911677604015661803882226861367553424

Similarly, we can also use other Boost header files in our projects.

5. Conclusion

In this article, we’ve seen two ways of installing Boost on Ubuntu Linux. Notably, installing from the package manager is usually easier in comparison to installing from the source code.

Also, package managers eliminate the need to explicitly link Boost libraries while running our projects. However, installing from source code gives the advantage of installing the latest version of the Boost software. This is because the default repository of an OS may not contain the most recent version.