高版本Ubuntu安装低版本gcc

高版本Ubuntu安装低版本gcc

Ubuntu不同版本默认apt install gcc安装的gcc和g++版本不同,高版本Ubuntu无法直接通过apt install gcc安装低版本编译器,需要修改apt源,这里以Ubuntu22.04为例,给出安装gcc-5/gcc-7的简单方法

1. 首先需要简单介绍常用国内apt源-->阿里云和清华云(此步骤只是参考可略过)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

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

AWK

上面源中jammy标识为ubuntu22.04,若为其他版本Ubuntu,将对应jammy改为其他版本标识,如focal即可,常用的Ubuntu版本代号如下:

1
2
3
4
Ubuntu 22.04:jammy
Ubuntu 20.04:focal
Ubuntu 18.04:bionic
Ubuntu 16.04:xenial
APACHE

2. Ubuntu22.04默认安装gcc-11版本,如果需要安装低版本,可以修改apt源,添加低版本ubuntu源来安装

使用下面命令修改apt源

1
sudo vim /etc/apt/sources.list
SHELL

如要安装gcc-7,在sources.list文件末尾添加Ubuntu 20.04:focal的源

1
deb https://mirrors.aliyun.com/ubuntu/ focal main universe
AWK

如要安装gcc-5,在sources.list文件末尾添加Ubuntu 16.04:focal的源

1
deb https://mirrors.aliyun.com/ubuntu/ xenial main universe
AWK

sources.list文件修改完毕后,执行以下命令加载源

1
sudo apt-get update
PGSQL

注意,有时update时会报错, 提示GPG error缺少公钥,多见于添加了xenial版本源

1
2
3
4
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.
PGSQL

此时,解决办法根据报错信息“NO_PUBKEY 40976EAF437D05B5 NO_PUBKEY 3B4FE6ACC0B21F32”导入对应公钥即可

1
2
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
STYLUS

然后,再次sudo apt-get update,之后就可以直接apt安装gcc对应版本

1
2
sudo apt install gcc-5
sudo apt install gcc-7
APACHE

高版本Ubuntu安装低版本gcc
https://izhuhaoran.github.io/2023/01/23/Linux_Note/高版本Ubuntu安装低版本gcc/
作者
zhuhr
发布于
2023年1月23日
许可协议