GCC issues with Ubuntu 22.04 and mainline kernel

The Ubuntu Kernel team has started compiling the mainline kernel with gcc-13 as of versions 6.4.4 and 6.5-rc3. I don’t know what’s driving this change—i.e. if it’s coming from upstream or if it was an internal decision—or if they plan to revisit it. If I have time tomorrow I’ll go bug them on IRC. At any rate, if you install a newer mainline kernel on Ubuntu 22.04 you can potentially run into some snags when the dpkg post-install scripts kick off dkms. Ubuntu 22.04 uses gcc-11 by default—i.e. if you install the build-essential, dkms, or gcc package(s)—so it may not be able to compile modules against a kernel compiled with gcc-13. (Earlier versions of Ubuntu are probably impacted as well, but at this point it’s safe and recommended to upgrade to 22.04.2 LTS.)

I was able to resolve most of my issues by setting up alternatives for gcc-11 and gcc-12 and giving gcc-12 priority over gcc-11. Of course, this may break other things that will compile with gcc-11 but won’t compile with gcc-12, so caveat emptor. I wasn’t able to find a quick and easy solution for getting gcc-13 installed and working but that would be the logical next step.

Here are the steps to perform these actions on your machine:

# install gcc-12
sudo apt install gcc-12 g++-12

# create a low-priority alternative for /usr/bin/gcc, /usr/bin/g++, and /usr/bin/gcov using gcc-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11

# create a high-priority alternative for /usr/bin/gcc, /usr/bin/g++, and /usr/bin/gcov using gcc-12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12

# make sure you didn't goof anything up, should say version 12.1.0
gcc --version

# recompile any broken/missing dkms modules with gcc-12
ls -1 /boot/initrd.img-* | cut -d- -f2- | sudo xargs -n1 /usr/lib/dkms/dkms_autoinstaller start

References:

  1. https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.4.4/amd64/log
  2. https://kernel.ubuntu.com/~kernel-ppa/mainline/v6.5-rc3/amd64/log
  3. update-alternatives for gcc on Ubuntu · GitHub
  4. Command to rebuild all DKMS modules for all installed kernels? - Ask Ubuntu

Hope this helps someone. As always please let me know if you have any questions or corrections. Cheers!

Mixing compilers have never been supported “anywhere” and usually you should stick with whatever upstream uses (indirectly supports). If you looking for bleeding edge you’re likely better off looking at Arch or Alpine.

I’m not mixing compilers, I’m installing a mainline kernel in Ubuntu LTS. This is a fairly common practice and the kernels come from an “official” repository maintained by the Ubuntu kernel developers. The ability to easily run a newer kernel in an older distribution is a feature, not a bug.

The Ubuntu kernel developers have switched compilers, causing issues, and I have documented a resolution. If you’d like to chastise someone for mixing compilers, you can start here.

1 Like

I was running the 6.5-rc2 kernel previously for a couple of weeks but I made a big mistake and accidentally deleted it when I was cleaning out older kernels.

Then when I went back to reinstall the 6.5-rc kernel I had installed before, I discovered that between the time I first installed that kernel and my attempt to reinstall it, the kernel devs had gone back and recompiled the 6.5-rc2 kernel branch with the gcc-13 compiler. Now all kernels past 6.4.3 have been recompiled with gcc-13.

Now I am stuck with the default HWE Ubuntu 22.04.3 LTS 6.2 kernel.

Can you clarify whether you were able to install any of the 6.5-rcX kernels with the update-alternatives commands?

I have both gcc-11 and gcc-12 already installed in my Ubuntu 22.04.3 LTS distro.

Does making gcc first look for and report gcc version as -12 allow the 6.5 kernels to install?

OK, I found out the solution for installing gcc-13 to Ubuntu 22.04.3 LTS. Running the 6.5-rc5 kernel now on my 7950X.

https://phoenixnap.com/kb/install-gcc-ubuntu

Yes, I’ve been able to install a variety of 6.4 and 6.5-rc mainline kernels that were built using gcc-13 in Ubuntu 22.04 using this solution.

I followed the link instructions and set my update-alternative to gcc-13 and now can install the 6.5-rc kernels with no issues.

I don’t know why you went and found other instructions to follow when I included them in my original post above, but regardless I’m glad it’s working for you.

My distro doesn’t have gcc-13 so your instructions won’t work. Had to add the gcc ppa to get access to gcc-13.

Then after installing gcc-13 I could run the update-alternatives commands.