背景:
在Ubuntu20.04 LTS上,需要使用gcc和g++编译器编译程序,在安装build-essential时报错。如下:
acl@ubuntu:~/test$ sudo apt install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
build-essential : Depends: libc6-dev but it is not going to be installed or
libc-dev
Depends: g++ (>= 4:7.2) but it is not going to be installed
Depends: dpkg-dev (>= 1.17.11) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
初步猜测是源的问题,因此将源换成阿里的源,具体步骤如下:
1 备份原来的源
cp -ra /etc/apt/sources.list /etc/apt/sources.list.bak
2 将源换成阿里源
sudo gedit /etc/apt/sources.list
然后将里面的内容清空,把下面的内容复制进去,保存退出:
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
3 更新缓存和升级
sudo apt-get update
sudo apt-get upgrade
此时阿里源已经换好。
再次安装build-essential,就会发现能安装成功了。
测试gcc和g++是否安装成功:
gcc -v
g++ -v