How to Install GCC on Ubuntu 21.10
GCC, the GNU Compiler Collection is a compiler system developed to support various programming languages. GCC is a standard compiler for most projects related to GNU and Linux, including the Linux kernel.
Installing GCC on Ubuntu
Ubuntu repositories contain a build-essential
package which contains the GCC compiler, g++, and make.
Update your existing system by running the following command,
apt upgrade
apt install build-essential
Output:
root@server:~# apt install build-essential
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
dpkg-dev fakeroot g++ g++-11 gcc gcc-11 libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan6
libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev libfakeroot libgcc-11-dev libgomp1 libitm1 liblsan0
libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev libtsan0 libubsan1 linux-libc-dev lto-disabled-list make manpages-dev
rpcsvc-proto
Suggested packages:
debian-keyring g++-multilib g++-11-multilib gcc-11-doc gcc-multilib autoconf automake libtool flex bison gdb gcc-doc
gcc-11-multilib gcc-11-locales glibc-doc libstdc++-11-doc make-doc
The following NEW packages will be installed:
build-essential dpkg-dev fakeroot g++ g++-11 gcc gcc-11 libalgorithm-diff-perl libalgorithm-diff-xs-perl
libalgorithm-merge-perl libasan6 libatomic1 libc-dev-bin libc-devtools libc6-dev libcc1-0 libcrypt-dev libfakeroot
libgcc-11-dev libgomp1 libitm1 liblsan0 libnsl-dev libquadmath0 libstdc++-11-dev libtirpc-dev libtsan0 libubsan1
linux-libc-dev lto-disabled-list make manpages-dev rpcsvc-proto
If you want to install the manual page for GCC, run the below command,
apt-get install manpages-dev
After installing, to verify that GCC is successfully installed by checking the gcc version,
gcc --version
Output:
root@server:~# gcc --version
gcc (Ubuntu 11.2.0-5ubuntu1) 11.2.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.
Compiling a Hello World Example
Create a basic C code source, eg: let's create hello world in C program and open hello.c file,
nano hello.c
Add the following code to hello.c file.
// hello.c
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
Compile it into an executable and execute the hello program by running the following commands,
gcc hello.c -o hello
./hello
Output:
root@server:~# ./hello
Hello, world!
root@server:~#
Installing Multiple GCC Versions
Install and use multiple versions of GCC on Ubuntu 21.10,
Install the desired GCC and G++ versions by typing below command,
apt install gcc-8 g++-8 gcc-9 g++-9 gcc-10 g++-10
Now you have multiple versions of the GCC compiler installed, there’s a priority number that gets associated with each. The version with the highest priority will be the default system compiler.
You will have to run the commands below to set the default system compiler associating each with a priority.
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8
Later if you want to change the default version use the update-alternatives command
update-alternatives --config gcc
Output:
root@server:~# update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-10 100 auto mode
1 /usr/bin/gcc-10 100 manual mode
2 /usr/bin/gcc-8 80 manual mode
3 /usr/bin/gcc-9 90 manual mode
Press <enter> to keep the current choice[*], or type selection number:
You will be presented with a list of all installed GCC versions on your Ubuntu system, Select your choice and press Enter.
Done.