How to Install GCC 11 on Ubuntu: A Comprehensive Guide for 2024
Are you a software developer looking to take advantage of the latest features and optimizations in the GNU Compiler Collection (GCC)? If so, you‘ve come to the right place. In this in-depth tutorial, we‘ll walk you through the process of installing GCC 11 on Ubuntu step-by-step.
But first, let‘s briefly discuss what GCC is and why you might want to upgrade to version 11. GCC is a popular open-source compiler for programming languages like C, C++, Objective-C, Fortran, and more. It‘s an essential tool for many software development projects.
GCC 11 is a major release that came out in 2021. It includes a variety of new features, improvements, and bug fixes over previous versions. Some highlights of GCC 11 include:
- Support for C++20 and C2x draft standards
- Improved optimization and vectorization capabilities
- Enhanced diagnostics and debugging support
- New compiler warnings to catch potential bugs
- Faster compile times in many cases
Of course, every new GCC release also aims to generate more efficient and robust code. Whether you‘re developing system-level software, embedded applications, or high-performance computing projects, GCC 11 can help you get the most out of your hardware.
Now, let‘s get into the installation process. The instructions below are up-to-date for Ubuntu 22.04 LTS in 2024, but should work for other recent Ubuntu versions as well.
Step 1: Add the Toolchain testing repository
First, we need to add the "Toolchain" personal package archive (PPA) to our system. This repo contains pre-built GCC packages for easy installation on Ubuntu. Open a terminal and run:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Press Enter when prompted to confirm adding the PPA. The toolchain repo should now be registered with your system.
Step 2: Update package lists
Next, it‘s a good idea to update our local package lists so Ubuntu is aware of the newly-added repository. Run:
sudo apt update
You should see the toolchain PPA mentioned in the output, indicating the repo was added successfully.
Step 3: Install GCC 11
We‘re now ready to install the gcc-11 package with:
sudo apt install gcc-11 g++-11
Confirm with Y and press Enter to proceed with the installation. The package manager will handle downloading and installing GCC 11 along with its dependencies.
Optionally, you can install additional GCC 11 packages for other languages and libraries (like Fortran and OpenMP) with:
sudo apt install gfortran-11 libgcc-11-dev libstdc++-11-dev libomp-11-dev
Feel free to add or remove packages from this list based on your specific needs.
Step 4: Verify the installation
Once the installation finishes, verify it by checking the GCC version:
gcc-11 –version
You should see output like:
gcc-11 (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
…
This confirms GCC 11 was installed correctly. If you also installed g++ for C++, you can check its version with:
g++-11 –version
Step 5: Test GCC 11 with a sample program
Let‘s compile and run a simple "Hello, world!" program in C to make sure everything is working. Create a file named test.c with the following contents:
int main() {
printf("Hello, world!\n");
return 0;
}
Save the file, then compile it with:
gcc-11 test.c -o test
Finally, run the compiled program:
./test
If you see the output "Hello, world!", congratulations – you‘ve successfully installed and tested GCC 11 on your Ubuntu system!
Switching Between GCC Versions
You may have noticed that we invoked the newly-installed compiler using gcc-11 instead of just gcc. This is because Ubuntu‘s alternatives system allows multiple versions of GCC to be installed simultaneously.
By default, gcc points to the "native" GCC version included with your Ubuntu release (like GCC 9.4 on Ubuntu 20.04). To check this, run:
gcc –version
To temporarily use GCC 11 for a single command, simply use the gcc-11 command as we did earlier. However, if you want to switch the default gcc command to always point to GCC 11, you can use:
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-11 11
This registers GCC 11 as a valid "alternative" for the gcc command with a priority of 11. You can then select it as the default with:
sudo update-alternatives –config gcc
Choose the entry for /usr/bin/gcc-11 from the list to make it the default.
To undo this change later and restore the original gcc version, simply re-run the alternatives –config command above and select the desired version.
Uninstalling GCC 11
If you decide you no longer need GCC 11 on your system, you can uninstall it by running:
sudo apt remove gcc-11 g++-11
Use sudo apt autoremove to clean up any leftover dependencies that were automatically installed and are no longer needed.
If you also added the toolchain testing repo and want to remove it, run:
sudo add-apt-repository –remove ppa:ubuntu-toolchain-r/test
This ensures your system won‘t try to install testing updates from the PPA in the future.
Compatibility Notes
In most cases, code that compiles with previous GCC versions should also work with GCC 11 without changes. However, there are a few potential gotchas to be aware of:
GCC 11 is stricter about some language standards compliance issues, so code that relied on non-standard behaviors or extensions may need updates.
Some command-line options and defaults have changed, which could affect your build scripts. Consult the GCC 11 release notes for details.
If you use third-party libraries, they may need to be rebuilt with GCC 11 to ensure binary compatibility.
It‘s always a good idea to thoroughly test your software when upgrading compilers. If you run into issues, the GCC mailing lists and bug tracker are great places to seek help from the community.
Conclusion and Further Resources
In this guide, we covered how to install, use, and uninstall GCC 11 on Ubuntu Linux. We also discussed key features of this release and potential compatibility concerns to keep in mind.
Installing a newer GCC version can be a great way to improve the performance, security, and maintainability of your C/C++ projects. You can find the complete GCC 11 release notes and documentation on the official GCC web site:
The GCC Wiki also has a wealth of additional information, tips, and FAQs contributed by the developer community over the years:
I hope this guide has been helpful in getting you up and running with GCC 11 on Ubuntu. Happy coding!