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"
Answer from hhlp on askubuntu.comFirst 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
How to use a newer version of gcc on ubuntu? - Stack Overflow
How to switch from gcc9 to gcc10 - g++9 to g++10
linux - How to change the default GCC compiler in Ubuntu? - Stack Overflow
Fedora alternative gcc version
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:
sudo 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
sudo 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
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.)
I am required to use a certain gcc version (9.3). Currently fedora ships with gcc v11.
I am wondering if there is a way to use this gcc version on my current fedora machine. Is it possible to use toolboxes for this purpose? Or is there some other solution besides using an distro with an older gcc version?