If you're a developer working on a Debian-based Linux distribution (e.g. Ubuntu), sometimes you need to know all available versions of gcc or g++ before installing or switching compilers. This is especially useful when dealing with projects that require specific compiler versions, or when testing code across multiple versions. This tutorial demonstrates how to get available gcc or g++ versions using APT.
Before querying available packages, make sure your package list is up-to-date:
sudo apt update
gcc compiler
We can use apt-cache search
combined with grep
and sort
to extract all available gcc compiler versions:
apt-cache search '^gcc-[0-9.]+$' | grep -Po 'gcc-[0-9.]+' | sort -V
Explanation:
apt-cache search '^gcc-[0-9.]+$'
- searches for all packages starting withgcc-
followed by a version number.grep -Po 'gcc-[0-9.]+'
- extracts only the package names, likegcc-9
,gcc-10
, etc.sort -V
- sorts the versions numerically (sogcc-10
comes aftergcc-9
).
Example output:
gcc-9
gcc-10
gcc-11
gcc-12
gcc-13
gcc-14
g++ compiler
Similarly, for g++ compiler, we can use:
apt-cache search '^g\+\+-[0-9.]+$' | grep -Po 'g\+\+-[0-9.]+' | sort -V
Explanation:
- The
\+\+
is escaped because+
is a special character in regular expression. - The rest works the same way as for gcc.
Example output:
g++-9
g++-10
g++-11
g++-12
g++-13
g++-14
Leave a Comment
Cancel reply