GCC Installation on Ubuntu
If you are a programmer, you probably know how important it is to have a C/C++ compiler installed on your computer. GCC is undoubtedly one of the most popular and widely-used compilers, as it supports various programming languages and platforms. In this article, we’ll explain how to install GCC on Ubuntu, a widely-used Linux distro. By the end of this article, you’ll be able to install multiple versions of GCC and G++, install build-essential package and verify the installation, and compile and run a basic C program.
To make things easier for you, we’ve gathered information from multiple sources. These sources include Linuxize, Learnubuntu, Linuxconfig, Linuxhint, and Geeksforgeeks. We’ll be referencing these sources throughout the article, so you can be assured that the information provided is accurate and reliable.
Method 1: Using the Terminal
The terminal is a powerful tool that enables you to execute commands and perform various tasks on your Ubuntu system. Using the terminal to install GCC on Ubuntu is a straightforward process, and it is the preferred method for many programmers. Here’s how to do it:
Step 1: Update the System
Before installing any software or package, it is always recommended to update the system. This ensures that you have the latest software versions and security patches installed. Open the terminal and type the following command:
sudo apt update
Step 2: Install GCC
Once the system is updated, you can proceed to install GCC. According to Linuxize, the easiest way to install GCC is to install the ‘build-essential’ meta-package, which contains the GCC compiler and other necessary utilities for compiling software. To install the ‘build-essential’ package, type the following command in the terminal:
sudo apt install build-essential
This command installs the latest version of GCC available in the Ubuntu repositories, as well as other necessary utilities.
Step 3: Verify the Installation
After installing GCC, you can verify the installation by checking the version number. Type the following command in the terminal:
gcc --version
This will display the version number of GCC installed on your system. If you see the version number, that means GCC is successfully installed on your Ubuntu system.
Installing Build-Essential Package
The ‘build-essential’ package contains a set of essential tools and utilities that are required for compiling software on Ubuntu. It includes GCC, G++, make, and dpkg-dev, among other packages. Here’s how to install the ‘build-essential’ package on your Ubuntu system:
Method 1: Using the Terminal
Step 1: Update the System
Before installing any package, it is always recommended to update the system. Open the terminal and type the following command:
sudo apt update
Step 2: Install build-essential package
After updating the system, you can install the ‘build-essential’ package by typing the following command in the terminal:
sudo apt install build-essential
This will install the latest version of the ‘build-essential’ package available in the Ubuntu repositories.
Step 3: Verify the Installation
You can verify the installation of the ‘build-essential’ package by checking the version number of GCC. Type the following command in the terminal:
gcc --version
This will display the version number of GCC installed on your system. If you see the version number, that means the ‘build-essential’ package is successfully installed on your Ubuntu system.
Method 2: Using Synaptic Package Manager
Step 1: Install Synaptic Package Manager
If you prefer a graphical user interface, you can use the Synaptic Package Manager to install the ‘build-essential’ package. To install Synaptic Package Manager, open the terminal and type the following command:
sudo apt install synaptic
Step 2: Search and select build-essential package for installation
After installing Synaptic Package Manager, launch it and search for the ‘build-essential’ package. Once you find it, select it for installation.
Step 3: Apply the changes and verify the installation
After selecting the ‘build-essential’ package for installation, click on the ‘Apply’ button to apply the changes. The package will be downloaded and installed on your system. You can verify the installation by checking the version number of GCC using the terminal.
Method 3: Using Ubuntu Software Centre
Step 1: Open Ubuntu Software Centre
If you are using Ubuntu Software Centre, you can easily install the ‘build-essential’ package from it. Open Ubuntu Software Centre and search for the ‘build-essential’ package.
Step 2: Search and select build-essential package for installation
Once you find the ‘build-essential’ package, select it for installation.
Step 3: Install build-essential package and verify the installation
After selecting the ‘build-essential’ package for installation, click on the ‘Install’ button to install the package. Once the installation is complete, you can verify the installation by checking the version number of GCC using the terminal.
Installing Multiple Versions of GCC and G++
Sometimes, you need to install multiple versions of GCC and G++ on your Ubuntu system, especially if you are working on projects that require a specific version of the compilers. Here’s how to install and switch between multiple versions of GCC and G++:
Step 1: Install Required GCC Versions
According to Linuxconfig.org, you can install multiple GCC versions by using the following command:
sudo apt install gcc-<version_number>
Replace <version_number>
with the version number of GCC that you want to install. For example, to install GCC version 7, you can use the following command:
sudo apt install gcc-7
You can install as many versions of GCC as you need using this method.
Step 2: Install Required G++ Versions
You can install multiple versions of G++ in the same way as GCC. Use the following command to install a specific version of G++:
sudo apt install g++-<version_number>
Replace <version_number>
with the version number of G++ that you want to install.
Step 3: Switch Between GCC and G++ Versions
According to linuxconfig.org, you can switch between multiple versions of GCC and G++ using the update-alternatives
command.
Step 1: List Available Versions of GCC
To list all available versions of GCC on your system, type the following command in the terminal:
update-alternatives --list gcc
This will display a list of all installed GCC versions.
Step 2: Select the Default Version of GCC
To select the default version of GCC, type the following command in the terminal:
sudo update-alternatives --config gcc
This will display a list of all available GCC versions installed on your system. Enter the number corresponding to the version you want to use as the default and press Enter.
Step 3: List Available Versions of G++
To list all available versions of G++ on your system, type the following command in the terminal:
update-alternatives --list g++
This will display a list of all installed G++ versions.
Step 4: Select the Default Version of G++
To select the default version of G++, type the following command in the terminal:
sudo update-alternatives --config g++
This will display a list of all available G++ versions installed on your system. Enter the number corresponding to the version you want to use as the default and press Enter.
Now you can switch between multiple versions of GCC and G++ whenever you need to.
Compiling and Running a Basic C Program
Now that you have installed GCC on your Ubuntu system, you can compile and run a basic C program. Here’s how to do it:
Step 1: Write a Basic C Program
Open a text editor and type the following code:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
This is a basic C program that outputs the string “Hello, World!” to the console.
Step 2: Save the Program
Save the program with a .c
extension, such as hello.c
.
Step 3: Compile the Program
To compile the program, open the terminal and navigate to the directory where the program is saved. Type the following command:
gcc -o hello hello.c
This command compiles the program and generates an executable file called hello
.
Step 4: Run the Program
To run the program, type the following command in the terminal:
./hello
This will execute the program, and you should see the output “Hello, World!” in the terminal.
Congratulations, you have successfully compiled and run a basic C program using GCC on your Ubuntu system!
That’s It!
You now know how to install GCC on Ubuntu, install multiple versions of GCC and G++, and compile and run a basic C program using GCC. With this knowledge, you can start developing your own C programs on your Ubuntu system.
We hope you found this guide helpful. If you have any questions or comments, please feel free to leave them below.
And don’t forget to check out our other great content for more helpful tips and tutorials!
Questions
Q. Who needs to install GCC on Ubuntu?
A. Anyone who wants to develop C programs on Ubuntu.
Q. What is GCC and why is it important?
A. GCC is the GNU Compiler Collection, a set of compilers and libraries used for various programming languages. It is important for developing C programs.
Q. How do I install GCC on Ubuntu?
A. Use the command “sudo apt install gcc” to install the GCC compiler on Ubuntu.
Q. What is the recommended package for compiling projects with GCC on Ubuntu?
A. The recommended package is “build-essential”, which includes GCC, G++, make, and dpkg-dev. Install it with the command “sudo apt install build-essential”.
Q. How can I install multiple versions of GCC on Ubuntu?
A. Use the command “sudo apt install gcc-
Q. What should I do if I encounter errors while installing GCC on Ubuntu?
A. Try updating your system and packages before attempting to install GCC. If you still encounter errors, consult the documentation or seek help from the Ubuntu community.