How to Install GCC on Linux Mint 22, 21 or 20

GCC (GNU Compiler Collection) is a powerful and widely-used compiler that supports various programming languages, including C, C++, and Fortran. It is an essential tool for developers, offering features such as optimization capabilities, support for multiple architectures, and extensive diagnostic messages to aid in debugging. GCC is known for its robustness and versatility, making it a critical component for software development on Linux systems.

To install GCC on Linux Mint 22, 21, or 20, you have two primary methods. The first method utilizes the Linux Mint default repository, which provides a stable and straightforward installation. The second method involves using the Ubuntu Toolchain PPA, which offers access to the latest stable builds of GCC versions 14, 13, 12, 11, 10, and 9, along with tips on how to switch between these versions.

Method 1: Install GCC via the Linux Mint Default Repository

This section outlines the steps for installing GCC using the default repository provided by Linux Mint. Utilizing the default repository is recommended to ensure compatibility with your system’s packages.

Update Linux Mint Packages

Updating your system is a crucial preliminary step. This ensures that all existing packages are up-to-date, minimizing the likelihood of package conflicts during the installation of GCC.

sudo apt update && sudo apt upgrade

Install GCC from the Linux Mint Repository

After updating the system, the next step is to install GCC. There are two options available:

  1. Install just the GCC package.
  2. Install the build-essential package, which encompasses GCC and various development tools such as make, g++, and dpkg-dev.

The build-essential package is handy if you plan to do development in C or C++, as it includes additional libraries and tools that are often required.

To install GCC alone on Linux Mint:

sudo apt install gcc

Alternatively, to install the build-essential package:

sudo apt install build-essential

Verify GCC Installation on Linux Mint

After installation, it is prudent to verify that GCC has been successfully installed and is accessible from the command line. Moreover, checking the version of GCC will provide insights into the features and capabilities available to you, as different versions of GCC support different language standards and optimizations.

To verify the installation and check the version of GCC:

gcc --version

This command will output the version of GCC installed, confirming that the installation process was successful.

Method 2: Install GCC Using Ubuntu Toolchain PPA

This section elucidates the steps for installing the latest or alternative versions of GCC Compiler using Ubuntu Toolchain PPA on Linux Mint. This method is suitable if you are looking for a specific version of GCC that is not available in the default Linux Mint repository.

Import Ubuntu Toolchain PPA

To begin, we will import the Ubuntu Toolchain PPA, which hosts a variety of GCC versions. This is done via the add-apt-repository command. The -y flag accepts the prompt that would otherwise be displayed automatically.

sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa -y

Update Package List for Ubuntu Toolchain PPA on Linux Mint

After adding the PPA, it is necessary to update the package list so that apt is aware of the new packages available from the Ubuntu Toolchain PPA. This ensures the latest metadata is fetched for the packages you will install.

sudo apt update

Install the Desired GCC Version

At this juncture, you can install the GCC version that meets your requirements. The Ubuntu Toolchain PPA provides several versions of GCC. For example, GCC 14, 13, 12, 11, 10, and 9 were available when this guide was written.

Here are the commands to install these versions:

sudo apt install g++-14 gcc-14
sudo apt install g++-13 gcc-13
sudo apt install g++-12 gcc-12
sudo apt install g++-11 gcc-11
sudo apt install g++-10 gcc-10
sudo apt install g++-9 gcc-9

These commands will install the GCC and G++ compilers for your chosen version.

Validate the Installation of GCC

After the installation, validating that the GCC compiler was installed successfully is wise. Use the following command to confirm that the chosen version of GCC is available for use:

gcc-12 --version # Replace 12 with the version you installed

Setting Up Multiple GCC Compiler Versions

In specific scenarios, as a developer or system administrator, you may need multiple versions of GCC installed on your Linux Mint system. This section will guide you through installing multiple versions of GCC and configuring the system to switch between them as needed.

Install Multiple GCC Versions on Linux Mint via Toolchain PPA

First, let’s focus on installing the GCC versions you need. This command will install several versions of GCC along with their corresponding G++ (the C++ compiler) versions:

sudo apt install gcc-9 g++-9 gcc-10 g++-10 gcc-11 g++-11 g++-12 gcc-12 g++-13 gcc-13 g++-14 gcc-14

This command installs GCC versions 9, 10, 11, 12, 13, and 14 and their corresponding G++ versions.

Configure GCC Version Priorities

Now that multiple versions are installed, it’s essential to let the system know how to choose between them. This is where the update-alternatives command comes into play.

The update-alternatives command allows you to set priorities for each version of GCC. In the example below, we’re setting GCC 12 as the highest priority, followed by GCC 11, GCC 10, and GCC 9.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 90 --slave /usr/bin/g++ g++ /usr/bin/g++-13 --slave /usr/bin/gcov gcov /usr/bin/gcov-13
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 100 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 80 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --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 40 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

These commands associate each GCC version with a priority level (100 for GCC 12, 90 for GCC 13, 80 for GCC 11, and so on). The system will default to the version with the highest priority, but you can change this later if needed, as in the case above, where I wanted to work with GCC 12.

Verify Default GCC Version

After configuring the priorities, let’s confirm that the expected version is now the default GCC version. Use the following command:

gcc --version

Switch Between GCC Versions

You can easily reconfigure the default version using the command if you need to switch to a different GCC version. The following command will display the installed versions along with their priorities:

sudo update-alternatives --config gcc

This will present you with a list of the installed GCC versions. Enter the number corresponding to the version you want to make the default and press Enter.

Create a Test Application (Optional)

In this section, you will learn how to verify that the GCC compiler functions appropriately by creating a simple C program. This is essential to ensure the installed GCC compiler is ready for development projects.

Create a C Program

To begin, let’s create a basic C program. We will be utilizing nano, which is a straightforward text editor in the terminal.

Open Nano and Create a New File

Enter the following command to open nano and create a new file called hello.c:

nano hello.c

Insert Code into the File

Once the text editor is open, add the following code to hello.c:

#include <stdio.h>

int main()
{
   printf("Hello, World from Linuxcapable.com!");
   return 0;
}

This code is a simple C program that prints a message to the console.

Save and Exit the File

To save the file, press CTRL+O. This will write the changes to the file. To exit nano, press CTRL+X.

Compile the C Program

Now that the source code is ready, it’s time to compile the program. Compiling transforms the source code into an executable file for the system.

Use the gcc command to compile the program:

gcc hello.c -o hello

Here, gcc is the command to run the GCC compiler, hello.c is the file name you want to compile, and -o hello specifies the name of the output file; in this case, hello.

Execute the Compiled Program

Finally, let’s execute the compiled program. This is done by entering the following command:

./hello

Upon execution, you should observe the following output in your terminal:

Hello, World from Linuxcapable.com!

This output confirms that your GCC compiler is correctly installed and functioning as expected. It’s now ready for you to start developing and compiling your C and C++ applications on Linux Mint.

Conclusion

With GCC successfully installed on your Linux Mint system, you can leverage its powerful compilation capabilities for your development projects. Using the Linux Mint default repository ensures a stable setup, while the Ubuntu Toolchain PPA provides flexibility with access to multiple GCC versions. Regularly check for updates to keep your compiler current and maintain optimal performance in your development environment. Enjoy the robust and versatile features that GCC offers to developers.

Joshua James
Follow me
Latest posts by Joshua James (see all)

Leave a Comment