On FreeBSD, install the LAPACK package with:

# pkg install math/lapack


The example program solves the following equation using LAPACK:

A * X = B

The LAPACK subroutine sgesv() computes the solution to a real system of linear equations A · X = B, where A is an n-by-n matrix and X and B are n-by-nrhs matrices.

call sgesv(n, nrhs, a, lda, ipiv, b, ldb, info)
Argument Type Description
n integer The number of linear equations, i.e., the order of the matrix A (n ≥ 0).
nrhs integer The number of right hand sides, i.e., the number of columns of the matrix B (nrhs ≥ 0).
a real, dimension(lda, *) Array of type real of size lda-by-n.
lda integer The leading dimension of array A (lda ≥ max(1, n)).
ipiv integer, dimension(*) The pivot indices that define the permutation matrix.
b real, dimension(ldb, *) On entry, the n-by-nrhs matrix of right hand side matrix b. On exit, if info = 0, the n-by-nrhs solution matrix.
ldb integer The leading dimension of array B (ldb ≥ max(1, n)).
info integer = 0: successful exit;
< 0: if info = −i, the i-th argument had an illegal value;
> 0: if info = i, U(i, i) is zero. The factorisation has been completed, but the factor U is exactly singular, so the solution could not be computed.
! example.f90
program main
    ! Example program that solves the matrix equation A * X = B using LAPACK.
    implicit none
    real    :: a(2, 2)  ! Matrix A.
    real    :: b(2)     ! Matrix B.
    real    :: pivot(2) ! Pivot indices (list of swap operations).
    integer :: rc       ! Return code.

    a = reshape([ 2., 3., 1., 1. ], [ 2, 2 ])
    b = [ 5., 6. ]

    call sgesv(2, 1, a, 2, pivot, b, 2, rc)

    if (rc == 0) then
        print *, 'Solution [x, y]: ', b
        print *, 'Error: ', rc
    end if
end program main

Compile, link, and run the example with:

$ gfortran9 -L/usr/local/lib/ -o example example.f90 -llapack -lblas
$ ./example
 Solution [x, y]:   0.999999940       3.00000024

If LAPACK and BLAS are compiled as static libraries, point the library search path -L to their actual location.


LAPACK95 is a Fortran 95 interface to the FORTRAN 77 LAPACK library. It brings several improvements over the original LAPACK package, by using language features introduced in Fortran 95, like assumed-shape arrays, optional arguments, and generic interfaces. The underlying LAPACK code remains unchanged, as LAPACK95 provides only interfaces to existing routines. The user’s guide and an index of all routines are available online.


Packages of LAPACK95 should be available for most Unix-like operating systems. On FreeBSD, install the package with:

# pkg install math/lapack95

If you choose to build LAPACK95 from Ports, you can either link the BLAS reference implementation or the optimised ATLAS library. But LAPACK95 can be compiled as well simply from source.