rrz0
rrz0

Reputation: 2302

How to use a newer version of gcc on ubuntu?

I am running ubuntu 18.04 and I currently have:

(tensorflow_p36) ubuntu@user:~$ gcc --version
gcc (GCC) 4.8.5

(tensorflow_p36) ubuntu@user:~$ gcc-8 --version
gcc-8 (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0

(tensorflow_p36) ubuntu@user:~$ which gcc
/home/ubuntu/anaconda3/envs/tensorflow_p36/bin/gcc

I am trying to use the latest gcc-8 as show here. However when I run

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8

I get the following error:

update-alternatives: error: alternative g++ can't be slave of gcc: it is a master alternative

When I try:

(tensorflow_p36) ubuntu@user:~$ sudo update-alternatives --config gcc

I get another error:

update-alternatives: error: no alternatives for gcc

How is it that I do not have alternative for gcc? Any suggestions on how I can resolve this error and configure gcc-8 to be the default gcc installation would be appreciated.


Running which gcc from outside the conda environment does not return anything:

ubuntu@user:~$ which gcc
ubuntu@user:~$

but

ubuntu@user:~$ gcc-8 --version
gcc-8 (Ubuntu 8.3.0-6ubuntu1~18.04.1) 8.3.0

Update. I'm trying the following as per the helpful suggestion below, but still to no avail.

(tensorflow_p36) ubuntu@user:~$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 40
(tensorflow_p36) ubuntu@user:~$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 60
(tensorflow_p36) ubuntu@user:~$ sudo update-alternatives --config g++
There are 3 choices for the alternative g++ (providing /usr/bin/g++).

  Selection    Path                             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/x86_64-linux-gnu-g++-7   100       auto mode
  1            /usr/bin/g++-4.8                  60        manual mode
  2            /usr/bin/g++-8                    40        manual mode
  3            /usr/bin/x86_64-linux-gnu-g++-7   100       manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/g++-8 to provide /usr/bin/g++ (g++) in manual mode

So far so good, but when I test the g++ version, it's still the same!

~$ g++ --version
g++ (GCC) 4.8.5

Upvotes: 6

Views: 13978

Answers (2)

smac89
smac89

Reputation: 43098

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

  1. https://askubuntu.com/a/26518/145907
  2. https://askubuntu.com/a/1206264/145907

Upvotes: 0

johndpope
johndpope

Reputation: 5249

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.

Upvotes: 2

Related Questions