Skip to content

Install CPR Library for fetching api response in linux

Debraj Das edited this page Mar 6, 2026 · 1 revision

Building and installing the library globally is definitely the way to go if you plan on doing a lot of C++ API fetching. It saves you from waiting for it to download and compile every single time you start a new project.

Since you already installed the required tools (cmake, git, and the libcurl headers) in our previous steps, you are completely ready to go.

sudo apt update
sudo apt install build-essential cmake git libcurl4-openssl-dev libssl-dev

Here is how to build CPR from its source code and install it permanently onto your Debian system:

Step 1: Clone, Build, and Install CPR Globally

Open your terminal and run these commands one by one. I recommend running this in your home directory or a Downloads folder, as you can delete the CPR source code folder after it is installed.

1. Download the CPR source code:

git clone https://github.com/libcpr/cpr.git
cd cpr

2. Configure the build:

Note: We are adding -DCPR_USE_SYSTEM_CURL=ON. This tells CPR to use the libcurl you already installed on Debian, rather than downloading and building its own version, saving a ton of time.

cmake -S . -B build -DCPR_USE_SYSTEM_CURL=ON

3. Compile the library:

cmake --build build

(This will take a minute or two. Go grab a sip of water!)

4. Install it globally (requires sudo):

Bash

sudo cmake --install build

Boom! CPR is now installed in your system's standard library folders (usually /usr/local/include and /usr/local/lib). You can safely delete the cpr folder you downloaded if you want to clean up.


Step 2: How to use it in your projects now

Because it is installed globally, your life just got a lot easier. You have two ways to compile your projects going forward:

Method A: The Raw g++ Command (No CMake required!)

If you just have a single main.cpp file and don't want to bother with CMake at all, you can now compile it directly from the terminal. You just have to link cpr and its dependency curl:

g++ main.cpp -o app -lcpr -lcurl -lssl -lcrypto

./app