How to install GCC on CentOS 7

How To Install GCC on CentOS 7

Spread the love

yum install gccIn this tutorial, we will take a look at how to install GCC on CentOS 7. GCC or GNU Compiler Collection is released by the Free Software Foundation and as the name suggests, it is a very useful collection of programming compilers such as C, C++, Objective-C, Objective-C++, Fortran, Java, Go and Ada. GCC is an official compiler of the GNU operating system but also it is a standard compiler on many Unix operating systems such as Linux. Installing GCC on CentOS 7, is really an easy task and it shouldn’t take more than 10 minutes.

1. Update the server

First of all log in to your CentOS 7 VPS via SSH as user root

ssh root@IP_Address -p Port_number

and make sure that all installed packages on the server are up to date

yum -y update

2. Install GCC from repository

GCC can be easily installed from the official CentOS repositories. Run the following command to install GCC on your server

yum -y install gcc

Once the installation is completed, you can the version of GCC installed on your server

gcc --version

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO

As might be seen from the output, the GCC version distributed by CentOS 7 is 4.8.5 which is not the latest version of GCC.

You may also want to install gcc-c++. It will automatically include the C++ library and compile files with extensions that indicate they are C source as C++, instead of as C.

yum -y install gcc-c++

3. Install GCC from source

If you need a newer version of GCC than the one distributed by the official CentOS 7 repositories you can install it from source.

It is highly recommended to start a screen session before starting the installation. Run the following command

screen -U -S gcc

Download the tarball of the GCC version you want to install. In our example we will use GCC version 7.3.0


Unpack the tar archive and change the current working directory

tar zxf gcc-7.3.0.tar.gz
cd gcc-7.3.0

Install bzip2 and run the ‘download_prerequisites’ script to download some prerequisites needed by GCC. You have to run this from the top level of the GCC source tree.

yum -y install bzip2

Once the prerequisites are downloaded execute the following command to start configuring the GCC build environment

./configure --disable-multilib --enable-languages=c,c++

Once it is completed, run the following command to compile the source code. It may take a few hours for the compilation to complete, so sit back and relax.

make -j 4
make install

4. Verify GCC installation on CentOS 7

After the compilation process is completed. You can check if GCC is properly installed.

gcc --version

gcc (GCC) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

install gcc linuxOf course, you don’t have to install GCC on CentOS 7,  if you use one of our outsourced server support services, in which case you can simply ask our expert Linux admins to install GCC on your CentOS 7 server for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, on how to install GCC on CentOS 7,  please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

