Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save application2000/73fd6f4bf1be6600a2cf9f56315a2d91 to your computer and use it in GitHub Desktop.
Save application2000/73fd6f4bf1be6600a2cf9f56315a2d91 to your computer and use it in GitHub Desktop.
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04)
These commands are based on a askubuntu answer http://askubuntu.com/a/581497
To install gcc-6 (gcc-6.1.1), I had to do more stuff as shown below.
USE THOSE COMMANDS AT YOUR OWN RISK. I SHALL NOT BE RESPONSIBLE FOR ANYTHING.
ABSOLUTELY NO WARRANTY.
If you are still reading let's carry on with the code.
sudo apt-get update && \
sudo apt-get install build-essential software-properties-common -y && \
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
sudo apt-get update && \
sudo apt-get install gcc-snapshot -y && \
sudo apt-get update && \
sudo apt-get install gcc-6 g++-6 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 && \
sudo apt-get install gcc-4.8 g++-4.8 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8;
When completed, you must change to the gcc you want to work with by default. Type in your terminal:
sudo update-alternatives --config gcc
To verify if it worked. Just type in your terminal
gcc -v
If everything went fine you should see gcc 6.1.1 by the time I am writing this gist
Happy coding!
See my blog post at https://www.application2000.com
@kiril-kirov
Copy link

kiril-kirov commented Mar 25, 2021

gcc-9[-multilib] and g++-9[-multilib] are also available for Ubuntu 12.04 LTS!

I extended the steps above, as they complain with g++ can't be slave of gcc + some additions and beautifications:

#!/bin/bash

GCC_GXX_VERSION=9
gcc_alternatives=(gcc cc)
gxx_alternatives=(g++ c++ cpp)
all_alternatives=("${gcc_alternatives[@]}" "${gxx_alternatives[@]}")

remove_alternatives() {
    alternatives=("$@")
    for alt in "${alternatives[@]}"; do
        sudo update-alternatives --remove-all $alt || continue
    done
}

install_and_set_alternatives() {
    path=$1
    shift
    alternatives=("$@")
    for alt in "${alternatives[@]}"; do
        sudo update-alternatives --install /usr/bin/$alt $alt $path 100
        sudo update-alternatives --set $alt $path
    done
}

print_green() {
    args=$@
    echo -e "\033[0;32m$args\033[0m"
}

check_versions() {
    alternatives=("$@")
    for alt in "${alternatives[@]}"; do
        print_green "$alt\t->\t`$alt --version | head -n 1`"
    done
}

install_gcc_gxx() {
    sudo apt-get update -y &&
    sudo apt-get upgrade -y &&
    sudo apt-get dist-upgrade -y &&
    sudo apt-get install build-essential software-properties-common -y &&
    sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y &&
    sudo apt-get update -y &&
    sudo apt-get install gcc-$GCC_GXX_VERSION g++-$GCC_GXX_VERSION -y
}

install_gcc_gxx
remove_alternatives "${all_alternatives[@]}"
install_and_set_alternatives "/usr/bin/gcc-$GCC_GXX_VERSION" "${gcc_alternatives[@]}"
install_and_set_alternatives "/usr/bin/g++-$GCC_GXX_VERSION" "${gxx_alternatives[@]}"

echo
echo "Check the following versions:"
check_versions "${all_alternatives[@]}"

@ivanmara555
Copy link

GCC 11.x on Ubuntu 18.04 & 20.04:

sudo apt-get update -y &&
sudo apt-get upgrade -y &&
sudo apt-get dist-upgrade -y &&
sudo apt-get install build-essential software-properties-common -y &&
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y &&
sudo apt-get update -y &&
sudo apt-get install gcc-11 g++-11 -y &&
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 60 --slave /usr/bin/g++ g++ /usr/bin/g++-11 &&
sudo update-alternatives --config gcc

select gcc-11

@AndiHairy97
Copy link

ami@ami:~$ sudo apt-get update && \

