Thanks to you all for your help. I ended up just creating aliases within ~/.bash_profile as follows:
alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'
The answer from Lynken is very helpful, but I adapted it with aliases since that's easier for me to undo if necessary.
Specifically, if PATH is set such that /usr/local/bin (where brew puts the link to gcc 4.8) appears before appears /usr/bin (where gcc is linked by default), then creating links as Lyken suggested within /usr/local/bin should theoretically work for me. In practice, it doesn't for some reason -- failing with a linker error and aliases work around that error without me needing to solve that issue, too.
The other benefit of aliases is that I'm not having to link which I want homebrew to handle and not have to compete with that tool for which version of gcc is linked in /usr/local
Thanks to you all for your help. I ended up just creating aliases within ~/.bash_profile as follows:
alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'
The answer from Lynken is very helpful, but I adapted it with aliases since that's easier for me to undo if necessary.
Specifically, if PATH is set such that /usr/local/bin (where brew puts the link to gcc 4.8) appears before appears /usr/bin (where gcc is linked by default), then creating links as Lyken suggested within /usr/local/bin should theoretically work for me. In practice, it doesn't for some reason -- failing with a linker error and aliases work around that error without me needing to solve that issue, too.
The other benefit of aliases is that I'm not having to link which I want homebrew to handle and not have to compete with that tool for which version of gcc is linked in /usr/local
Assuming you're using bash (it's the default), then you can add /usr/local/bin as your top priority in PATH like this:
echo "PATH=\"/usr/local/bin:$PATH\"" >> ~/.bash_profile
This will ensure that /usr/local/bin is checked before all other areas of your path. Then just start a new terminal session to load the new variable.
Another way to do this:
cd /usr/bin
rm cc gcc c++ g++
ln -s /usr/local/bin/gcc-4.8 cc
ln -s /usr/local/bin/gcc-4.8 gcc
ln -s /usr/local/bin/c++-4.8 c++
ln -s /usr/local/bin/g++-4.8 g++
First erase the current update-alternatives setup for gcc and g++:
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
Install Packages
It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:
sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4
Install Alternatives
Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative, where a bigger number is a higher priority.)
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++
Configure Alternatives
The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
Or switch using script:
#!/bin/sh
if [ -z "$1" ]; then
echo "usage: $0 version" 1>&2
exit 1
fi
if [ ! -f "/usr/bin/gcc-
1" ]; then
echo "no such version gcc/g++ installed" 1>&2
exit 1
fi
update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"
execute in terminal :
gcc -v
g++ -v
Okay, so that part is fairly simple. The tricky part is that when you issue the command GCC it is actually a sybolic link to which ever version of GCC you are using. What this means is we can create a symbolic link from GCC to whichever version of GCC we want.
- You can see the symbolic link :
ls -la /usr/bin | grep gcc-4.4 ls -la /usr/bin | grep g++-4.4
- So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:
rm /usr/bin/gcc rm /usr/bin/g++ ln -s /usr/bin/gcc-4.3 /usr/bin/gcc ln -s /usr/bin/g++-4.3 /usr/bin/g++
- Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:
ls -la /usr/bin/ | grep gcc ls -la /usr/bin/ | grep g++
- Finally we can check our GCC -v again and make sure we are using the correct version:
gcc -v g++ -v
In centos,how to switch to default gcc after switched to a higher version of gcc with devtoolset - Unix & Linux Stack Exchange
How to use a newer version of gcc on ubuntu? - Stack Overflow
arch linux - How to temporarily change default version of gcc in ArchLinux - Unix & Linux Stack Exchange
How to change the default GCC options by editing files? - Unix & Linux Stack Exchange
As @Tommy suggested, you should use update-alternatives.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.
It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc, and one will be favoured.
To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:
update-alternatives --query gcc
Now, note the priority attributed to gcc-4.4 because you'll need to give a higher one to gcc-3.3.
To set your alternatives, you should have something like this (assuming your gcc installation is located at /usr/bin/gcc-3.3, and gcc-4.4's priority is less than 50):
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50
--edit--
Finally, you can also use the interactive interface of update-alternatives to easily switch between versions. Type update-alternatives --config gcc to be asked to choose the gcc version you want to use among those installed.
--edit 2 --
Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc file (this will apply the change only for your user, which is safer in my opinion):
echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root:
apt-get install g++-4.5
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50
update-alternatives --set g++ /usr/bin/g++-4.5
update-alternatives --set gcc /usr/bin/gcc-4.5
update-alternatives --set cpp-bin /usr/bin/cpp-4.5
Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:
update-alternatives --auto g++
update-alternatives --auto gcc
update-alternatives --auto cpp-bin
(Note the use of cpp-bin instead of just cpp. Ubuntu already has a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)
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 devtoolset-# via 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 bash:
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:
bash
source ~/.bash_profile
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 ~/.bashrc or ~/.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 ~/.bashrc or ~/.bash_profile and then run bash or 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
echo $$ - second enabling the new devtoolset via
scl enable devtoolset-9 bash - then check your pid again via
echo $$ - 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
~/.bashrc - 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
gcc-7 and gcc-8 will happily co-live together.
I would suggest to let gcc-7 be installed, for satisfying build-essential and perhaps other dependent packages, and configure gcc-8 to be your default gcc installation.
Use update-alternatives for having gcc redirected automatically to gcc-8:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8
This will give you the convenience of gcc being at the latest version, and still you will be able to invoke gcc-7 or gcc-8 directly.
If you'll wish to change the default gcc version later on, run sudo update-alternatives --config gcc. It will bring a prompt similar to this, which lets you pick the version to be used:
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-8 800 auto mode
1 /usr/bin/gcc-7 700 manual mode
2 /usr/bin/gcc-8 800 manual mode
Press <enter> to keep the current choice[*], or type selection number:
The higher priority is the one that is picked automatically by update-alternatives.
Master table of all GCC versions for each Ubuntu
At: How do I use the latest GCC on Ubuntu?
GCC 8 on Ubuntu 16.04
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-8 g++-8
gcc-8 --version
gives 8.1.0 as of 2018-11. See also:
GCC 9 on Ubuntu 19.04
sudo apt install gcc-9
https://packages.ubuntu.com/search?keywords=gcc-9
It's possible you arrived because something blew up with nvidia. (I'm on cudatoolkit 11.4 and actually downgrading got me out of trouble - your mileage may vary.)
sudo apt install gcc-9 g++-9
sudo mkdir /usr/local/cuda/bin
sudo ln -s /usr/bin/gcc-9 /usr/local/cuda/bin/gcc
WARNING - I recommend using timeshift to make a backup of your working system. https://github.com/teejee2008/timeshift
This will get you the latest gcc 11
sudo add-apt-repository 'deb http://mirrors.kernel.org/ubuntu hirsute main universe'
sudo apt-get install g++-11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-11 100
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-11 50
sudo update-alternatives --set g++ /usr/bin/g++-11
sudo update-alternatives --set gcc /usr/bin/gcc-11
sudo update-alternatives --set cpp-bin /usr/bin/cpp-11
gcc --version
gcc (Ubuntu 11-20210417-1ubuntu1) 11.0.1 20210417
Copyright (C) 2021 Free Software Foundation, Inc.
From the looks of it, inside your conda environment, the gcc command has been linked to /home/ubuntu/anaconda3/envs/tensorflow_p36/bin/gcc, and the version is 4. Whereas outside the environment, the only gcc on the path is gcc-8, which corresponds to version 8.
Also, as you've observed, you are unable to create an alternative for gcc because it is linked to g++ alternative.
I also prefer to have gcc be the main alternative, and all other tools (including g++) follow suit. In this case, I will start by deleting the g++ alternative:
Copysudo update-alternatives --remove-all g++
Now that we've got that out of the way, we can create one for gcc which links to gcc-8
Copysudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-8 --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-8 --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-8
References
- https://askubuntu.com/a/26518/145907
- https://askubuntu.com/a/1206264/145907