If you follow my Twitter, you already know that I started a series of videos on YouTube (https://auladec.nilo.pro.br/) about the C language for those who already know Python. If you want to learn Python, don’t forget to visit the site of my book for more information (https://python.nilo.pro.br).

In these videos, I ask everyone to use the GCC version 13.2.0 that is available from Ubuntu 23.10. Ubuntu 24.04 LTS should be released by the end of the month, but it may be delayed and the question remains: how can we update the GCC on Ubuntu 22.04?

I found two alternatives:

  1. Add a testing PPA and install GCC 13.1 via apt.
  2. Compile directly from source, which is more time-consuming, but brings the version we want.

To add the PPA (Personal Package Archives), type:

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

After that, the compiler can be called with:

gcc-13 --version

Which should return something like:

$ gcc-13 --version
gcc-13 (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Since we added a testing PPA, I recommend removing it at the end of the process:

sudo add-apt-repository -r ppa:ubuntu-toolchain-r/test

But note that the most recent version of gcc in the PPA is 13.1 and not 13.2.

The other alternative requires a bit more work, but the result is better. Note: The compilation took more than 50 minutes (it also requires 8 GB of free disk space) on my virtual machine, so be warned :-D

These steps were found in the article: https://www.dedicatedcore.com/blog/install-gcc-compiler-ubuntu/ which you can read for access to more details.

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

The line make -j3 can be adapted to a higher number if you have multiple processors. Note that we used sudo in several parts, which means you need to be the root of the system to install new packages.

Finally, test the new compiler with:

/usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version

Which should result in:

$ /usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version
gcc-13.2.0 (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

To make it easier to use, type:

sudo ln -s /usr/local/gcc-13.2.0/bin/gcc-13.2.0 /usr/bin/gcc-13.2.0

which creates a link to /usr/bin, so you can call the compiler with:

$ gcc-13.2.0 --version
gcc-13.2.0 (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

If you don’t know my C course site yet, visit https://auladec.nilo.pro.br/. You will find links to each lesson, but also to the YouTube channel and other sites of mine.