19

I'm trying to install a package in R (package "ks"). It fails with error message:

/usr/bin/ld: cannot find -lgfortran

I've already installed the Ubuntu package build-essential:

sudo apt-get install build-essential

I've also tried adding symbolic links:

sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
sudo ln -s /usr/lib/libgfortran.so.3.0.0 /usr/lib/libgfortran.so
sudo ldconfig

I use gcc and g++ regularly, but haven't yet had reason to use gfortran.

I've seen others with this problem on gcc-4.7 (http://ubuntuforums.org/showthread.php?t=2123821), so I wonder if this is from manually installing gcc-4.7. I followed the recipe from the accepted answer here here and here here.

Running lsb_release -a prints

Ubuntu 12.04.2 LTS

and running uname -r prints

3.2.0-39-generic-pae

Can anyone offer advice for why ld doesn't work?

1
  • 1
    You tried to make symbolic links against runtime libraries, not the ones that linker wants (*.a dev libs). Looks like the path issue. Apr 1, 2013 at 20:55

4 Answers 4

19

What I did, I check on ubuntu if the versions of g++, gcc and gfortran are the same or not.

I guess it is better if you have all the three of same version.

# first check the versions:
gcc --version
g++ --version
gfortran --version

# If versions are different, then install:
sudo apt-get update
apt-get install gcc-4.9 g++-4.9 gfortran-4.9

For me this resolved all the issues which I was facing while installing "TTR" package in R using RStudio on Ubuntu.

If you further run into the issue gfortran: command not found, make sure that you have /usr/bin/gfortran linking to the actual version of gfortran you have installed (e.g. linking to gfortran-4.9):

sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-11 70
6
  • 2
    This is the correct solution. None of the above solutions worked for me Sep 2, 2018 at 15:13
  • 2
    I am on gcc 6.5, but the same methods solved the issue. Interestingly, same source: an obscure R package.
    – Ufos
    Jan 28, 2019 at 12:43
  • Yes, this was one best solution I could think of. Feb 17, 2019 at 14:13
  • 4
    More generally: apt install gfortran-<your GCC version>.
    – ZachB
    Jun 26, 2020 at 18:15
  • 1
    On Ubuntu 22.04, my gcc and g++ were 11.3.0 but gfortran not found. I installed gfortran-11 since 11.3.0 and 11.3 did not exist
    – Kermit
    Jul 24, 2023 at 12:45
9

A little more explanation to Cloud Oliver's answer.

Find out the gcc version currently being used through this command.

$sudo update-alternatives --config gcc

* 2            /usr/bin/gcc-4.7   60        manual mode

The entry with * tells which version of GCC is being used. Just press enter to keep the current settings.

If it prints "no alternatives for gcc", you might have to use this command to find out the version:

apt-cache policy gcc
  1. cd to the right gcc directory (replace the version number with yours):

    cd /usr/lib/gcc/x86_64-linux-gnu/4.7/
    
  2. You can search for libgfortran.a with the following command (optional). In my case it was present in the /usr/lib/gcc/x86_64-linux-gnu/4.8/ directory.

    find /usr/lib/gcc/x86_64-linux-gnu/ -name libgfortran.a
    
  3. Create the symlink:

    sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.a libgfortran.a
    
1
  • You should use the gfortran lib that matches the compiler instead of mixing versions. Manoj Kumar's answer below is correct.
    – ZachB
    Jun 26, 2020 at 18:13
7

You need to install gfortran. The error points out that the static lib for build-time linking is missing not the dynamic libs you tried to adjust.

6
  • When I try sudo apt-get install gfortran, I get 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.... Any thoughts?
    – user
    Apr 1, 2013 at 20:06
  • Well, on my system I get for dpkg -S /usr/lib/gcc/x86)64-linux-gnu/4.6/libgfortran.a the answer that this is from the gfortran-4.6 package. Gcc is looking for the .a file. If you installed another gcc version, the linker is looking inside a similarly named directory for libgfortran.a.
    – rfindeis
    Apr 1, 2013 at 20:36
  • Looks like a valid answer. Apr 1, 2013 at 20:59
  • Symbolic linking the 4.6 library for the 4.7 compiler might work. Also passing the correct path with -L (which is largely the same). If this fails you need to install the 4.7 version of gfortran from the ppa you used for gcc 4.7. What gfortran version do you have now?
    – rfindeis
    Apr 1, 2013 at 21:00
  • I needed to use the same trick as installing gcc-4.7 to install gfortran-4.7.
    – user
    Apr 1, 2013 at 23:43
5

Solution:

cd /usr/lib/gcc/x86_64-linux-gnu/4.6
sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.a libgfortran.a
3
  • Could you make it a little more verbose? Maybe a separate cd line at least? Nov 22, 2014 at 5:18
  • 4
    Please add an explanation. People should never blindly run commands they find on the internet. So please also don't write them.
    – s3lph
    Nov 23, 2014 at 13:41
  • Thank you, this solved it for me! In my case it was cd /usr/lib/gcc/x86_64-linux-gnu/4.9.3 and then sudo ln -s /usr/lib/gcc/x86_64-linux-gnu/4.8/libgfortran.a libgfortran.a
    – Zhubarb
    Jan 29, 2017 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .