Upgrading gfortran on Ubuntu22 LTS

Hello,

I am using Ubuntu22 LTS on my PC and gfortran-11 for all my calculations. This version works very nicely for me, but I would also like to try newer versions (hopefully without compiling from source!) Looking at related threads (e.g. below), it seems that we can install newer versions by apt as follows:

     sudo add-apt-repository ppa:ubuntu-toolchain-r/test
     sudo apt update
     sudo apt install gfortran-13

Here, I am wondering if this method of installation might have some possible “interference” with existing Gfortran versions? For example, does the result / behavior of the older versions could potentially change because of the associated installation of newer libraries or config files? Or, is the installation completely separate (including libraries) and so there is essentially no worry about interference?

Another approach for upgrading Gfortran may be to upgrade the OS itself to Ubuntu 24 LTS (particularly with “in-place” upgrade). But here, I am also worried whether I can install older versions (for comparison and testing), e.g. gfortran-11 that I currently use. I have never tried an in-place OS upgrade, but only “fresh” installation up to now, so I am not sure whether I can keep old compiler environments.

(FWIW, I’ve been using Ubuntu22 also for workstations (!) and wondering if it is a reasonable choice… but I will ask this in a separate thread.)

If you have related experience, I would really appreciate any hints or information.
Thanks very much!

1 Like

You can build it yourself! See this website on how to get the gcc repo: GCC: Anonymous read-only Git access - GNU Project basically

git clone git://gcc.gnu.org/git/gcc.git my_gcc
cd my_gcc
git checkout releases/gcc-14.2.0
contrib/download_prerequisites
./configure --prefix=$HOME/install/compilers/gcc/14.2.0 --enable-languages=c,c++,fortran --enable-libgomp --enable-bootstrap --enable-shared --enable-threads=posix --with-tune=generic --disable-multilib
make -j install

Bootstrap with a lower version so if you’re building gcc/14.2.0 use say 11.4 … etc.

If you learn how to build it yourself you’ll stop using apt-get for a bit, since this allows you full control on what to use. You can load the version by creating an env file like:

export GCCVERSION=x.y.z
export GCC_ROOT=/shared/compilers/gcc/$GCCVERSION
export PATH=$GCC_ROOT/bin:$PATH
export LD_LIBRARY_PATH=$GCC_ROOT/lib64:$LD_LIBRARY_PATH
export LD_RUN_PATH=$GCC_ROOT/lib64:$LD_RUN_PATH
export CPATH=$GCC_ROOT/include:$CPATH
export INCLUDEPATH=$GCC_ROOT/include:$INCLUDEPATH
export CXX=$GCC_ROOT/bin/g++
export CC=$GCC_ROOT/bin/gcc
export FC=$GCC_ROOT/bin/gfortran

2 Likes

You could also try Homebrew for Linux, getting the newest version (14.2.0 at the moment), completely independent from the “system” repositories

1 Like

Yeah, I used to build GCC from source when I was younger, but recently I’ve got too much old used to installing from package managers and so become very lazy… (I even use the default apt version of MPI, though I compiled it manually before :sweat_smile:) So I was wondering about the simplest possible way (maybe apt or other package managers)…

Thanks very much for mentioning Homebrew for Linux. I once used it for trying some compilers, but have not fully explored its usage at that time (I remember I need to carefully set the PATH variable, so as not to mix commands installed with apt and brew). I will give it another try on Ubuntu22 also.

Me too. I remember spending hours watching GCC build, back when subversion was still a thing. :laughing:

Debian-based distros (like Ubuntu) use an “alternatives” system. So, you can have alongside installations for multiple versions of the same thing, like the PPA method you showed, and use the update-alternatives to pick the default.

I’m just not sure if that still works properly for GCC in Ubuntu (since they seem to be moving away from APT).

Docker might be another option…

1 Like

This is the method I always use and recommend on Ubuntu 22. I have gnu 11, 12 and 13 and never had problems. The default gcc/gfortran versions are the 11 and the newer versions having different names do not conflict. I’ve setup some bash functions to activate/deactivate aliasing to have “gcc”/“gfortran” to point to the desired one. If you install Miniforge you can also have gnu 14 from within the conda environement.

2 Likes

On my Linux Mint systems which are Ubuntu/Debian based, all new versions of gfortran I install using apt get a specific version name. ie gfortran-13 gets installed as /usr/bin/gfortran-13, gfortran-11 gets installed as /usr/bin/gfortran-11. I have never encountered any issues having both installed, ie no conflicts.

1 Like

I have GCC/G++/GFortran 11,12,13(built from sources) via update-alternatives. Ubuntu 22 and 24. No issues.

2 Likes

I’ve never had any issue with multiple versions of gfortran on the same system from 10.4 to 24.04.

Like rest of this thread, I’ve usually installed them from the ppa:ubuntu-toolchain-r/test.

I switch versions without issue using update-alternatives.

1 Like

Hi, I am sorry to be late for reply, and thank you very much for all the information!
I will try installing GCC >= 13 via PPA and test it with my codes (+ MPI) to check the results and timing. As for the update-alternatives command, I know only the name of it but never tried it up to now… (so I currently use the default GCC11 for all my production runs). I will report back if I hit some issues or more questions. Thanks!