Linux中如何安装特定的gcc版本

  • Post author:
  • Post category:linux


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++-7
sudo 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 mode
  1            /usr/bin/gcc-7   700       manual mode
  2            /usr/bin/gcc-8   800       manual mode

Press <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.0

WARNING: 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 amd64
cpp-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
cpp-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
cpp-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
cpp-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
cpp-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
g++-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
g++-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
g++-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gcc/focal 4:9.3.0-1ubuntu2 amd64
gcc-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gcc-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gcc-doc/focal 4:9.3.0-1ubuntu2 amd64
gcc-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-hppa64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
gcc-mingw-w64-base/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gcc-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gcc-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gcc-multilib/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gcc-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gcc-offload-nvptx/focal 4:9.3.0-1ubuntu2 amd64
gcc-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-riscv64-unknown-elf/focal 9.3.0-0ubuntu1 amd64
gcc-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gcc-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gcc-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gccbrig/focal 4:9.3.0-1ubuntu2 amd64
gfortran/focal 4:9.3.0-1ubuntu2 amd64
gfortran-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gfortran-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gfortran-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
gfortran-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gfortran-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gfortran-multilib/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gfortran-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gfortran-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gfortran-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gfortran-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gnat-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
gnat-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gnat-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gobjc/focal 4:9.3.0-1ubuntu2 amd64
gobjc++/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
gobjc++-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gobjc++-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gobjc++-multilib/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gobjc++-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc++-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gobjc++-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gobjc-aarch64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-alpha-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gobjc-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gobjc-hppa-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-m68k-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-mingw-w64/focal,focal 9.3.0-7ubuntu1+22~exp1ubuntu4 all
gobjc-mingw-w64-i686/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gobjc-mingw-w64-x86-64/focal 9.3.0-7ubuntu1+22~exp1ubuntu4 amd64
gobjc-multilib/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-arm-linux-gnueabi/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-arm-linux-gnueabihf/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-i686-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-multilib-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gobjc-multilib-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64
gobjc-powerpc-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-powerpc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-powerpc64le-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-riscv64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-s390x-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-sh4-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-sparc64-linux-gnu/focal 4:9.3.0-1ubuntu2 amd64
gobjc-x86-64-linux-gnu/focal 4:9.3.0-1ubuntu2 i386
gobjc-x86-64-linux-gnux32/focal 4:9.3.0-1ubuntu2 amd64

找出这么一大堆的lib之后, 你就可以挑选你自己想要安装的包用”sudo apt install”安装了,例如,

sudo apt install gcc-x86-64-linux-gnu



版权声明:本文为tanmx219原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。