I want to install several gcc with different versions in centos. The default version of gcc in centos 6 is 4.9.3. So I use devtoolset install a higher version of gcc. Then I switch to the higher version of gcc by executing "source /opt/rh/devtoolset-5/enable". But now if I want to switch back to the default gcc, how should I do? By the way, is there any solution to install multiple gcc with different versions in centos 5?
The version of
gcc that's distributed with
CentOS 6 is actually 4.4.7.
You can install as many versions of
gcc either by installing
yum or by compiling then from source.
The first way is the easiest. Make sure that you are installing the
devtoolset packages via the
scl repo. I figure that you already did as you have installed one already but in case you didn't:
yum install centos-release-scl
You can then use the below command to set the
gcc version to whichever one you want. Using 5 for this example and assuming that your shell is
scl enable devtoolset-5 bash
If you want to change to 6:
scl enable devtoolset-6 bash
If you want to change back to the default then any of the following will work assuming
bash is your shell:
The first will start a new shell session and set any aliases/variables/commands in
~/.bashrc. The second will set it with the variables/commands in
~/.bash_profile. (Without the devtoolset enabled).
You can even put
scl enable devtoolset-5 bash, for example, in
~/.bash_profile so that it sets the
gcc version to one of the
devtoolset versions at login. To go back to the system default if you use this method, comment the line out in
~/.bash_profile and then run
source ~/.bash_profile, respectively. That will start a new shell session with everything in one of those shell init files except the
scl enable command that you commented out. The only downside is that any variables that you've set via the
export command will no longer be there as the shell session will be new.
I'm no expert on
scl but I do have years of linux experience.
When you do
scl enable devtoolset-9 bash what is happening is that a new bash is started and a new environment is set up.
You can see the new
bash process by:
- first starting a new shell and checking your shell's pid via
- second enabling the new devtoolset via
scl enable devtoolset-9 bash
- then check your pid again via
- for bonus points you can do
pstree -pto see that your new bash pid has a parent pid of your old bash process
So to finally answer your question: To return to the default
g++ compiler all you need to do is exit your current
bash process and then you should have the old g++ compiler.
Important note regarding your ~/.bashrc:
- my solution won't work if you have somehow modified your
- i.e. if you have something in there that always does the
scl enable devtoolset-9
- see the other solutions on this page because the other solutions talk more in-depth about your ~/.bashrc and how to modify or unmodify it
It's probably just a matter of changing some environment variables,
How big is that
Can you look at it, see what it does,
and adapt it for the older compiler that you want to use?
The obvious things to look for would be the names of the directories
where the new compiler's files are installed
(change them to the corresponding pathnames for the old compiler)
and the version number.
I don't know what else is likely to be there.