Skip to content

Ubuntu: live with multiple versions of gcc etc.

Wenliang ZHANG edited this page Jun 11, 2021 · 5 revisions

In the old good days, *Nix users tend to use chroot to deploy mutiple verions of development tools GCC with chroot. And Red Hat had made the life much easier with devtools. But Ubuntu has its own solution.

Read first

How to choose the default gcc version

Take action

  1. Remove the alternatives
    for x in gcc g++ x86_64-linux-gnu-gcc x86_64-linux-gnu-g++ gcc-ar gcc-nm gcc-ranlib gcov gcov-dump gcov-tool
    do
      sudo update-alternatives --remove-all $x
    done
        
  2. Install all of the versions
    sudo apt-get install gcc-4.8 gcc-5 gcc-7 gcc-6 gcc-8 gcc-9 g++-4.8 g++-5 g++-6 g++-7 g++-8 g++-9
        
  3. Update the alternatives
    for x in gcc g++ x86_64-linux-gnu-gcc x86_64-linux-gnu-g++ gcc-ar gcc-nm gcov gcov-dump gcov-tool
    do
      priority=0
      for version in 4.8 5 6 7 8 9
      do
        priority=$(($priority + 10))
        sudo update-alternatives --install /usr/bin/$x $x /usr/bin/$x-$version $priority
      done
    done
    
    sudo update-alternatives --install /usr/bin/ranlib ranlib /usr/bin/x86_64-linux-gnu-ranlib 10
        
  4. Change the default gcc/g++ when needed:
    for x in gcc g++ x86_64-linux-gnu-gcc x86_64-linux-gnu-g++ gcc-ar gcc-nm gcov gcov-dump gcov-tool
    do
      sudo update-alternatives --config $x
    done
        
  5. Notes

    Not found: gcc-ar-4, gcc-nm-4, gcov-dump-4, gcov-tool-4.

    gcc-ar, gcc-ar-5, gcc-ar-7 are 2.26.1
    gcc-nm, gcc-nm-4, gcc-nm-7 are 2.26.1.
    gcov-dump-5, gcov-dump-7 are 5 and 7.
    gcov-tool-5, gcov-tool-7 are 5 and 7.
        

Other commands

The same logic can be applied to other software like bison, clang etc.

Clone this wiki locally