How to install a specific version of g++ or gcc on Ubuntu/Linux

  • Updated : 23-06-2024 12:58
  • By : Phosphataz

  • How to install g++/gcc on ubuntu ?


    To install the gnu compiler collection open the terminal (ctrl + alt + t) and execute this command :

    apt install build-essentials
    


    This will install the default version of the compiler (g++ and gcc and other ) on your system. To verify the version installed, execute this command :

    # for g++ 
    g++ --version
    
    # for gcc
    gcc --version
    


    Install the specific version


    When you install the gnu compiler collection the version installed may not be the latest version or specific version you want.

    To install the specific version you want :

    1 - add the ubuntu toolchain PPA

    sudo add-apt-repository ppa:ubuntu-toolchain-r/test
    


    2 - update your package list

    sudo apt update
    


    3 - install the version you want :

    NB : replace the number 13 by the version you want

    # for g++ 
    sudo apt install g++-13
    
    # for gcc
    sudo apt install gcc-13
    


    4 - Run this command to set it as default version :

    NB : replace the number 13 by the version you want

    # for g++
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 100
    sudo update-alternatives --config g++
    
    # for gcc
    sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100
    sudo update-alternatives --config gcc
    


    5 - verify the installation version one more time :

    # for g++ 
    g++ --version
    
    # for gcc
    gcc --version