177

I want to compile my program with the latest version of gcc.

Ubuntu 14.04 comes with gcc 4.8.2, however there's 4.9.0 available, moreover, I see that it is available as a package: gcc-4.9. I tried to install it

sudo apt-get install gcc-4.9

but it says

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'gcc-4.9-base' for regex 'gcc-4.9'
gcc-4.9-base is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Looks like it is already installed, just not as the default one? How do I utilize it to build my program?

muru
  • 197,895
  • 55
  • 485
  • 740
Nickolai Leschov
  • 8,250
  • 19
  • 50
  • 90

5 Answers5

237

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.

SudoSURoot
  • 2,829
  • 2
  • 14
  • 16
  • Thanks for the edit @muru ...like I said below, I was up for about 3 days very tired and update-alternatives does requires sudo and I think I forgot the (--) before slave. :) So, Thank you. – SudoSURoot Feb 15 '15 at 17:16
  • Try sudo apt-get -f install – SudoSURoot Oct 25 '15 at 02:18
  • 4
    This works perfectly. Also, substitute 5 in for 4.9 to get GCC 5 working on Ubuntu. (My development team now runs both 4.9 and 5 alongside each other following your method.) Thank you, and +1! – CodeMouse92 Nov 06 '15 at 16:52
  • I substituted gcc-4.9 with gcc-5 in the commands, but the eventual gcc -v still shows the 4.x version. Anyone else had this issue? – benjaminz Apr 18 '16 at 14:31
  • It works for gcc-5, but not for gcc-6. For gcc-6 it complains that it can't locate the package. – Moj Apr 27 '16 at 16:17
  • 2
    @hwat It should work for gcc-6 now. – edwinksl Jun 05 '16 at 02:38
  • 1
    This has helped me several times, but I always forget - make sure to apply the same solution to other utilities in the GNU compiler collection you may use! GCOV for example will complain about a version mismatch if you do not, possibly producing incorrect data. – Toby Nov 10 '16 at 10:39
  • Needed this as help installing CLFS and using 16.10 as the host system. 6.2.0 in 16.10 is too new and causes build failures – eyoung100 Mar 28 '17 at 23:25
  • saved the day ... – Arpit Solanki Jun 23 '17 at 13:59
  • Why Linux distributions are still supplied with GCC 4.9? Why aren't they going forward to GCC 7.x branch? – Royi Sep 01 '17 at 08:37
  • Full list of commands on https://gist.github.com/application2000/73fd6f4bf1be6600a2cf9f56315a2d91 is helpful: includes configuring update-alternatives. – WillC Nov 04 '18 at 09:11
  • 1
    Confirmed working for gcc-10 – Sterls Sep 28 '20 at 16:58
  • May I suggest editing to explain the random 60 after the /gcc.(version) file? I found an answer here. The 60 in my case didn't work because other versions were at 90. – Elliott Sep 20 '21 at 04:48
  • The 60 is the priority. The reason this didn't work for you is that you most likely had another version of gcc set as your alternative already with a higher priority. I don't believe that higher priority is the correct fix for everyone. I don't have time right this minute to do this but I can add an update-alternatives --remove-all for command for gcc. – SudoSURoot Sep 21 '21 at 10:08
  • I added a little information on checking and removing older versions of gcc for priority issues – SudoSURoot Sep 21 '21 at 10:19
  • If I run your "update-alternatives" command I get this error: update-alternatives: error: no alternatives for gcc even though I've gcc-12 and gcc-11 installed. – Raleigh L. Oct 31 '22 at 03:19
  • Make sure you have the correct file location and name for gcc and g++. Use which gcc and which g++ to find the locations Ex: sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12.2 60 --slave /usr/bin/g++ g++ /usr/bin/g++-12.2 – SudoSURoot Nov 02 '22 at 11:57
46

Ultimate mega master compatibility table

OK let's do this:

                  GCC                clang
        +-------------------------+--------------------------------+
        | 13 12 11 10 9 8 7 6 5 4 | 16 15 14 13 12 11 10 9 8 7 6 5 |
+-------+-------------------------+--------------------------------+
| 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 |     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 |                                |
+-------+-------------------------+--------------------------------+

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:

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:

Older GCC version questions

  • what if I want to have only one compiler version installed? how do I get rid of older one? and what does update-alternatives command, that other people suggest, do, that you do not have it in your answer? – Noone AtAll Dec 22 '19 at 05:53
  • and why is there a hole for Ubuntu 18.10 and GCC 9? It is not supported? – Noone AtAll Dec 22 '19 at 07:36
  • @NooneAtAll update-alternatives creates symlinks between /usr/bin/gcc and your GCC of choice. You should use it in conjunction with this answer if you want to make gcc be a non-default GCC: https://askubuntu.com/questions/233190/what-exactly-does-update-alternatives-do Empty case means not supported via the methods described above. Notably, the PPA appears to only support LTS releases of Ubuntu. I would not recommend using a non LTS version of Ubuntu except the latest release. – Ciro Santilli OurBigBook.com Dec 22 '19 at 08:47
  • So, it is not possible to run the latest g++ (g++-11) on my very old PC with Lubuntu 16.04? There is not way at all? – Chameleon Dec 02 '21 at 15:23
  • 1
    @Chameleon either build from source or try to find another PPA – Ciro Santilli OurBigBook.com Dec 02 '21 at 16:48
  • Any chance of updating for gcc-12? – user7761803 Jul 05 '22 at 11:22
  • 1
    @user7761803 it's not on the official Ubuntu PPA yet: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test so you have to either compile from source ( see e.g. https://stackoverflow.com/questions/21872229/how-to-edit-and-re-build-the-gcc-libstdc-c-standard-library-source/51946224#51946224 ) or find some other PPA. – Ciro Santilli OurBigBook.com Jul 05 '22 at 11:45
  • Please update for gcc-13. I can see this package on the link from this answer (https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test/+packages) but cannot install it on Ubuntu 22.04. Update: seems I have to upgrade to 23.04 for this. – 4LegsDrivenCat Jan 03 '23 at 13:30
  • 1
    @4LegsDrivenCat yes. And also it seems GCC 13 is not released yet: https://www.phoronix.com/news/GCC-13-Ends-Stage-1-Development Let's wait a bit before adding it in the answer I think. If you need it, the best approach is to compile from source as per "How to build your own toolchain from source". – Ciro Santilli OurBigBook.com Jan 03 '23 at 17:59
