Question

I'm trying to install the package lars. Ubuntu 11.04 Natty 64-bit. From building I get:

* installing *source* package âlarsâ ...
** libs
gfortran   -fpic  -O3 -pipe  -g -c delcol.f -o delcol.o
gcc -shared -o lars.so delcol.o -lgfortran -lm -L/usr/lib64/R/lib -lR
/usr/bin/ld: cannot find -lgfortran
collect2: ld returned 1 exit status
make: *** [lars.so] Error 1
ERROR: compilation failed for package âlarsâ

gfortran is installed and when I run gfortran --version I get

gfortran --version GNU Fortran (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

sudo ldconfig -v gives the error

/sbin/ldconfig.real: Cannot stat /usr/lib/libgfortran.so: No such file or directory

I have already removed and reinstalled gfortran. What do I need to fix this?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

Same problem installing R package minqa on ubuntu 12.04, R3.1.0., an x86 32bits (actually it was part of the caret package installation).

Solved by

sudo ln -s /usr/lib/i386-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so

r-base-dev reinstall didn't work and I didn't try to re-install gfortran because of all the dependencies.

Depending on the system/version,

ls -l /usr/lib/libgfortran.so

checks that the link exists/is right.

I use Centos and I can't get r-base-dev. I have also installed gfortran and its version matches that of gcc and g++; it still didn't work. However, I solved this problem by creating ~/.R/Makevars, using

cd ~
mkdir .R
touch Makevars

I found the directory where I installed gfortran (apparently the problem is that R can't find it) by

which gfortran

It said I installed gfortran in usr/bin/gfortran. Then I added flags to .R/Makevars to tell R to use:

F77 = /usr/bin/gfortran
FC = $F77
FLIBS = -L/usr/bin/gfortran

You can edit the Makevars file this way:

vi .R/Makevars

Now you have entered the vi program that can edit text files. Type i to edit; you will see INSERT by the bottom of the terminal window. Then you can input what I put above. To save the changes and quit vi, press the esc key, and type :wq.

I'm not totally sure if I put the FLIBS line correctly, since it's very different for MacOS. In MacOS, there's a directory under gfortran that has the libraries to link to, but apparently gfortran is not a directory in linux. At least this worked for me, and also solved the problem of /usr/bin/ld: cannot find -lquadmath, so I installed R packages requiring gfortran smoothly.

Just leaving this here for future reference as in my case (Amazon Linux EC2 AMI) the issue was merely with the naming of the symbolic link and not with its location.

sudo ln -s /usr/lib64/libgfortran.so.3 /usr/lib64/libgfortran.so
sudo ln -s /usr/lib64/libquadmath.so.0 /usr/lib64/libquadmath.so

I didn't have to install any libraries. Posting what worked for me, maybe it will be useful for someone.

I had ~/.R/Makevars defining to use CC=gcc-8. Default gcc on my machine is 7.4.0, but I installed gcc-8. At the same time I didn't have gfortran 8, but only 7.4.0. Commenting out the line in Makevars makes compilation fall back to use default gcc-7, and it was successfully using gfortran-7 lib then.

If you are using gcc44, you'll need:

yum install gcc44-gfortran

For future lost souls, it also helps to verify compiler versions all match (per https://askubuntu.com/questions/276892/cannot-find-lgfortran). In my case gcc and gfortran were both 4.8.4, but g++ was 4.6.

For anyone who reaches this page with the same error on a Mac, try the following:

Install Homebrew and run:

brew install gcc

Then, create a file ~/.R/Makevars with the contents (being mindful that this corresponded to gcc version 9.1.0):

VER=-9
CC=gcc$(VER)
CXX=g++$(VER)
CFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
CXXFLAGS=-mtune=native -g -O2 -Wall -pedantic -Wconversion
FLIBS=-L/usr/local/Cellar/gcc/9.1.0/lib/gcc/9

  • R v3.6.0
  • gcc v9.1.0
  • Homebrew v2.1.6
  • macOS v10.14.5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top