The best way to correctly install gcc-4.9 and set it as your default gcc version use:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9
The --slave, with g++, will cause g++ to be switched along with gcc, to the same version. But, at this point gcc-4.9 will be your only version configured in update-alternatives, so add 4.8 to update-alternatives, so there actually is an alternative, by using:
sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
Then you can check which one that is set, and change back and forth using:
sudo update-alternatives --config gcc
If you have an issue with update-alternatives gcc priority 60 not being higher than previous versions installed you can use the previous update-alternatives --config gcc command to check installed versions and use:
sudo update-alternatives --remove gcc
Or:
sudo update-alternatives --remove-all gcc
NOTE: You could skip installing the PPA Repository and just use /usr/bin/gcc-4.9-base but I prefer using the fresh updated toolchains.
For GCC 5.X or 6, the packages (and correspondingly, the commands) are just called gcc-5, gcc-6, etc. This is due to the change in GCC's version scheme, where 5.1 is the first GCC 5 release, and future 5.X releases are for bug fixes.
The best way to correctly install gcc-4.9 and set it as your default gcc version use:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9
The --slave, with g++, will cause g++ to be switched along with gcc, to the same version. But, at this point gcc-4.9 will be your only version configured in update-alternatives, so add 4.8 to update-alternatives, so there actually is an alternative, by using:
sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
Then you can check which one that is set, and change back and forth using:
sudo update-alternatives --config gcc
If you have an issue with update-alternatives gcc priority 60 not being higher than previous versions installed you can use the previous update-alternatives --config gcc command to check installed versions and use:
sudo update-alternatives --remove gcc
Or:
sudo update-alternatives --remove-all gcc
NOTE: You could skip installing the PPA Repository and just use /usr/bin/gcc-4.9-base but I prefer using the fresh updated toolchains.
For GCC 5.X or 6, the packages (and correspondingly, the commands) are just called gcc-5, gcc-6, etc. This is due to the change in GCC's version scheme, where 5.1 is the first GCC 5 release, and future 5.X releases are for bug fixes.
Ultimate mega master compatibility table
OK let's do this:
GCC clang
+-------------------------------------+-------------------------------------------------+
| 15 14 13 12 11 10 9 8 7 6 5 4 | 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 |
+-------+-------------------------------------+-------------------------------------------------+
| 25.10 | D M M M M | D M M M |
| 25.04 | D M M M | D M M M |
| 24.10 | D M M M | D M M M M M |
| 24.04 | M D M M M M | D M M M M |
| 23.10 | D M M M M | M D M M M |
| 23.10 | D M M M M | D M M M |
| 23.04 | M D M M M | M D M M |
| 22.10 | D M M M | D M M |
| 22.04 | P M D M M | D M M M |
| 21.10 | D M M M | D M M M M |
| 21.04 | M D M M M | D M M M |
| 20.10 | D M M M | D M M M M M |
| 20.04 | P D M M | D M M M M |
| 19.10 | D M M | |
| 19.04 | M D M M | |
| 18.10 | D M M M | |
| 18.04 | P P M D M M | M M M D M |
| 16.04 | P P P P D M | |
+-------+-------------------------------------+-------------------------------------------------+
D: Default GCC
sudo apt-get update sudo apt-get install gcc g++ gcc --versionWhatever the
gccpackage aliases to: https://packages.ubuntu.com/search?keywords=gcc and also present in manifests: How do I list the default installed packages?M: Present in Main repo
sudo apt-get update sudo apt-get install gcc-X g++-X gcc-X --versionAll Ubuntu versions that have a hit for a given GCC version, e.g. for GCC 7: https://packages.ubuntu.com/search?keywords=gcc-7 or clang 7 https://packages.ubuntu.com/search?keywords=clang-7
The minor versions of these packages can get updated from time to time within a single Ubuntu version (on the
.revisions?), e.g. 8.3.0 to 8.4.0 so we are not keeping track of that.P:
ppa:ubuntu-toolchain-r/test, which is owned by Ubuntu people and therefore can be trusted to not be a virus, although it is possibly unstable:sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-get install gcc-X g++-X gcc-X --versionFull list: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test
The minor versions of these packages can get updated from time to time, e.g. 8.3.0 to 8.4.0.
Blank spaces on the table mean either "no package available" or "I didn't bother to check". Notably I've not been looking into PPA packages too thoroughly. Edits accepted.
All the questions:
- install gcc-9 on Ubuntu 18.04?
- How to install gcc-7 or clang 4.0?
- install gcc-9 on Ubuntu 18.04?
- What are the GCC and clang versions available in Ubuntu 18.04?
How to set a non-default GCC as the default?
E.g., you installed /usr/bin/gcc-7 but you want to use that instead of /usr/bin/gcc when you run gcc main.c.
Use sudo update-alternatives as mentioned in other answers: https://askubuntu.com/a/581497/52975 It creates the required symlinks for you.
See also: What exactly does `update-alternatives` do?
How to build your own toolchain from source
If even the PPA is not old/new enough for you, see this:
- https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603
- https://stackoverflow.com/questions/26305738/can-i-build-gcc-for-arm-with-an-x64-one/26306591#26306591
Older GCC version questions
- Ubuntu 20.04 - gcc version lower than gcc-7
- How to use an older version of GCC
Videos
I think you can get it by adding this PPA to your repositories:
https://launchpad.net/~ubuntu-toolchain-r/+archive/test
You can add the PPA by running
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
After it's in the repositories (and after running apt-get update) you should be able to either 1) update to the latest version using apt-get upgrade, or possibly 2) you'll have it available as a separate package you need to you'll need to apt-get install. I'm not sure which is the case with this package.
Further information: Guide on how to add a PPA to your repositories.
Add https://launchpad.net/~ubuntu-toolchain-r/+archive/test to your repositories by running:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Then follow the steps on AskUbuntu to map gcc to the version you just installed.
In our case, you want to run:
sudo apt-get update
sudo apt-get install gcc-4.6
sudo apt-get install g++-4.6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
You can install GCC 6 by adding the ubuntu-toolchain-r/test PPA. To do so, run the following commands:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-6
You can verify that gcc-6 is installed by running gcc-6 --version and the output should say gcc-6 (Ubuntu 6.1.1-2ubuntu12~16.04) 6.1.1 20160510.
As suggested by Mohamed Slama, if you want to further change the default GCC and G++ to the latest versions, install g++-6 with
sudo apt install g++-6
and then run
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6
If you want to build it from source (which I recommend as you can for example make a cross-compiler, etc.) download the source from a mirror.
Then extract it with:
tar -xvf gcc-6.1.0.tar.gz
After that change directory to there:
cd gcc-6.1.0
Then create build directory and cd to it:
mkdir build
cd build
Then configure the makefile (--disable-multilib means to not build libraries for cross-compilation):
../configure --enable-languages=c,c++ --disable-multilib
If you ran into errors due to missing required libraries or other prerequisites: (Credits to this)
./contrib/download_prerequisites
And then build it:
make -j 8
This process may take some time and after done invoke this:
sudo make install
That's it!
Hey can anyone tell me if there's an easy way or a set of directions I can follow to install and use GCC13.2.0 in Ubuntu based distros. I'm using both Ubuntu LTS and latest version.