I have a new version of gcc installed from source in my directory on a larger computer system which has another (older) version used as default. I have a few programs I need to compile using this newer version so I need to switch the version my system automatically uses somehow.

The newer gcc verion has a set of executables in gcc_9_2/bin/ that look like:

g++-9.2   gcc-9.2   gfortran-9.2

i.e with version numbers attached.

The simplest thing that occurred to me was to change $PATH so that the directory containing the newer versions was searched first, but I don't know how to do that and export just adds directories to the end of the path (which won't work). Then remove the version numbers from the executables such as gcc-9.2 to gcc and similar, though I'd be surprised if this does not cause problems.

I've looked at the answers in How to choose the default gcc and g++ version? but the first response requires sudo permissions (which I don't posses) and the second requires me to rm /usr/bin/gcc which again, I don't have permission to do.

TLDR: When I type gcc my computer uses one version of gcc, I need it to use a different version and I don't have permission to edit the directory where the old version is installed

  • 1
    "export just adds directories to the end of the path" no, it adds them wherever you tell it to ex. export PATH=/my/special/gcc/path:$PATH will add to the front Sep 19 '19 at 12:56
  • So it does. However after doing that and checking gcc --version I still get the old one, whether I change the filename 'gcc-9.2' to 'gcc' or not.
    – CT1234
    Sep 19 '19 at 13:04
  • gcc is typically just a symbolic link to the current version; if your local installation did not create such a link you would need to do it manually ex. ln -s gcc-9.2 path/to/gcc_9_2/bin/gcc Sep 19 '19 at 13:12

You could use alias command to override a command.

alias gcc="/home/username/pathtogcc/bin/gcc"

This is often used for built-ins like ls, e. g. when using it like

alias ls="ls -l"
  • 1
    This also has the advantage that you can simply run \gcc to execute the systemwide gcc installation.
    – danzel
    Sep 19 '19 at 13:16
  • ... it has the disadvantage that, by default, it will only apply when gcc is invoked directly in an interactive shell (not when it's being invoked from a Makefile or other build process for example) Sep 19 '19 at 13:32
  • Thanks that worked! However my cmake seems to completely ignore this and use the old version anyway, even if I give the location of the new compiler at the beginning of my PATH.
    – CT1234
    Sep 19 '19 at 14:12
  • 1
    @CT1234 that was exactly my point - for cmake you can set CMAKE_C_COMPILER and/or (for C++) CMAKE_CXX_COMPILER explicitly instead ex. cmake -DCMAKE_C_COMPILER="path/to/gcc-9.2/bin/gcc-9.2" ../ Sep 19 '19 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.