61

I just installed Lubuntu 18.04 LTS. There are two options for installing gcc: gcc-7 and gcc-8. Both are available from apt-get install out of the box.

Even after I installed gcc-8, the system is still going to install gcc-7 when I install other packages such as build-essential.

Since gcc-8 is newer, is there a way to make it install gcc-8 and gcc-8 only?

3
  • 1
    Just don't install build-essential but the other packages it depend on directly? dpkg-dev g++-8 gcc-8 libc6-dev libc-dev make
    – muru
    Apr 27, 2018 at 5:41
  • gcc-7 is required by gcc-8. Note : You can have as many gcc/g++ versions as you want, installed at the same time. Bionic extras : g++-4.8 g++-5 g++-6 g++-8 ... ... g++-7/gcc-7 is the system compiler. The older versions are required for building some older applications. Apr 27, 2018 at 9:40
  • 1
    @KnudLarsen "gcc-7 is required by gcc-8" what?
    – ZachB
    Sep 25, 2018 at 17:47

3 Answers 3

93

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.

1
  • this should be the accepted answer
    – Void Star
    Oct 22, 2020 at 22:46
23

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

4
  • 1
    This doesn't answer the question. This information could be relevant for a completely separate question, but that one already does have the answer: askubuntu.com/q/466651/113421.
    – Ruslan
    May 22, 2019 at 16:38
  • 2
    @Ruslan thanks for feedback. Helping 100x Googlers is more important than helping 1 OP ;-) May 22, 2019 at 17:10
  • 1
    @CiroSantilli冠状病毒审查六四事件法轮功 Thank you! Saved my day... and Nice Profile picture btw :P May 2, 2020 at 10:09
  • @user1241241 thanks! I'm glad you like my discreet profile. May 2, 2020 at 15:45
0

Go download unversioned gcc packages (cpp/gcc/g++/g++multilib/etc.) with package version 8, and install them using dpkg -i *.deb. Make sure you have the corresponding packages with -8 suffix installed first.

http://deb.debian.org/debian/pool/main/g/gcc-defaults/

Reason:

build-essential and a lot of other packages depend on unversioned packages (no version number in package name) such as gcc, g++, etc., and those unversioned packages depend on versioned packages (whose package name contains a version as suffix) such as gcc-X, g++-X, respectively.

A unversioned package gcc with package version 8.~ depends on gcc-8, whereas the gcc package in offcial Ubuntu 18.04 repository comes with package version 7.~, which in turn depends on gcc-7.

Those unversioned gcc packages install nothing, just act as dependency declaration to the versioned gcc packages.
The unversioned packages from Debian 10 (codename buster) are versioned with 8.~. You can safely manually install them and the dependent versioned packages are still from your official ubuntu repository, so it's totally safe to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.