45

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Andrew Redd
  • 4,632
  • 8
  • 40
  • 64
  • Couple of questions: 1) Is there really a file /usr/lib/libgfortran.so? (and if it's a symlink, is the file it points to really there?) 2) Is the location of libgfortran.so in your LD_LIBRARY_PATH? – geoffjentry Jun 10 '11 at 06:09
  • I checked the sources. This is a standard R build nothing special of odd about it. Single fortran file so I'm assuming that it is the standard R build trying to link against libgfortran.so – Andrew Redd Jun 10 '11 at 14:09

11 Answers11

67

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.

kevin
  • 2,998
  • 4
  • 23
  • 17
  • It seems to me that libgfortran3-dev is missing on Ubuntu 13.10; there are such packages for newer versions, but for R (from the official Ubuntu packages), one seems to need to link against libgfortran.so.3. In other words, I only got it to work with this manual symlinking solution, because installing r-base-dev (and thus, the gfortran packages) did not suffice. – hans_meine Dec 05 '13 at 17:03
  • 5
    Same on Ubuntu 14.04, again I tried Dirk's suggestion first (probably wise to be upgrading R regularly anyway!) – Louis Maddox Aug 17 '15 at 20:33
  • Worked for me as well! – NoBackingDown Feb 09 '17 at 21:30
  • Same on Ubuntu 16.04 LTS. – Chris Jun 20 '18 at 16:34
  • Still true with 22.04 and installing ggforce. Symlinking to libgfortran.so.5. – Bouncner Feb 21 '22 at 08:48
44

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 1
    Dirk, usually your advice is spot on, but something funny is going on with the config here. In the question I show the build messages. gfortran is found and used to compile delcol.f successfully, but then links again gfortran, which it cannot find. – Andrew Redd Jun 10 '11 at 14:05
  • 1
    Did you by chance mess around with the symbolic links between the different `gcc-*`, `g++-*` and `gfortran-*` versions and/or their library equivalents? The 'something funny' is sometimes simple operator error. On my box, `libgfortran.so.3` lives in `/usr/lib/x86_64-linux-gnu/` and comes from the `libgfortran3` package. – Dirk Eddelbuettel Jun 10 '11 at 14:11
  • 8
    Yes that is the same for mine. There was a problem with the link /usr/lib/libgfortran.so once that was correct to point to /usr/lib/x86_64-linux-gnu/libfortran.so.3.0.0 things work again. – Andrew Redd Jun 10 '11 at 16:33
  • 1
    Careful. My Ubuntu 11.04 has no /usr/lib/libgfortran.so link. Try `ldconfig -p | grep libgfortran` and it should show that libgfortran.so.3 is found from the `/usr/lib/x86_64-linux-gnus` directory. Or else my (pretty new) box at work is off :) – Dirk Eddelbuettel Jun 10 '11 at 16:38
  • 1
    Install r-base-dev did the trick for me, on ubuntu, with the same error as the OP – pocketfullofcheese Feb 06 '13 at 19:29
  • I also needed to create the symlink manually on Ubuntu 13.10, because there is no libgfortran3-dev anymore. – hans_meine Dec 05 '13 at 17:09
  • @hans_meine: I didn't need that on a fresh 13.10 machine. I have `libgfortran3:amd64` installed and all is well. – Dirk Eddelbuettel Dec 05 '13 at 18:39
  • Note that if you have changed the version of `gcc` or `g++`, this solution may not be enough. Check the `update alternative` solution below. – Gumeo Feb 11 '19 at 14:00
13

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.

dlpolanco
  • 139
  • 1
  • 7
  • Note that *exact* matching may not be required : setting `gcc` to 5.4.1 made it work for `gfortran` 6.2.0. – Evpok Feb 14 '17 at 08:59
9

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.

Lambda Moses
  • 433
  • 5
  • 14
  • Works for me on CentOS 7; life saviour! Here's the `Makevars` file, adapted from `rstan` install guide for future reference. ```dotR <- file.path(Sys.getenv("HOME"), ".R") if (!file.exists(dotR)) dir.create(dotR) M <- file.path(dotR, "Makevars") if (file.exists(M)) file.remove(M) if (!file.exists(M)) file.create(M) cat("F77 = /usr/bin/gfortran", "FC = $F77", "FLIBS = -L/usr/bin/gfortran", file = M, sep = "\n", append = TRUE) readLines(M) # install a package that requires gfortran install.packages("bridgesampling")``` – urganmax Dec 17 '19 at 16:10
  • Works for me also on CentOS 7. Thanks for sharing this! – user553965 Nov 11 '20 at 17:25
8

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.

Ogaga Uzoh
  • 2,037
  • 1
  • 10
  • 12
Yano
  • 598
  • 5
  • 12
  • same story for me on debian with package igraph. – fabians Dec 01 '14 at 13:22
  • 1
    Note that on some machines you may need to run `sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so` instead (this is what worked for me on Linux Mint 18.3) – seaotternerd Jul 12 '18 at 00:24
  • StackExchange should consider giving more importance to "the other valid solutions for the same problem." This answer worked for me! – omar Mar 01 '19 at 00:08
4

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
Megatron
  • 15,909
  • 12
  • 89
  • 97
2

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
2

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.

jangorecki
  • 16,384
  • 4
  • 79
  • 160
0

If you are using gcc44, you'll need:

yum install gcc44-gfortran
0

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.

Community
  • 1
  • 1
Neil
  • 1,578
  • 2
  • 10
  • 7
-1

As a follow-on to Megatron's answer for Mac homebrew, I had a similar problem with dependencies:

ERROR: configuration failed for package ‘openssl’
removing ‘/usr/local/lib/R/4.1/site-library/openssl’
Warning in install.packages :
installation of package ‘openssl’ had non-zero exit status

just typed brew install openssl into bash and it worked on next packages.install.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    Please don't post duplicate answers, and instead upvote the original answer – Evgeny Bovykin Nov 03 '21 at 09:55
  • 1
    it's not duplicate, it refers to a different problem of the same category that can be solved by using another instance of the mentioned solution. but I understand if my solution should refer to the original problem... – bluesdriv3r Nov 03 '21 at 10:06
  • Hi @bluesdriv3r welcome to Stack Overflow :) as you say, answers "should refer to the original problem". Future readers searching for solutions to your problem are unlikely to look at this question about gfortran, and more likely to look at e.g. [this question about openssl](https://stackoverflow.com/questions/59175791/r-devtools-package-installation-fails-to-find-openssl), where your answer would be a duplicate. – veryreverie Nov 03 '21 at 17:07
  • thank you :) I will look into that! – bluesdriv3r Nov 03 '21 at 18:01