How to install Boost library in Ubuntu or any Debian based distribution

Boost is a portable set of C++ programming language libraries. It contains libraries for pseudo-random number generation, linear algebra, multi-threading, image processing, regular expression, and unit testing. At the time of writing this article, boost has 168 different libraries included in boost.

In this article, you will find the steps to install the boost library in Ubuntu or any other Debian-based distributions.

Install boost library in C++

The boost library can be installed on your Ubuntu machine in a couple of ways. One of them is to install it from the system repository, and the second option is to download and install the package from the source, or else you can use the source file as a portable library.

Both methods have their own advantages. If you are considering the first method, then you will not find the latest version of the library, but the installation will be simple and less time-consuming.

If you don’t care about the latest release, then you can choose the first option, and those who want to use the latest version of boost in their system can use the source method to install boost libraries.

Install Boost library from Ubuntu system repository

To start installation run the below command into your terminal and wait for the process gets complete.

$ sudo apt  install libboost-all-dev
Install boost library from repo
Install boost library from repo

Once the installation is complete, you can use the library in your project.

Install Boost library from the source package for all Linux distributions

With the above method, you can get the boost library installed on your Ubuntu machine, but the version will be older compared to this one, and the second thing is that this method will work on all Linux systems without any issue.

So, let me get into step straight away.

First of all, you will require the latest release of boost from the official page, or you can also get a boost from the Github release section of boost.

Once the pages get loaded, click on “Download”, which redirects you to the index page. From there you can get libraries in different compressions.

Click on Download
Click on Download

At the time of writing, this instruction’s latest version is 1.79.0. I’m downloading boost_1_79_0.tar.gz for this article. If you want, you can go with other compressed files too.

Select compressed Boost 1.79.0
Select the compressed version of Boost 1.79.0

Once the file is downloaded, go to the directory where the file is located and extract it using the below command if you have downloaded boost_1_79_0_.tar.gz.

$ tar xvf boost_1_79_0.tar.gz 

After that, you can use these libraries as portable with your project, so just make sure to remember the path where you have extracted the libraries.

And if you want to install libraries on your system-wide, then pass the following command to the terminal and specify the library directory on your program file to prevent errors.

./bootstrap.sh --prefix=/usr/
./b2
sudo ./b2 install

Demo Sample

Once you are done with the above procedure, you can test the library functionality with the sample program. You can copy-paste the code snippet from here and save it as an example.cpp.

#include <usr/include/boost/lambda.hpp>                     // Remove this line if you are using portable library
#include</boost/lamda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>

int main()
{
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;

    std::for_each(
        in(std::cin), in(), std::cout << (_1 * 3) << " " );
}

I’m assuming you are using the g++ library for compilation. Go to the directory where example.cpp is located and run the following command.

$ g++ example.cpp -o sample

For portable libraries user can run the below command:

$ g++ -I /path/boost_1_79_0 example.cpp -o sample

Once the program is compiled successfully you can test out this basic sample.

$ ./sample
1
3
2
6
3
9

That’s all to install boost libraries on Ubuntu and other Linux distributions.

If any problems occur while installation, do let us know in the comment section.

Leave a Reply