Easy Steps To Install GCC Compiler on AlmaLinux 9 – OrcaCore

Posted on

Easy Steps To Install GCC Compiler on AlmaLinux 9 - OrcaCore

Easy Steps To Install GCC Compiler on AlmaLinux 9 – OrcaCore

This guide, brought to you by Orcacore, will walk you through the process of installing the GCC Compiler on AlmaLinux 9. GCC (GNU Compiler Collection) is a powerful suite of compilers crucial for software development. It supports a wide array of programming languages, including Ada, C, C++, Fortran, Objective-C, and Objective-C++, and historically Java. GCC essentially bridges the gap between human-readable source code and machine-executable instructions.

GCC’s primary function is to translate high-level source code into assembly code. This process involves rigorous syntax and semantic validation, sophisticated optimizations, and the final conversion to assembly language. Furthermore, GCC acts as a command-line "driver," simplifying the interaction with the complex toolchain required for software compilation. It abstracts away many intricate details regarding object file formats and runtime libraries.

Before proceeding, ensure you have access to an AlmaLinux 9 server and are logged in as a non-root user with sudo privileges. If you haven’t already, you can set this up by following our guide on Initial Server Setup with AlmaLinux 9.

1. Installing GCC Compiler on AlmaLinux 9

First, update your system’s package index to ensure you have the latest information about available packages. Execute the following commands:

# sudo dnf update -y
# sudo dnf clean all

The GCC Compiler packages are bundled within a package group aptly named "Development Tools." This group encompasses all the essential utilities required for building applications and libraries on a GNU/Linux system.

Development Tools on AlmaLinux 9

You can verify whether the "Development Tools" group is already installed on your system using the following command:

sudo dnf group list
Development Tools on AlmaLinux 9

If the "Development Tools" group is not listed as installed, you’ll need to install it using the following command:

sudo dnf group install "Development Tools"

Check Development Tools Info

After the compiler and its associated packages are installed, you can use the following command to display detailed information about the "Development Tools" group:

sudo dnf group info "Development Tools"
Check Development Tools Info and GCC Compiler

To verify the successful installation of GCC on your AlmaLinux 9 system, check its version using the following command:

gcc --version
Output
gcc (GCC) 11.3.1 20220421 (Red Hat 11.3.1-2)
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.

To determine the installation path of GCC, use the whereis command:

whereis gcc
Output
gcc: /usr/bin/gcc /usr/lib/gcc /usr/libexec/gcc /usr/share/man/man1/gcc.1.gz /usr/share/info/gcc.info.gz

2. Test GCC Compiler on AlmaLinux 9

Now that GCC is installed, let’s test it with a simple "Hello, World!" program. Create a file named hello.c in your home directory using your preferred text editor (e.g., vi):

cd ~ && sudo vi hello.c

Add the following C code to the file:

#include <stdio.h>

int main() {
  printf("OrcaCore, Hello world!n");
  return 0;
}

Save and close the file.

Compile the hello.c source code using the GCC compiler:

gcc hello.c -o helloworld

This command compiles the hello.c file and creates an executable file named helloworld.

Finally, run the executable:

./helloworld

The output should be:

Output
OrcaCore, Hello world!

This confirms that GCC is correctly installed and functioning on your AlmaLinux 9 system.

Conclusion

You have successfully learned how to Install GCC Compiler on AlmaLinux 9 and create a test program. Stay connected with us on Facebook, Twitter, and YouTube for more helpful tutorials and updates.

Here are some additional articles you might find interesting:

  • Install and Configure WireGuard on AlmaLinux 9
  • Install Nagios Monitoring Tool on AlmaLinux 9
  • What You Need to Know about AlmaLinux 10
  • Telegram Desktop Installation on AlmaLinux 9
  • Set up GIMP on AlmaLinux 9
  • Ansible Installation on AlmaLinux 9
  • Update PHP Version in RHEL 8
  • Skype Installation in AlmaLinux

Alternative Solutions for Installing GCC Compiler on AlmaLinux 9

While using the "Development Tools" group is a convenient way to install GCC and other essential development utilities, there are alternative approaches you can take. These alternatives provide more granular control over the installed packages and can be useful in specific scenarios.

Alternative 1: Installing Individual Packages

Instead of installing the entire "Development Tools" group, you can install the gcc package directly, along with any other specific development tools you require. This approach is beneficial if you only need a subset of the tools included in the group, reducing the overall disk space used.

First, identify the core packages required for C/C++ compilation. Besides gcc, you’ll likely need glibc-devel (the GNU C Library development headers) and binutils (a collection of binary utilities, including the linker).

Use the dnf install command to install these packages individually:

sudo dnf install gcc glibc-devel binutils

You can then verify the installation by checking the GCC version, as demonstrated in the original guide:

gcc --version

This method gives you more control over the packages installed and avoids unnecessary dependencies if you don’t need all the tools included in the "Development Tools" group. It is a more minimalist approach for those with specific needs.

Alternative 2: Using Containers (e.g., Docker)

Another powerful alternative is to use containerization technologies like Docker. This approach allows you to create an isolated environment with all the necessary tools and dependencies for compiling your code, without affecting your host system. This is particularly useful for ensuring consistent builds across different environments or for working with specific compiler versions.

Here’s how you can use Docker to compile a C program:

  1. Create a Dockerfile:

    Create a file named Dockerfile in your project directory with the following content:

    FROM almalinux:9
    
    RUN dnf update -y && 
        dnf install -y gcc glibc-devel binutils && 
        dnf clean all
    
    WORKDIR /app
    
    COPY . .
    
    CMD ["gcc", "hello.c", "-o", "helloworld"]

    This Dockerfile starts from the official AlmaLinux 9 image, installs GCC and related development tools, copies your source code into the container, and compiles it.

  2. Create the C source file (hello.c):

    Create the hello.c source file with the same content as in the original guide:

    #include <stdio.h>
    
    int main() {
      printf("OrcaCore, Hello world!n");
      return 0;
    }
  3. Build the Docker image:

    Navigate to your project directory in the terminal and run the following command to build the Docker image:

    docker build -t gcc-almalinux .
  4. Run the Docker container:

    After the image is built, run a container from it to execute the compilation:

    docker run --rm -v "$PWD:/app" gcc-almalinux

    The --rm flag automatically removes the container after it exits. The -v "$PWD:/app" flag mounts your current working directory (where hello.c is located) inside the container’s /app directory, making the compiled helloworld executable available on your host system.

  5. Execute the compiled program:

    Now, you can execute the compiled program on your host system:

    ./helloworld

This approach offers several advantages:

  • Isolation: Your development environment is isolated from your host system, preventing conflicts with other software.
  • Reproducibility: Docker ensures consistent builds across different environments, as the Dockerfile defines the exact dependencies and configurations.
  • Version Control: You can easily switch between different compiler versions by modifying the Dockerfile.

Using Docker provides a robust and reproducible way to manage your development environment, especially when working on complex projects or collaborating with others. This alternative solution to installing GCC Compiler on AlmaLinux 9 is highly recommended for more advanced uses.

Leave a Reply

Your email address will not be published. Required fields are marked *