sudo apt-get install build-essential software-properties-common -y &&
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y &&
sudo apt-get update &&
sudo apt-get install gcc-snapshot -y &&
sudo apt-get update &&
sudo apt-get install gcc-6 g++-6 -y &&
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-6 &&
sudo apt-get install gcc-4.8 g++-4.8 -y &&
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8;
[sudo] password for ami:
Get:1 http://dl.google.com/linux/earth/deb stable InRelease [1,807 B]
Ign:1 http://dl.google.com/linux/earth/deb stable InRelease
Hit:2 http://ubuntu.mirror.myduniahost.com/ubuntu xenial InRelease
Hit:3 http://ubuntu.mirror.myduniahost.com/ubuntu xenial-updates InRelease
Hit:4 http://ubuntu.mirror.myduniahost.com/ubuntu xenial-backports InRelease
Get:5 https://linux.teamviewer.com/deb stable InRelease [11.9 kB]
Hit:6 http://ubuntu.mirror.myduniahost.com/ubuntu xenial-security InRelease
Get:7 https://linux.teamviewer.com/deb stable/main amd64 Packages [11.2 kB]
Get:8 https://linux.teamviewer.com/deb stable/main i386 Packages [11.0 kB]
Get:9 http://deb.anydesk.com all InRelease [5,588 B]
Fetched 41.5 kB in 1s (30.4 kB/s)
Reading package lists... Done
W: GPG error: http://dl.google.com/linux/earth/deb stable InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 78BD65473CB3BD13
W: The repository 'http://dl.google.com/linux/earth/deb stable InRelease' is not signed.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
N: Skipping acquire of configured file 'main/binary-i386/Packages' as repository 'http://dl.google.com/linux/earth/deb stable InRelease' doesn't support architecture 'i386'
Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
software-properties-common is already the newest version (0.96.20.10).
software-properties-common set to manually installed.
The following additional packages will be installed:
gcc-doc
The following packages will be upgraded:
gcc-doc
1 upgraded, 0 newly installed, 0 to remove and 84 not upgraded.
1 not fully installed or removed.
Need to get 0 B/3,448 B of archives.
After this operation, 10.2 kB disk space will be freed.
(Reading database ... 864713 files and directories currently installed.)
Preparing to unpack .../gcc-doc_4%3a5.3.1-1ubuntu1_amd64.deb ...
install-info: No dir file specified; try --help for more information.
dpkg: warning: subprocess old pre-removal script returned error exit status 1
dpkg: trying script from the new package instead ...
dpkg: error processing archive /var/cache/apt/archives/gcc-doc_4%3a5.3.1-1ubuntu1_amd64.deb (--unpack):
there is no script in the new version of the package - giving up
install-info: No dir file specified; try --help for more information.
dpkg: error while cleaning up:
subprocess installed post-installation script returned error exit status 1
Errors were encountered while processing:
/var/cache/apt/archives/gcc-doc_4%3a5.3.1-1ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

please help me? im facing this problem for a very llong time and untill now it still not fixed

@clarkdunson
Copy link

Thank you very much!

@YJ-20
Copy link

YJ-20 commented Aug 25, 2021

good!

@pachiras
Copy link

Thank you! You saved me!

@thehappyitguy
Copy link

Thank you!
gcc version 9.4.0

@PierrickPochelu
Copy link

Thanks @peterhanneman

GCC 7.1 on Ubuntu 14.04 & 16.04:

sudo apt-get update -y && \
sudo apt-get upgrade -y && \
sudo apt-get dist-upgrade -y && \
sudo apt-get install build-essential software-properties-common -y && \
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
sudo apt-get update -y && \
sudo apt-get install gcc-7 g++-7 -y && \
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7 && \
sudo update-alternatives --config gcc

I got gcc/g++ version 11.1.0 with similar procedure on 18.04 . Thank you!

@sinugowde
Copy link

sinugowde commented May 16, 2023

Hi All,
I am trying to install GCC12 on ubuntu 20.04.6 LTS (Focal Fossa) os but not able to find GCC12. tried to use the above command but with gcc-12.
can anybody provide any suggestions?

@jasonbeach
Copy link

Hi All, I am trying to install GCC12 on ubuntu 20.04.6 LTS (Focal Fossa) os but not able to find GCC12. tried to use the above command but with gcc-12. can anybody provide any suggestions?

I actually just tried this and it appears that gcc12 and 13 are only available as a package on Ubuntu 22.04 and later. If you want it on earlier versions you'll have to build it from source. 😭

@sinugowde
Copy link

Thanks @jasonbeach for your inputs..

@Shaiken
Copy link

Shaiken commented Jun 2, 2023

thank you!

@Pomax
Copy link

Pomax commented Oct 25, 2024

this still doesn't explain how to install gcc/g++10 on ubuntu 16.04.7, which some folks are still not by choice forced to work with.

@alexandreelise
Copy link

@Pomax. Your account seems to behave like a bot. Since I unsure. I'll ignore your comments from now on. If you are not a bot. The real answer to what you want is trial and error until you succeed. Struggles is part of the journey. Instant answer on a plate will not help you learn. Have a delightful day (bot)?

@jasonbeach
Copy link

this still doesn't explain how to install gcc/g++10 on ubuntu 16.04.7, which some folks are still not by choice forced to work with.

Heh...apparently the bot doesn't get the sadistic humor of still being forced to work on an O.S. that stopped being supported 3yrs ago.

Regardless, it appears there is no prebuilt package for gcc10 on 16.04 which means if you want it you'll have to build it from source. Clang is surprisingly easy to build from source but I have no idea about GCC.

@Pomax
Copy link

Pomax commented Oct 25, 2024

@alexandreelise weird thing to say based on two comments on related gist from someone with multiple 1500+ star repos, but okay? I found this gist while looking for info on how to install the latest GCC on Ubuntu 16, so past version 9, which is what the other gist I commented on was for. Of course, I had been hoping someone had posted a copy-paste solution for gcc 10 already, but that was unfortunately not the case. I might add a comment with instructions if I can make some of the stackoverflow posts I managed to find work.

As for @jasonbeach's sadism remark, that's unfortunately far too real: I'm forced to work on a project that's still stuck on Ubuntu 16.04.7 (and not even kernel 4.15, but 4.4). I wish it wasn't, reality is not always so kind. There are way too many projects still stuck on 8 year old code bases running on even older infrastructure. I'm just trying to find anything that will let me get these projects migrated off of these dead versions of ubuntu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment