Sunday, February 19, 2017

MPIR library installation on Ubuntu 16.04

Environment: Using Vagrant and Virtual Box

Open the terminal of your host machine.

Create a folder:

mkdir mpir
cd mpir

Initiate vagrant project using ubuntu 16.04 xenial64:

vagrant init ubuntu/xenial64

Vagrant up to spawn the VM

vagrant up --provider virtualbox

Machine starts.

To login into the VM:

vagrant ssh

You will be logged in as ubuntu user.

To resynchronize the package index files from its sources:

sudo apt-get update

Install these prerequisites before compiling:

sudo apt-get install yasm m4 build-essential unzip wget -y

Download the mpir library:


Unzip the downloaded zipped file:


Change directory to mpir-3.0.0

cd mpir-3.0.0/

Run the following commands:

make check

Once the above commands are successful, run:

sudo make install

MPIR produces libraries named libmpir, etc. under /usr/local/lib,  and the header file mpir.h under /usr/local/include

Create a soft link or set the variable to find mpir library which is not on a standard location:

sudo ln -s /usr/local/lib/ /usr/lib/
export LD_LIBRARY_PATH=/usr/local/lib/

To test library is working, copy paste the code snippet 

/*Program to check the Working of mpir library */


using namespace std;

int main()
 mpf_t a;         //mpir float variable
 mpf_init(a);     //initialise a
 mpir_ui two = 2; //mpir unsigned integer variable
 FILE* stream;    //file type pointer to output on standard output (console)
 mpf_init_set_ui (a, 2);          //set value of a to 2
 mpf_out_str (stream, 10, 2, a); //output value of a
 cout << "\nMPIR working" << "\n" ;

and run:

g++ test.cpp -lmpir -o test


  1. Thank You very much it helped me to solve my issue...i was stuck for more than a day

  2. Hi,
    I've successfully done step "./configure", but I couldn't pass the make or make check step.
    Can you please help?

    1. Can you share the error message you are getting??

  3. I am not able to create soft link. Any idea. Its a mac OS.

    $ sudo ln -s /usr/local/lib/libmpir.dylib /usr/lib/libmpir.dylib
    ln: /usr/lib/libmpir.dylib: Operation not permitted

    1. have you tried to add -f ?
      sudo ln -sf /usr/local/lib/libmpir.dylib /usr/lib/libmpir.dylib

  4. thanks for the article.
    Can you please let me know how can I print the output without scientific notation.

  5. undefined reference to `All math functions', unable to run the program, any fixes.

    Tried below no go for first one, second line works, but still codes do not run. Same code runs in my friend's laptop.

    sudo ln -s /usr/local/lib/ /usr/lib/


    export LD_LIBRARY_PATH=/usr/local/lib/

    Note: sample code runs fine

    Error: g++ -std=c++11 -lmpir -o big big.cpp
    big.cpp:(.text+0x56c): undefined reference to `__gmpf_mul'

  6. While following I've installed mpir3.0.0 from official site on Ubuntu Desktop 20.04 LTS. However, after running code I am getting:

    Segmentation fault (core dumped)

    Any suggestions? Thank you

    1. Hi,I just use ubuntu 20.04 LTS
      You can try to change the code "FILE* stream" to "FILE* stream = NULL"
      this works for me, hopes it's works for you