Recently while building Caffe, I got into a situation where I had to downgrade gcc, If you are in such situation then this post will be a lot saviour for you. Directly removing gcc with the command
$ sudo apt-get autoremove gcc g++ and installing respective gcc version is not recommended.
On the contrary, a cleaner way is to have multiple version of gcc configured in the same machine and then switch them as per requirements.
Let’s install two different gcc versions
Issue the following command and install latest gcc. At the time of writing this article latest gcc was version 7.3, when you issue this command you might get gcc of different version
$ sudo apt-get install gcc g++$ gcc -vgcc version 7.3.0
Now install another version. I am installing gcc 6.x you can install any other version like gcc 4.x, gcc 5.x etc. as per your requirement.
$ sudo apt-get install gcc-6 g++-6$ gcc-6 -vgcc version 6.5.0
Now we will use update-alternatives tool to make a mapping
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 2 --slave /usr/bin/g++ g++ /usr/bin/g++-7$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 1 --slave /usr/bin/g++ g++ /usr/bin/g++-6
Whenever you need to switch between the compiler then issue the following command and select the appropriate option and hit enter
$ sudo update-alternatives --config gccThere are 2 choices for the alternative gcc (providing /usr/bin/gcc). Selection Path Priority Status------------------------------------------------------------* 0 /usr/bin/gcc-7 2 auto mode
1 /usr/bin/gcc-6 1 manual mode
2 /usr/bin/gcc-7 2 manual modePress <enter> to keep the current choice[*], or type selection number:
If you have selected option 1 then issuing following command will show
$ gcc -vgcc version 6.5.0
Tested with Ubuntu 18.04 LTS
Thank you for reading this post, If you like it then please clap and share it on Facebook, LinkedIn or Twitter so that other people get benefits from it.
You can follow me on Github, LinkedIn or contact me at email@example.com