Ubuntu不同版本默认apt install gcc安装的gcc和g++版本不同,如Ubuntu22.04默认安装gcc/g++为11版本,高版本Ubuntu无法直接通过apt install gcc安装低版本编译器,可以通过离线安装(过于繁琐),本文推荐修改apt源,添加对应低版本ubuntu源即可,这里以Ubuntu22.04为例,给出安装gcc-5/gcc-7的简单方法,同时给出了常用的国内镜像源。
apt install gcc的会同时安装gcc和g++
1. 首先简单介绍常用国内apt源-->阿里云和清华云(此步骤只是参考可略过)
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
## Pre-released source, not recommended.
# deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
## Pre-released source, not recommended.
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
上面源中每一行中的jammy标识为ubuntu22.04,若为其他版本Ubuntu,将对应jammy改为其他版本标识,如focal即可,常用的Ubuntu版本代号如下:
Ubuntu 22.04:jammy
Ubuntu 20.04:focal
Ubuntu 18.04:bionic
Ubuntu 16.04:xenial
2. Ubuntu22.04默认安装gcc-11版本,如果需要安装低版本,可以修改apt源,添加低版本ubuntu源来安装
使用下面命令修改apt源
sudo vim /etc/apt/sources.list
如要安装gcc-7,在sources.list文件末尾添加Ubuntu 20.04:focal的源
deb https://mirrors.aliyun.com/ubuntu/ focal main universe
如要安装gcc-5,在sources.list文件末尾添加Ubuntu 16.04:focal的源
deb https://mirrors.aliyun.com/ubuntu/ xenial main universe
sources.list文件修改完毕后,执行以下命令加载源资源
sudo apt-get update
注意,有时update时会报错, 提示GPG error缺少公钥,多见于添加了xenial版本(Ubuntu16)源
W: GPG error: http://mirrors.aliyun.com/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32
E: The repository 'http://mirrors.aliyun.com/ubuntu xenial InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
此时,解决办法根据报错信息“NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32”导入对应公钥即可
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
然后,再次sudo apt-get update,之后就可以直接apt安装gcc对应版本
sudo apt install gcc-5
sudo apt install gcc-7