- How To Install The Latest GCC On Ubuntu
- 1. Check The Current GCC Version.
- 2. Install GCC On Ubuntu Steps.
- How to Install GCC Compiler (build-essential) on Ubuntu 20.04
- Choose a different version or distribution
- Introduction
- GCC Compiler Features
- What does the build-essential Package do?
- Prerequisites to Install GCC Compiler
- Step 1 – Install GCC Compiler on Ubuntu
- Step 2 – Compiling a Hello World Example
- Step 3 – Install Multiple GCC Versions
- FAQs to Install GCC Compiler on Ubuntu
- Where is GCC compiler in Ubuntu?
- How to verify if GCC is installed on my PC?
- Conclusion
How To Install The Latest GCC On Ubuntu
GCC is the abbreviation of GNU Compiler Collection. It contains a set of compilers that can compile C, C++, Objective-C, etc language-coded programs. This article will tell you how to install the latest version of GCC on Linux OS, especially Ubuntu.
1. Check The Current GCC Version.
- Run the command gcc -version in a terminal, if you get the below output, it means GCC is not installed.
$ gcc --version Command 'gcc' not found, but can be installed with: sudo apt install gcc
2. Install GCC On Ubuntu Steps.
$ sudo apt update [sudo] password for jerry: Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:2 http://cn.archive.ubuntu.com/ubuntu focal InRelease Get:3 http://cn.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB] Get:4 http://cn.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB] Get:5 http://cn.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [581 kB] Get:6 http://cn.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,445 kB] Get:7 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [892 kB] Get:8 http://cn.archive.ubuntu.com/ubuntu focal-updates/universe i386 Packages [662 kB] Fetched 3,915 kB in 19s (205 kB/s) Reading package lists. Done Building dependency tree Reading state information. Done 191 packages can be upgraded. Run 'apt list --upgradable' to see them.
$ sudo apt install build-essential Reading package lists. Done Building dependency tree . Processing triggers for man-db (2.9.1-1) . Processing triggers for libc-bin (2.31-0ubuntu9.2) .
$ gcc --version gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Copyright (C) 2019 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.
$ whereis gcc make gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/gcc /usr/share/man/man1/gcc.1.gz make: /usr/bin/make /usr/share/man/man1/make.1.gz
$ make -v GNU Make 4.2.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2016 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
How to Install GCC Compiler (build-essential) on Ubuntu 20.04
Install GCC Compiler on Ubuntu 20.04 with this step-by-step tutorial. GCC is mainly a collection of compilers as well as of libraries for C, C++, and Objective-C.
Choose a different version or distribution
Introduction
Before we begin talking about how to install GCC Compiler on Ubuntu 20.04. Let’s briefly understand – What is a GCC Compiler?
The GNU Compiler Collection is mainly a collection of compilers as well as of libraries for C, C++, and Objective-C. There are many open-source projects, like GNU tools and the Linux kernel, that are compiled with GCC.
In this tutorial, you will install the GCC Compiler on Ubuntu 20.04.
GCC Compiler Features
- Cross-platform support: GCC supports a wide range of operating systems, including Linux, macOS, Windows, and others. This allows developers to write code for one platform and compile it for another.
- Optimization: GCC provides a variety of optimization options that help developers to generate highly optimized code for their target platform. These options can significantly improve the performance of the compiled code.
- Language support: GCC supports a variety of programming languages, including C, C++, Objective-C, Fortran, Ada, and others. This makes it a versatile tool for developers who work with different programming languages.
- Standard compliance: GCC complies with various industry standards, such as the C11 and C++17 standards. This ensures that the code generated by GCC is compatible with other compilers that also comply with these standards.
- Open-source: GCC is an open-source tool, which means that developers can access the source code, modify it, and redistribute it as per their requirements.
- Debugging support: GCC provides a range of debugging options, such as generating debug symbols, supporting breakpoints, and generating core dumps. These options help developers to easily debug their code.
- Extensibility: GCC can be extended using plugins and custom front-ends, allowing developers to customize the compiler to meet their specific requirements.
What does the build-essential Package do?
The build-essential meta-package is a package available on Ubuntu that includes five separate packages essential during the software compilation process. It also contains the GCC compiler, which is required to build and run C programs. The five packages included in the build-essential package are as follows:
- gcc: The GNU Compiler Collection, which is used to compile programs written in C language.
- g++: The GNU C++ compiler, which is used to compile programs written in C++ language.
- libc6-dev: This package includes the GNU library files needed to compile simple C and C++ programs, along with header files.
- make: This is a useful tool for controlling the compilation of a program. It reads a file called a «makefile» to instruct the compiler on how to complete its job.
- dpkg-dev: This package can be used for unzipping, compiling, and uploading Debian source packages. If you plan to package your program for a Debian-based system, this tool will be useful.
Prerequisites to Install GCC Compiler
1) You must log in as root or user with the sudo privileges.
Step 1 – Install GCC Compiler on Ubuntu
1) Here, the default Ubuntu repositories have a meta-package as build-essential . It contains the GCC compiler and many libraries and utilities necessary for compiling the software. Perform the below steps to install GCC Compiler on Ubuntu 20.04.
2) Firstly, start by updating the packages list:
3) After that, install build-essential package by typing:
sudo apt install build-essential
The command will install a bunch of new packages. It will also include gcc , g++ and make . You may also need to install the manual pages. It is about using GNU/Linux for the development:
sudo apt-get install manpages-dev
4) To validate if the installation of the GCC compiler is successful, you will use gcc —version command. It will print the GCC version:
5) 9.3.0 is a default version of GCC available in Ubuntu 20.04 repositories.
Output gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0 Copyright (C) 2019 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.
GCC compiler installation on your system is now successful. You can now start using it.
Step 2 – Compiling a Hello World Example
1) To Compile a basic C or C++ program, you will do it using GCC. Open your text editor and create the below file:
2) Proceed to save the file. Then, compile it into an executable using the below command:
3) It will create a binary file with a name hello in the same directory. It is where you run the command. Now, execute the hello program, by:
Step 3 – Install Multiple GCC Versions
1) Here, you will now install and use multiple versions of GCC on Ubuntu 20.04. The latest versions of the GCC compiler have new functions and optimization improvements.
Further, the default Ubuntu repositories have several GCC versions. It is from 7.x.x to 10.x.x . Now, you will install the latest three versions of GCC and G++.
2) Therefore, first, add ubuntu-toolchain-r/test PPA to your system using the following command:
sudo apt install software-properties-common sudo add-apt-repository ppa:ubuntu-toolchain-r/test
3) Next, install the necessary GCC and G++ versions by:
sudo apt install gcc-8 g++-8 gcc-9 g++-9 gcc-10 g++-10
4) Then, the command below will configure an alternative for each version and associate a priority with it. The default version is the one with the highest priority. So, here is the gcc-9 :
sudo 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 sudo 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 sudo 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
5) Later, if you will need to change the default version, you can do it using the update-alternatives command:
sudo update-alternatives --config gcc
Output 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 to keep the current choice[*], or type selection number:
6) You will see a list of all installed GCC compiler versions on your Ubuntu system. Then, enter the number of versions you want to use as a default. Proceed to press Enter . The command will create symbolic links which will be to the specific versions of GCC and G++.
FAQs to Install GCC Compiler on Ubuntu
Where is GCC compiler in Ubuntu?
You need to use the which command to locate c compiler binary called gcc. Usually, it is installed in /usr/bin directory.
To find the location of the Linux C Compiler, launch a terminal and then type the following commands:
which g++
which cc
which gcc
How to verify if GCC is installed on my PC?
You can use the following command to verify if GCC is installed on your PC:
If GCC is installed on your computer, a variety of data should be shown, beginning with the phrase Using built-in specs which indicates that GCC is installed on your PC.
Conclusion
We hope this detailed tutorial helped you to install the GCC Compiler on Ubuntu 20.04.
If you have any queries or doubts, please leave them in the comment below. We’ll be happy to address them.