25

Use the Toolchain Test Builds PPA:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9

I don't think GCC 4.9 is fully available for Ubuntu 14.04 yet. The base package (gcc-4.9-base) and the GCC Go 4.9 compiler (gccgo-4.9) are available, but the other frontends are not. I don't know why.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 1
    @SudoSURoot it may or may not be set as default, that's not my concern (there are other posts dealing with that). However, that GCC 4.9's C and C++ frontends can be installed on 14.04 purely from the official repositories is patently false, since the gcc-4.9-base package does not and will not contain a binary frontend. – muru Feb 14 '15 at 22:18
  • You are correct about the base package. As you'll see in my answer, I used the PPA. I posted the comment first, then answered the question as stated, from question... Because simply adding the PPA and installing 4.9 does not enable it which is needed to "use gcc 4.9". I should have deleted the comment after I answered the question. I apologize. My answer is what should be focused on. I jumped to conclusions... and commented to fast. I am actually pretty new with using 14.04 since I got me dedicated server. I'm gong to make sure I didn't down vote and I'll fix it if I did. – SudoSURoot Feb 14 '15 at 22:28
  • @SudoSURoot alright. Even so, unless OP has a problem with calling gcc-4.9 or g++-4.9 instead of gcc or g++, I will stick to not updating the alternatives. – muru Feb 14 '15 at 22:31
  • I totally understand. I build Android systems though, which uses a lot of $(which gcc) and $(which g++) etc... So I always have to have defaults set to the version I want to use. Some of my kernel code wouldn't compile with host gcc-4.8 so I had to update. I just repeated what worked for me. Thanks for reminding me though... Like I said in my edit, from last comment, I may have down voted, prematurely, if so I'll fix it. I had been up for 2 or 3 days working on my kernel source when I posted these. Everyone has their ways... I just found the more thorough my answer the less questions I g – SudoSURoot Feb 14 '15 at 22:40
  • The latest package is gcc-7 ATM – Elder Geek Nov 29 '16 at 17:50
9

Ubuntu 16.04 and later

You can already install gcc 7.0 in Ubuntu 18.04 from the default repositories. To install gcc-7 in Ubuntu 17.10, 18.04 and 18.10 open the terminal and type:

sudo apt install gcc-7 

To install gcc-8 in Ubuntu 18.04 and later open the terminal and type:

sudo apt install gcc-8

To install gcc-9 in Ubuntu 19.04 and later open the terminal and type:

sudo apt install gcc-9

You can install gcc-7 in Ubuntu 16.04 from ppa:jonathonf/gcc-7.1.

sudo add-apt-repository ppa:jonathonf/gcc-7.1  
sudo apt update  
sudo apt install gcc-7  

You can install gcc-8 in Ubuntu 16.04 from ppa:jonathonf/gcc-8.0.

sudo add-apt-repository ppa:jonathonf/gcc-8.0  
sudo apt update  
sudo apt install gcc-8  

Multiple versions of gcc can be installed alongside each other. You can change the default gcc version by using the update-alternatives command to determine which actual file is referenced by a generic name, for example which actual file is referenced by gcc. For more information see the answers to this question: How to change the default GCC compiler in Ubuntu?.

karel
  • 114,770
4

To call gcc 4.9 specifically, use gcc-4.9 at the command prompt.

All the gcc versions you have installed can be called individually by adding a hyphen and the version number at the end of gcc. In your case, gcc-4.8 and gcc-4.9 should be available. In a terminal, type gcc- (note the hyphen) and the push tab twice to see if there are any other versions installed.

Note that the default gcc is likely still 4.8. (Use gcc -v to verify this.) Unfortunately changing the default is not trivial if you installed gcc-4.9 from the default repository as it did not add a update-alternatives entry. If you are interested in how to change the default, see answers to this this question.

  • How do I make a project relying on makefile and possibly other build infrastructure build with gcc-4.9 instead of gcc? – Nickolai Leschov Jul 13 '14 at 13:41
  • 1
    The best way to make sure gcc-4.9 is used across build systems is to change the default gcc. Follow the link I provided in my answer for how to do this. Alternatively you could create a symbolic link, though I recommend against this except as last resort since the package manager might complain when updating gcc. If you insist: sudo rm /usr/bin/gcc; sudo ln -s /usr/bin/gcc-4.9 /usr/bin/gcc. To revert to gcc-4.8 default, just replace gcc-4.9 with gcc-4.8. You may have to do this with g++ and other GNU compilers as well. – holocronweaver Jul 13 '14 at 20:29
  • GCC 4.9 is not available in Ubuntu 14.04 (except for the GCC Go compiler), so while this answer will probably work in Ubuntu 14.10, it doesn't help now. :-) – Søren Løvborg Aug 02 '14 at 17:43
  • Actually it is available in Ubuntu 14.04, though it may have been a mistake: http://packages.ubuntu.com/trusty/gcc-4.9-base. I have been using it since April. – holocronweaver Aug 03 '14 at 11:46