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

13

46

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?

Cypress answered 10/6, 2011 at 5:10 Comment(2)
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?Balfore
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.soCypress
I
47

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.

Ilowell answered 10/6, 2011 at 12:54 Comment(8)
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.Cypress
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.Ilowell
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.Cypress
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 :)Ilowell
Install r-base-dev did the trick for me, on ubuntu, with the same error as the OPAgronomy
I also needed to create the symlink manually on Ubuntu 13.10, because there is no libgfortran3-dev anymore.Mitzi
@hans_meine: I didn't need that on a fresh 13.10 machine. I have libgfortran3:amd64 installed and all is well.Ilowell
Note that if you have changed the version of gcc or g++, this solution may not be enough. Check the update alternative solution below.Drawknife
P
68

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.

Pericline answered 21/3, 2013 at 6:32 Comment(5)
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.Mitzi
Same on Ubuntu 14.04, again I tried Dirk's suggestion first (probably wise to be upgrading R regularly anyway!)Dimond
Worked for me as well!Cirenaica
Same on Ubuntu 16.04 LTS.Samarium
Still true with 22.04 and installing ggforce. Symlinking to libgfortran.so.5.Superman
I
47

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.

Ilowell answered 10/6, 2011 at 12:54 Comment(8)
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.Cypress
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.Ilowell
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.Cypress
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 :)Ilowell
Install r-base-dev did the trick for me, on ubuntu, with the same error as the OPAgronomy
I also needed to create the symlink manually on Ubuntu 13.10, because there is no libgfortran3-dev anymore.Mitzi
@hans_meine: I didn't need that on a fresh 13.10 machine. I have libgfortran3:amd64 installed and all is well.Ilowell
Note that if you have changed the version of gcc or g++, this solution may not be enough. Check the update alternative solution below.Drawknife
I
14

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.

Isaacson answered 26/9, 2014 at 16:40 Comment(2)
Note that exact matching may not be required : setting gcc to 5.4.1 made it work for gfortran 6.2.0.Jugoslavia
for me that was the issue: gcc and g++ were 13.2 and gfortran was 4.8 (!)Undershoot
A
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.

Abstain answered 29/3, 2018 at 4:41 Comment(2)
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")Have
Works for me also on CentOS 7. Thanks for sharing this!Leopoldoleor
E
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.

Epicarp answered 9/6, 2014 at 18:1 Comment(3)
same story for me on debian with package igraph.Anaemia
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)Beatnik
StackExchange should consider giving more importance to "the other valid solutions for the same problem." This answer worked for me!Holohedral
G
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
Gorlin answered 20/6, 2019 at 14:24 Comment(0)
M
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
Marga answered 7/1, 2019 at 16:58 Comment(0)
M
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.

Megganmeggi answered 3/9, 2019 at 10:15 Comment(0)
S
0

If you are using gcc44, you'll need:

yum install gcc44-gfortran
Sidonie answered 8/9, 2014 at 20:25 Comment(0)
A
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.

America answered 30/11, 2016 at 2:44 Comment(0)
T
0

I compiled GCC 7.4 from source but configured --enable-languages=c,c++ which doesn't install Fortran, which is needed.

Traitor answered 17/10, 2023 at 15:49 Comment(0)
L
0

In my case, adjusting the gcc, g++ and gfortran versions worked. But the update-alternatives --config command couldn't find the alternatives so I had to manualy install them first (in my case I had to upgrade g++ and gfortran from version 11 to 12):

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 100
sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-12 100

and them manually set them:

sudo update-alternatives --set g++ /usr/bin/g++-12
sudo update-alternatives --set gfortran /usr/bin/gfortran-12
Lampoon answered 3/7, 2024 at 10:19 Comment(0)
B
-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.

Blandishment answered 2/11, 2021 at 21:48 Comment(4)
Please don't post duplicate answers, and instead upvote the original answerInsinuating
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...Blandishment
Hi @Blandishment 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, where your answer would be a duplicate.Luella
thank you :) I will look into that!Blandishment

© 2022 - 2025 — McMap. All rights reserved.