How To Install Newer Version Of GCC on CentOS 6.x

You might encounter GCC libraries erros that stated you use older  version of gcc or you need newer version of gcc in some cases to run some programs on CentOS 6 system.

I will show you How To Install Newer Version Of GCC on CentOS 6.x

You have to compile new gcc version on your linux machine. I suggest you to use a server that has more than 4 cpu because compiling gcc takes hours if you have limited cpu on your server. In addition to cpu number importance, you need a more than 20gb free space for compiling gcc. Please take account these suggestions.

As of the writing of this article, centos 6.x use GCC v4.4.7 version and latest GCC v7.3 released. You can install any version on your VPS server.

How To Install GCC on CentOS 6.x

We will install older version of GCC from base repo on our VPS because we need C++ compiler in order to compile newer version on our VPS server

1. Login to your VPS

A. Login as a Root user
B. Create a user account and set a password for it
adduser <username>
passwd <username>

C. You have to give root privileges to your newly created user account
Find the line "root ALL=(ALL) ALL" and after that line
Add the line "<username> ALL=(ALL) ALL"
then Ctrl+C and write :wq and exit.

D. You can logou t from your root account and re-login with new accout that you gave root privileges

2. Install GCC packages on your Centos 6.x VPS.
sudo yum install gcc gcc-c++

3. After install, Check GCC versions and locations.
gcc --version
May say: gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)
g++ --version
May say: g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11)
which gcc
which g++

How To Install Newer Version Of GCC on CentOS 6.x

We installed officially supported GCC version on our VPS but we want newer version so lets continue…

1. Be sure! you installed older version of GCC that we show you complete steps above. 

2. We need to install additionally required packages:
sudo yum install svn texinfo-tex flex zip libgcc.i686 glibc-devel.i686

3. We can decide which version of GCC we are gonna compile and use our VPS. This command will show you all availabe versions GCC :
svn ls svn://gcc.gnu.org/svn/gcc/tags | grep gcc | grep release


4. We decided to compile GCC 6.1 version. This steps could takes few minutes depending on your internet connection. This article is written for gcc_6_1_0_release/ so please be careful while copying our code if you decided to install different version.
mkdir ~/GCC-source
cd ~/GCC-source
svn co svn://gcc.gnu.org/svn/gcc/tags/gcc_6_1_0_release/

5. After getting all files, lets install source of additional prerequisites. It will install MPFR, GMP , and MPC on your VPS. Run this command inside of the source files.
cd gcc_6_1_0_release/
--- Important, run this as shown, from the gcc_6_1_0_release directory.
--- Do not! cd to the contrib directory or you get errors

6. compiling use great amount of memory so if you have a memory lower than 1GB on your VPS server we suggest you to follow these steps. However, If you have more than 2GB you can skip this step. This step add 500MB of virtual memory, using a swap file.
dd if=/dev/zero of=$SWAP bs=1M count=500
mkswap $SWAP
sudo swapon $SWAP

7. We are building ! This could take hours depending on how man cpu you have. If this thing goes smoothly we will see “finished” as a last line. It is normal to see some error like messages. We will use different directory to compile this because it is better way to do it.
cd ..
mkdir gcc_6_1_0_release-build/
cd gcc_6_1_0_release-build/
../gcc_6_1_0_release-build/configure && make && sudo make install && echo "finished"
--- If your VPS has multiple CPU cores, you can speed up this progress by changing the middle part of the code
--- change line from "&& make &&" to "&& make -j <number of CPU cores> &&".
--- You can see how many CPU cores your VPS has by running this code "nproc"

8. If you followed the instructions at 6. steps remove the swap file.
sudo swapoff $SWAP
rm /tmp/swap

9. Lets check installed versions and their locations
hash -r
(Makes your login "forget" about the previously seen locations of gcc and g++)
gcc --version
May say: gcc (GCC) 6.1.0
g++ --version
May say: g++ (GCC) 6.1.0
which gcc
which g++

10. We need to add new libraries to ld (the GNU linker).
echo "/usr/local/lib64" > usrLocalLib64.conf
sudo mv usrLocalLib64.conf /etc/ld.so.conf.d/
sudo ldconfig
--- This may say a file or two "is not an ELF file - it has the wrong magic bytes at the start."
--- You may ignore this message. It is silent about the work it successfully completed.

11. After we compiling newer version of GCC, our GCC-source folder size is over 8.0GB or so. We can keep this folder in case of need in future. However, if you need space you can reclaim this space.

cd ~/
rm -rf GCC-source

Yes, we have newer version of GCC on our centos 6.x VPS now.

CentOS officially supported GCC versions can be found in /usr/bin/ and include files in /usr/include

You newer GCC version files can be found in : /usr/local/bin  newer 32-bit libs in  /usr/local/lib newer 64-bit libs in /usr/local/lib64 newer include files in /usr/local/include

Lets point out some other issues that Centos 6.x users face and solve with it newly compiled GCC versions :  GLIBCXX_3.4.xx Not Found – Install Newer Version Of GLIBCXX on Centos 6