Building R package and error "ld: cannot find -lgfortran"

I had the same problem when trying to install the CRAN package VGAM on Ubuntu 12.10 64bit. I already had r-base-dev installed, but Andrew Redd's second comment to Dirk Eddelbuettel's answer worked for me.

Specifically, I was getting two errors:

/usr/bin/ld: cannot find -lgfortran
/usr/bin/ld: cannot find -lquadmath

Which were fixed by the lines:

sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libquadmath.so.0 /usr/lib/libquadmath.so

Note that only the first line would be necessary to take care of the problem from the original post. The second line fixed of my additional error with lquadmath.


For the Debian / Ubuntu family, we usually recommend

 $ sudo apt-get install r-base-dev

as it pulls in all packages commonly needed for compiling. And this approach gets tested all the time as the automated package builders rely on this (as well as additional per-package Build-Depends). The gfortran package is listed here too; maybe you have a broken link from a prior installation so I'd also try dpkg --purge gfortran; apt-get install gfortran. That said, dozens of R packages (and R itself) use Fortran so there should not be any magic here.


It looks like other suggestions already fixed your problem, but your question also applied to me but the solution was different in my case. My problem was that my gcc and g++ versions differed from my gfortran version. I used the following to switch them so that they were all the same.

  1. Check what version of gcc, g++, and gfortran you have:

    g++ --version
    gcc --version
    gfortran --version
    
  2. Match them so that they are all the same:

    sudo update-alternatives --config g++
    sudo update-alternatives --config gcc
    sudo update-alternatives --config gfortran
    

In my case, I only had one version of gfortran so I simply changed the g++ and gcc versions to match that of gfortran.

Tags:

Ubuntu

R

Gfortran