Before compiling code, you need to know how to check GCC version Linux has installed on your system. This is a basic but crucial step for any developer working in a Linux environment. Whether you are troubleshooting a build error or ensuring compatibility with your project, knowing your GCC version helps you avoid headaches later.
In this guide, we will walk through multiple ways to find the GCC version on Linux. We will cover the command line, package manager queries, and even how to check for multiple installed versions. By the end, you will be able to identify your GCC version quickly and confidently.
Why Checking The Gcc Version Matters
GCC, the GNU Compiler Collection, is a core tool for compiling C, C++, and other languages. Different versions support different language standards and optimizations. For example, GCC 4.8 added full C++11 support, while GCC 10 brought C++20 features. If you try to compile code written for a newer standard with an older compiler, you will get errors.
Also, some Linux distributions ship with older GCC versions for stability. Knowing your version helps you decide if you need to upgrade or install an additional compiler. It is a simple check that saves time during development.
How To Check Gcc Version Linux
The most direct way to check your GCC version is using the --version flag. Open your terminal and type the following command:
gcc --version
This will output something like:
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 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.
The first line shows the version number. In this case, it is 11.4.0. The output also includes distribution-specific information, like the Ubuntu package version.
Using The Short Version Flag
If you want only the version number without the extra text, use the -dumpversion option:
gcc -dumpversion
This prints just the version number, like 11 or 11.4. It is useful for scripting or when you need a clean output.
Checking The Full Version With Gcc -V
For more detailed information, use the -v (verbose) flag:
gcc -v
This shows the compiler version along with configuration details, including the target architecture, threading model, and search paths. It is helpful when you need to know how GCC was built.
What If Gcc Is Not Installed?
If you run gcc --version and get a “command not found” error, GCC is not installed on your system. You can install it using your package manager. For Debian-based systems like Ubuntu:
sudo apt update
sudo apt install gcc
For Red Hat-based systems like Fedora or CentOS:
sudo dnf install gcc
Or for older systems using yum:
sudo yum install gcc
After installation, verify the version again with gcc --version.
Checking Gcc Version Via Package Manager
You can also check the installed GCC version using your package manager. This is useful if you want to see the exact package version or if you have multiple GCC versions installed.
On Debian/Ubuntu Systems
Use dpkg to query the installed package:
dpkg -l | grep gcc
This lists all packages containing “gcc” in their name. Look for the main GCC package, like gcc or gcc-11. The version number appears in the output.
Alternatively, use apt:
apt list --installed 2>/dev/null | grep gcc
On Red Hat/Fedora Systems
Use rpm to query the installed package:
rpm -qa | grep gcc
This shows all installed RPM packages with “gcc” in the name. The version is part of the package name, like gcc-11.4.1-2.fc37.x86_64.
On Arch Linux
Use pacman:
pacman -Q gcc
This displays the installed version of the GCC package.
Checking For Multiple Gcc Versions
Sometimes you have multiple GCC versions installed on the same system. This is common when you need to compile code for different standards or compatibility. You can check all installed versions using the update-alternatives system on Debian-based distributions.
List All Installed Gcc Versions
Run the following command to see all GCC versions available:
ls /usr/bin/gcc*
This lists all executables starting with gcc in the /usr/bin directory. You might see gcc, gcc-9, gcc-11, etc.
Using Update-Alternatives
If you have multiple versions configured with update-alternatives, you can list them:
update-alternatives --list gcc
This shows the paths to all configured GCC versions. To switch between them, use:
sudo update-alternatives --config gcc
Then select the desired version from the list.
Checking Gcc Version In Scripts
If you are writing a script that needs to check the GCC version, you can use the -dumpversion or -dumpfullversion options. For example, in a bash script:
#!/bin/bash
GCC_VERSION=$(gcc -dumpversion)
echo "GCC version is $GCC_VERSION"
This captures the version number into a variable. You can then compare it using standard shell operators.
For more precise version comparison, consider using gcc -dumpfullversion, which outputs the full version string like 11.4.0 instead of just 11.
Checking Gcc Version For C++ (G++)
If you use the C++ compiler, the command is similar. Just replace gcc with g++:
g++ --version
Or for a short version:
g++ -dumpversion
The output format is identical to GCC. This is useful when you are specifically working with C++ code.
Common Errors And Troubleshooting
Sometimes you might get unexpected results when checking the GCC version. Here are a few common issues:
“Command Not Found” Error
If you see this, GCC is not installed or not in your PATH. Install it using your package manager as shown above. Also, check if you are using the correct command: gcc for C, g++ for C++.
Wrong Version Displayed
If the version shown is older than expected, you might have multiple versions installed and the default is not the latest. Use update-alternatives to switch or specify the full path, like /usr/bin/gcc-11.
Symbolic Link Issues
Sometimes the gcc command is a symbolic link to a specific version. Check the link with:
ls -l /usr/bin/gcc
This shows which actual executable it points to. You can change the link manually if needed.
Understanding The Gcc Version Number
GCC version numbers follow a pattern: major.minor.patch. For example, version 11.4.0 means major version 11, minor version 4, and patch level 0. Major versions introduce new features and language standards. Minor versions add improvements and bug fixes. Patch versions are for critical fixes.
Knowing this helps you understand what features are available. For instance, GCC 10 introduced the -fanalyzer option for static analysis, while GCC 12 added better support for the C++23 standard.
Checking Gcc Version On Different Linux Distributions
The method to check GCC version is the same across distributions, but the default version may vary. Here are some common examples:
- Ubuntu 22.04 LTS: Default GCC is 11.4.0
- Fedora 37: Default GCC is 12.2.1
- Debian 11: Default GCC is 10.2.1
- Arch Linux: Default GCC is usually the latest stable, like 13.2.1
- CentOS 7: Default GCC is 4.8.5 (older but stable)
You can always install a different version using your package manager. For example, on Ubuntu, you can install GCC 12 with:
sudo apt install gcc-12
Then use gcc-12 to compile with that version.
Using Gcc Version In Makefiles
When writing Makefiles, you might need to check the GCC version for conditional compilation. You can use the shell function:
GCC_VERSION := $(shell gcc -dumpversion)
ifeq ($(GCC_VERSION),11)
CFLAGS += -std=c++20
endif
This adds the C++20 standard flag only if GCC version is 11 or higher. You can also use gcc -dumpfullversion for more precise checks.
Checking Gcc Version Without Running Gcc
If you cannot run GCC directly (for example, in a restricted environment), you can check the version from the package database. On Debian systems, you can look at the package info:
apt-cache policy gcc
This shows the installed version and available versions from repositories. On RPM-based systems:
rpm -qi gcc
This displays detailed information about the installed package, including the version.
Frequently Asked Questions
How Do I Check The GCC Version On Linux Without Typing The Full Command?
You can use gcc -dumpversion for a short output. Or create an alias like alias gccver='gcc --version' in your shell configuration file.
What Is The Difference Between Gcc –Version And Gcc -V?
gcc --version shows the version and copyright information. gcc -v shows the version along with detailed configuration options and search paths.
Can I Check The GCC Version For A Specific Installed Version?
Yes, if you have multiple versions, use the full path like /usr/bin/gcc-11 --version or gcc-11 --version if it is in your PATH.
How Do I Check The GCC Version On A Remote Server Via SSH?
SSH into the server and run gcc --version. If GCC is not installed, you will get a “command not found” error.
What If My System Has No GCC Installed?
Install it using your package manager. For Ubuntu/Debian: sudo apt install gcc. For Fedora: sudo dnf install gcc. For CentOS: sudo yum install gcc.
Conclusion
Knowing how to check GCC version Linux is a simple but essential skill. You now have multiple methods: using gcc --version, gcc -dumpversion, package manager queries, and checking for multiple versions. Each method has its use case, from quick checks to scripting and troubleshooting.
Remember to verify your GCC version before compiling any project. It ensures compatibility and saves you from cryptic error messages. If you need a newer version, most distributions allow you to install it easily. Keep this guide handy for your next development session.