183

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
  • 207,228

5 Answers5

239

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,849
  • 2
  • 15
  • 16
62

Ultimate mega master compatibility table

OK let's do this:

                  GCC                              clang
        +----------------------------------+-------------------------------------------------+
        | 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.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 |                                                 |
+-------+----------------------------------+-------------------------------------------------+

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

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
  • 207,228
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
  • 122,292
  • 133
  • 301
  • 332
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.