How to upgrade gcc on CentOS

Question: The default gcc version on CentOS is too old to build a particular application. How can I upgrade the version of gcc installed on CentOS?

As of CentOS 6.5, the default version of gcc installed is 4.4.7. If you want to upgrade gcc on CentOS, you can install devtools, which is a CentOS version of Red Hat Developer Toolset. The devtools, which is available for CentOS 5 and 6, contains a suite of development tools including gcc, gdb, binutils, elfutils, etc. The devtools 1.1. offers gcc version 4.7.2.

To install devtools on CentOS, use the following commands.

$ sudo wget -P /etc/yum.repos.d
$ sudo sh -c 'echo "enabled=1" >> /etc/yum.repos.d/devtools-1.1.repo'
$ sudo yum install devtoolset-1.1

devtools will be installed under /opt/centos/devtoolset-1.1.

To use gcc from devtoolset, use the following command. This command will launch a new shell, wherein all environment variables are automatically set to use devtools.

$ scl enable devtoolset-1.1 bash
$ gcc --version
gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)

Alternatively, you can explicitly define the following variables to use devtools.

export CC=/opt/centos/devtoolset-1.1/root/usr/bin/gcc  
export CPP=/opt/centos/devtoolset-1.1/root/usr/bin/cpp
export CXX=/opt/centos/devtoolset-1.1/root/usr/bin/c++
Download this article as ad-free PDF (made possible by your kind donation): 
Download PDF

Subscribe to Ask Xmodulo

Do you want to receive Linux related questions & answers published at Ask Xmodulo? Enter your email address below, and we will deliver our Linux Q&A straight to your email box, for free. Delivery powered by Google Feedburner.

Support Xmodulo

Did you find this tutorial helpful? Then please be generous and support Xmodulo!

One thought on “How to upgrade gcc on CentOS

  1. To set this permanently, you have to add:

    export PATH=/opt/centos/devtoolset-1.1/root/usr/bin/:$PATH

    in your ~/.profile or ~./profile_bash or equivalent.

Leave a comment

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