Linux允许多个版本的gcc共存,当你需要安装多个版本的gcc的时候,比如同时安装gcc-7和gcc-8,你可以通过update-alternatives来重定位你想使用的gcc版本。
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /usr/bin/g++ g++ /usr/bin/g++-7sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8
如果你想改变使用的版本,那就运行一下这条命令吧,
sudo update-alternatives --config gcc
通常这个命令会给出一些提示,
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).Selection Path Priority Status------------------------------------------------------------* 0 /usr/bin/gcc-8 800 auto mode1 /usr/bin/gcc-7 700 manual mode2 /usr/bin/gcc-8 800 manual modePress <enter> to keep the current choice[*], or type selection number:
然后根据提示操作就可以了。
想要更多的信息,你可以搜索了解update-alternatives的具体用法,例如,
linux软件版本管理命令update-alternatives使用详解_hellocsz的博客-CSDN博客_linux软件版本管理
不建议卸载默认的gcc,尤其是你的程序依赖build-essentials的时候。
另外一个方法,就是你必须选择你熟悉的包来安装,比如你想安装gcc-9,或者是其子版本比如gcc-9.3.0,那么你可以先apt查找一下,
~$ apt search gcc | grep 9.3.0WARNING: apt does not have a stable CLI interface. Use with caution in scripts.cpp/focal,now 4:9.3.0-1ubuntu2 amd64 [已安装,自动]cpp-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64cpp-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64cpp-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64cpp-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386cpp-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64g++-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allg++-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64g++-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gcc/focal 4:9.3.0-1ubuntu2 amd64gcc-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gcc-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gcc-doc/focal 4:9.3.0-1ubuntu2 amd64gcc-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-hppa64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allgcc-mingw-w64-base/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gcc-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gcc-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gcc-multilib/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gcc-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gcc-offload-nvptx/focal 4:9.3.0-1ubuntu2 amd64gcc-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-riscv64-unknown-elf/focal 9.3.0-0ubuntu1 amd64gcc-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gcc-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gcc-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gccbrig/focal 4:9.3.0-1ubuntu2 amd64gfortran/focal 4:9.3.0-1ubuntu2 amd64gfortran-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gfortran-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gfortran-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allgfortran-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gfortran-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gfortran-multilib/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gfortran-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gfortran-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gfortran-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gfortran-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gnat-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allgnat-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gnat-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gobjc/focal 4:9.3.0-1ubuntu2 amd64gobjc++/focal 4:9.3.0-1ubuntu2 amd64gobjc++-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gobjc++-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gobjc++-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allgobjc++-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gobjc++-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gobjc++-multilib/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gobjc++-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gobjc++-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc++-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gobjc++-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gobjc-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gobjc-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gobjc-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 allgobjc-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gobjc-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64gobjc-multilib/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gobjc-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64gobjc-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64gobjc-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386gobjc-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
找出这么一大堆的lib之后, 你就可以挑选你自己想要安装的包用"sudo apt install"安装了,例如,
sudo apt install gcc-x86-64-linux-gnu