Installing OpenMPI from source

You can download the latest version of OpenMPI from its official page -

At the time of writing this (Oct, 2016), the latest version was 2.0.1, and so I will be referring to it but you can use the same procedure to install any version of the implementation just by changing the version number. The source code is packed (as is so often) as a TAR (Tape ARchive) file.

Go ahead and download the archieve. In my case that file happens to be : openmpi-2.0.1.tar.gz and follow these steps.

  1. Go to the folder where you downloaded the file, and unpack the file using the command.

    $ tar -xvf openmpi-2.0.1.tar.gz

  2. Now go into the folder that is created after the unpacking.

    $ cd openmpi-2.0.1

  3. Configure the installation file.

    $ ./configure --prefix="/home/$USER/.openmpi"

  4. Install it.

    $ make
    $ sudo make install

  5. Include the path to our path environment the path installation_directory/bin
    and to the library environment variable installation_directory/lib/
    Assuming your system uses bash, you can use the command export as follows

    $ export PATH="$PATH:/home/$USER/.openmpi/bin"
    $ export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/$USER/.openmpi/lib/"

  6. If you want the exportation to be effective for the next sessions and terminals, you'll have to write the exports in the environment variable's file. By default it should be /home//.bashrc, for bash users.

    $ echo export PATH="$PATH:/home/$USER/.openmpi/bin" >> /home/$USER/.bashrc
    $ echo export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/$USER/.openmpi/lib/" >> /home/$USER/.bashrc

  7. Install the openmpi-bin library.

    $ sudo apt-get install openmpi-bin

  8. And finally, do an update.

    $ sudo apt-get update

Getting your hands dirty with an example

Lets take a look at a hello world computer program which initializes MPI, executes a single print statement and then finalizes (i.e. Quits) MPI.

Implementation in C++

/* FILENAME : helloWorld_1.c */

#include < stdio.h>

/* Following header files provide the basic MPI definitions and data types */
#include < mpi.h>   

int main(int argc, char **argv) {
  /* Start MPI */
  MPI_Init(&argc, &argv); 
  /* Print a line */
  printf("Hello world\n");
  /* Exit MPI */

Compile the source to get the executable

 mpicc helloWorld_1.c -o helloWorld_1 

Execute it

 mpirun -np 4 helloWorld_1 

Implementation in Fortran 90

program helloWorld_2
    ! FILENAME : helloWorld_2.f90 
    implicit none
    ! Following header files provide the basic MPI definitions and data types 
    include 'mpif.h'
    ! Start MPI 
    call mpi_init(ierr)

    ! Print message to screen
    write(6,*) 'Hello World!'
    ! Exit MPI
    call mpi_finalize(ierr)

end program helloWorld_2

Compile the source to get the executable

 mpif90 helloWorld_2.f90 -o helloWorld_2 

Execute it

 mpirun -np 4 helloWorld_2