Friday, June 04, 2021

How to install GNU Scientific Library on Ubuntu

GNU Scientific Library (GSL) is an essential library for scientific computing using C. You may have to use this library to generate random numbers or solve a system of ODEs. Installing GSL on a Linux machine is straightforward. Even then some students find it difficult. Here is a step-by-step guide to install GSL on your Ubuntu machine.

1. Download GSL:

    https://www.gnu.org/software/gsl/

2. It will be a compressed tar file, like gsl-x.x.tar.gz

3. Place this file in the home 

4. Open the terminal and unpack this downloaded file:

    $ tar -zxvf gsl-x.x.tar.gz

5. A new folder will be created. cd to that folder.

6. Now we will configure, make and install GSL:

    $ ./configure
   $ make
   $ sudo make install

Each of these steps may take some time. Wait patiently. 
7. Close the terminal and open .bashrc file in the home. It is a hidden file. If required use Ctrl+H to access it. 
Edit this file to add:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/
8. Save the edited file and close it.
9. Now get back to the terminal and compile a C code that uses GSL. For example, use the test code testgsl.c:

    #include <stdio.h>
    #include <gsl/gsl_sf_bessel.h>
    int
    main (void)
    {
        double x = 5.0;
        double y = gsl_sf_bessel_J0 (x);
        printf ("J0(%g) = %.18e\n", x, y);
        return 0;
    }

To compile:

    $ gcc -Wall testgsl.c -o run -lgsl -lgslcblas -lm
10. Now run the compiled code:

         $ ./testgslrun

This should print:

    $ J0(5) = -1.775967713143382900e-001 


Done!! Happy Computing. 


#C #GNU #GSL #computing #programming





No comments: