19

I successfully used the instructions from https://linuxize.com/post/how-to-install-gcc-compiler-on-ubuntu-18-04/ to upgrade gcc version before, but they don't work for gcc-14. How do I install gcc-14 and g++-14 on Ubuntu 22.04 and 24.04?

Paul Jurczak
  • 1,237

5 Answers5

18

I am not yet brave enough to leave Ubuntu 22.04 LTS but needed g++14. The sudo apt-get gcc-14 did not work for me, as it installed clang++14 for some reason (perhaps a misconfiguration on my part). What did work for me was following the instructions I found at https://www.dedicatedcore.com/blog/install-gcc-compiler-ubuntu/

The steps I took:

sudo apt install build-essential
sudo apt install libmpfr-dev libgmp3-dev libmpc-dev -y
wget http://ftp.gnu.org/gnu/gcc/gcc-14.1.0/gcc-14.1.0.tar.gz
tar -xf gcc-14.1.0.tar.gz
cd gcc-14.1.0
./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-14.1.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-14.1.0
make
sudo make install

And if you would like to make it the default...

sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/gcc-14.1.0/bin/g++14.1.0 14
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-14.1.0/bin/gcc14.1.0 14

after that, g++ showed I was running version 14.1.0. I was then able to compile my project that included some c++20/23 features that were not in the previous versions of g++ (chrono/format).

John Jones
  • 181
  • 7
14

GCC-14 (and G++-14) is available in the Universe repository for Ubuntu 24.04, as evident in the Ubuntu Package archive.

It is equally evident that this package is not available for Ubuntu 22.04, so installing this on 22.04 will require some third-party interference, or you have to compile it yourself.

See here on how to enable the Universe repositories.

Artur Meinild
  • 31,035
1

gcc-14 is not available in Ubuntu 22.04 apt repos, but they are available in conda repositories. mamba, which is a C++ replacement for conda, is quite fast like apt and easy to work with.

# assumption: mamba is available, and an environment (e.g. base) is activate
$ mamba install conda-forge::gcc=14.1.0 conda-forge::gxx=14.1.0

verify

$ which gcc g++ ~/mambaforge/bin/gcc ~/mambaforge/bin/g++

See other versions of gcc/g++ available for your platform:

To install mamba,

name=Miniforge3-$(uname)-$(uname -m).sh
wget "https://github.com/conda-forge/miniforge/releases/latest/download/$name"
bash $name -b -p ~/mambaforge
~/mambaforge/bin/mamba init bash
rm $name

Or, follow the instructions in README @ https://github.com/conda-forge/miniforge?tab=readme-ov-file#install


I have been using conda/mamba as another way of installing packages. I know some of you would be appalled to hear "install gcc via conda". So, here is a longer explanation why this is not a crazy idea: conda/mamba is not just for python libs! These can install system libs and it works across different OS (Windows/Linux/OSX) as well as with architectures (x86/x86_64/aarch64). Building gcc from source seemed more work than I was willing to put in, and even after building I have to take care of exporting some environment variables (PATH, LD_LIBRARY_PATH etc which I am trying to avoid). conda/mamba isolates libs in such a way that the broken/incompatible packages will not break the operating system functionality. The bad thing though, we have less trust on who compiled the packages and whether or not there were any unwanted tweaks to the code.

TG Gowda
  • 671
1

Just use your mouse and click gcc-14.2.0. Copy the thing into your Ubuntu and untar it there.

daparic
  • 358
0

With a bit of ingenuity connecting up CMake and Docker, one can actually compile with gcc14 on Ubuntu 22.04 (or any Linux that doesn't support gcc14) and have the entire setup be in your project's source.

The setup has a docker-compose.yml:

services:
  gcc:
    build:
        context: .
        dockerfile: Dockerfile-gcc

that builds a custom docker image from a simple Dockerfile, named Dockerfile-gcc, based on the official GCC docker image:

FROM gcc:latest
RUN (ln -s /usr/local/bin/* /usr/bin || true) > /dev/null 2> /dev/null

Note the symbolic link creation with hiding of any errors (because there are some unimportant ones). The official GCC image has g++14 in /usr/local/bin but Ubuntu puts it in /usr/bin and CMake requires a full path to a compiler it can find. This means the docker image must have the compiler executable in the same location as Ubuntu (yes, this setup lies to CMake, get over it).

To actually have CMake run the gcc in the Docker image, take advantage of CMake's Compiler launcher settings (as this appears in my CMakePresets.json file under "cacheVariables"):

"CMAKE_CXX_COMPILER_LAUNCHER": "/usr/bin/docker;compose;run;--rm;-v;\"$PWD/../..\":\"$PWD/../..\";-w;\"$PWD\";gcc"

Mounting "$PWD/../.." is because of the other build settings in my setup that places the CMake working build directory under build/debug or build/release and the root source directory must be visible to the compiler. Your mount point may be different.

CMake's compiler launcher capability really targets things like ccache but, hey, using docker to launch a compiler seems like a reasonable use too :-)

But, I'm sure you are wondering (as was I),

What about the link stage? You could use CMAKE_CXX_LINKER_LAUNCHER. You are not using the gcc14 linker.

You are correct and get extra points for knowing about the linker launcher, but, so far for me at least, regular Ubuntu-gcc can link gcc14-built object files. Plus, the official gcc docker file is based on some Debian base so linking to whatever system libraries are in the docker image scares me more than using the linker on Ubuntu.

Caveat

libstdc++ has changed in g++14, in particular, there is a __cxa_call_terminate@@CXXABI_1.3.15 that is now exported and used. You need to copy /usr/local/lib64/libstdc++.so from the official image and deploy with your executable or shared library (and set RPATH so that shared library gets found)

mheyman
  • 129