42 thoughts on “How To Install GCC on CentOS 7

  1. Hi,
    I’m new to linux and i’m trying to understand it better, can you please detail this line: “ssh root@IP_Address -p Port_number

    What is IP_address, where do i find it, and what is Port_number and where do i find it, if you could provide an answer it would be awesome.

    Thanks in advance!

    1. This command is used to establish an SSH connection to a remote server. If you are doing this installation on your local computer, you can skip this part.

      The IP address is a unique numerical number assigned to every device connected to the Internet. The Port_number in this command refers to the port number of the SSH service which is running on your server. Usually, the default SSH port is set to 22.

      If you are not sure about the IP address and the SSH port number of your server you should contact your hosting provider and ask them for this information.

  2. On centos 7 I have installed ccg 7.3.0:
    #gcc –version
    gcc (GCC) 7.3.0
    Copyright (C) 2017 Free Software Foundation, Inc.

    however, gfortran version is 4
    # gfortran –version
    GNU Fortran (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16)

    I really need gfortran 6 o newer for some application

    please help me, I had tried many things and they didn’t work.

    1. You can install GNU Fortran using yum, RPM package or build it from source. Please check the official documentation for installation instructions.

  3. Hi, I’m not used to Linux much, but can you tell me why “It is highly recommended to start a screen session before starting the installation.” on Section 3? Thank you.

  4. This is because processes running in a screen session will continue to run even if the window is closed or you get disconnected.

  5. I followed your steps step by step but at the end there was an error.
    make install
    make[1]: Entering directory `/root/gcc-7.3.0′
    /bin/sh ./mkinstalldirs /usr/local /usr/local
    make[2]: Entering directory `/root/gcc-7.3.0/host-x86_64-pc-linux-gnu/fixincludes’
    make[2]: *** No rule to make target `install’. Stop.
    make[2]: Leaving directory `/root/gcc-7.3.0/host-x86_64-pc-linux-gnu/fixincludes’
    make[1]: *** [install-fixincludes] Error 2
    make[1]: Leaving directory `/root/gcc-7.3.0′
    make: *** [install] Error 2

  6. Hi, I am wondering if the Gcc version needs to match with some specific kernel version? Or those two are independent? Thanks.

  7. I followed your steps, and things seemed to go as expected, except there is a discrepancy between the version reported by gcc –version, which is 5.5.0 (the release I need for my client) and g++ -v, which reports 4.8.5. How do I make it so g++ links to the same compiler installation?

    1. The latest gcc and g++ version distributed by CentOS 7 is 4.8.5. If you would like to install a different version you would have to install it from source.

  8. I need a newer … but after compilation those are not created …. do I have to use another make command to do that?

  9. Hello! Is it possible to somehow install the version above 4.8.5 without being a superuser?

      1. Not true, you can build it locally without having root or sudo access just fine.
        You can use:

        ./configure –prefix=/home/user/your/repo
        make PREFIX=/home/user/your/repo install

  10. Just an extremely minor thing. Before point 4, you might consider advising the user to reload files from the path again. Otherwise older version of gcc appears on invocation at that step.

    1. What do you mean by reload files from path again? I followed the steps but I still see the old version on using the gcc –version command. Maybe this is the step that I am required to do.

  11. I followed your steps step by step but on the step
    make install

    echo GTFILES_LANG_H=gtype-ada.h gtype-c.h gtype-cp.h gtype-fortran.h gtype-go.h gtype-java.h gtype-jit.h gtype-lto.h gtype-objc.h gtype-objcp.h >> tmp-header-vars; \
    /bin/sh ../.././gcc/../move-if-change tmp-header-vars b-header-vars
    /bin/sh: tmp-header-vars: Permission denied

    /bin/sh: tmp-header-vars: Permission denied
    mv: cannot stat ‘tmp-header-vars’: No such file or directory
    make[2]: *** [s-header-vars] Error 1

    not sure how to proceed

  12. i installed the gcc and g++ according to the guide but when i do g++ –version it give me :
    yaodav@localhost ~]$ g++ –version
    g++ (GCC) 7.4.0
    but when i give the command as sudo i get :
    yaodavdav@localhost ~]$ sudo g++ –version
    g++ (GCC) 4.8.5 20150625 (Red Hat 4.8.5-36)
    how to enable the new gcc also to the sudo?

    the right version but when i give the command with sudo i get the old version

  13. /root/gcc-7.3.0/x86_64-pc-linux-gnu/libstdc++-v3/include/tr1/utility:106:1: fatal error: can’t write PCH file: No space left on device
    compilation terminated.
    make[5]: *** [x86_64-pc-linux-gnu/bits/stdtr1c++.h.gch/O2g.gch] Error 1
    make[5]: Leaving directory `/root/gcc-7.3.0/x86_64-pc-linux-gnu/libstdc++-v3/include’
    make[4]: *** [all-recursive] Error 1
    make[4]: Leaving directory `/root/gcc-7.3.0/x86_64-pc-linux-gnu/libstdc++-v3′
    make[3]: *** [all] Error 2
    make[3]: Leaving directory `/root/gcc-7.3.0/x86_64-pc-linux-gnu/libstdc++-v3′
    make[2]: *** [all-stage2-target-libstdc++-v3] Error 2
    make[2]: Leaving directory `/root/gcc-7.3.0′
    make[1]: *** [stage2-bubble] Error 2
    make[1]: Leaving directory `/root/gcc-7.3.0′
    make: *** [all] Error 2

  14. I followed the instruction to install gcc-8.2.0 on CentOS 7, but it cannot complete for
    make -j 4

    It ended up with error messages after
    configure: creating ./config.status
    config.status: creating Makefile
    config.status: creating doc/Makefile
    config.status: creating src/Makefile
    config.status: creating tests/Makefile
    config.status: creating tune/Makefile
    config.status: creating src/mparam.h
    config.status: executing depfiles commands
    config.status: executing libtool commands

    make[2]: Leaving directory `/aspendata/install/gcc-8.2.0′
    make[1]: *** [stage1-bubble] Error 2
    make[1]: Leaving directory `/aspendata/install/gcc-8.2.0′
    make: *** [all] Error 2

  15. I followed all the steps to install of section “3. Install GCC from source”.
    I hade no error. But when I do gcc –version I stiil got the native version 4.8.5 (centos 7).
    Where is installed the 7.3 version?

    1. Please check /usr/local as that is the default location of the installation. Please make sure you have downloaded the prerequisites and configured the GCC build environment as stated on the procedure.

  16. also I dont have Screen installed on my machine , doesnt it work without screen session?
    about step : screen -U -S gcc

    1. It is highly recommended to start a screen session because the installation would take a while to accomplish and losing your connection or any interruption might break the installation and you have to start again.

  17. Thanks for this valuable information,
    also Please can you tell, how to uninstall gcc that is being installed by the way you specified.

    Thanks in advance

  18. Thanks for the detailed information. I really like the kindness you show when answering in the “comments” section.

  19. Hi, I am using gcc-8.3.1 on Centos7 cluster. I am having same kind of errors when I am trying to install some certain R packages. Here is the process that I am following:

    module load R/4.0.3

    module unload gcc

    module load gcc/8.3.1


    After launching R, trying to install packages and having same errors (see for Rcpp package):
    g++-8.3.1 -I”/share/builds/spack/opt/spack/linux-centos7-sandybridge/gcc-8.3.0/r-4.0.3-zkmqmoo7jq7xgup5bt6azwkcph6jxqcm/rlib/R/include” -DNDEBUG -I../inst/include/ -I/usr/local/include -std=c++11 -fpic -g -O2 -c api.cpp -o api.o
    /bin/sh: g++-8.3.1: command not found

    make: *** [/share/builds/spack/opt/spack/linux-centos7-sandybridge/gcc-8.3.0/r-4.0.3-zkmqmoo7jq7xgup5bt6azwkcph6jxqcm/rlib/R/etc/Makeconf:181: api.o] Error 127
    ERROR: compilation failed for package ‘Rcpp’
    * removing ‘/nethome/mxk841/R/x86_64-pc-linux-gnu-library/4.0/Rcpp’
    * restoring previous ‘/nethome/mxk841/R/x86_64-pc-linux-gnu-library/4.0/Rcpp’

    Warning message:
    In install.packages(“Rcpp”, type = “source”) :
    installation of package ‘Rcpp’ had non-zero exit status

    Any tips for this kind of errors. Thanks in advance!

  20. Im new to linux. I installed the gcc compiler to compile openmpi on google cloud and it seemed to install fine since I checked using gcc –version. But when I tried to compile openmpi using ./configure –prefix=”/home/bogunbayo/openmpi411/” it gave the following error messages

    *** C++ compiler and preprocessor
    checking for g++… no
    checking for c++… no
    checking for gpp… no
    checking for aCC… no
    checking for CC… no
    checking for cxx… no
    checking for cc++… no
    checking for cl.exe… no
    checking for FCC… no
    checking for KCC… no
    checking for RCC… no
    checking for xlC_r… no
    checking for xlC… no
    checking whether we are using the GNU C++ compiler… no
    checking whether g++ accepts -g… no
    checking dependency style of g++… none
    checking how to run the C++ preprocessor… /lib/cpp
    configure: error: in `/home/bogunbayo/openmpi-4.1.1′:
    configure: error: C++ preprocessor “/lib/cpp” fails sanity check
    See `config.log’ for more details

    What did I do wrong? What do I need to do to fix this? Thank you

Leave a Reply

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