others-How to solve `you have held broken packages` problem in ubuntu system?

1. Purpose

In this post, I would demo how to solve the following error or problem when trying to install packages in ubuntu system:

The following packages have unmet dependencies:
 libgd-dev : Depends: libxpm-dev but it is not going to be installed
             Depends: libx11-dev but it is not going to be installed
             Depends: libxt-dev but it is not going to be installed
E: Unable to correct problems, you have held broken packages.



2. The solution

2.1 Already tried but not working solutions

I have already tried following solutions, but they are not working at all:

First, I tried to update all packages:

sudo apt-get -f install
sudo apt-get update
sudo apt-get upgrade

Then I tried update and clean/remove:

sudo apt-get update
sudo apt-get clean
sudo apt-get autoremove

And then I tried to fix broken packages:

sudo apt --fix-broken install
sudo apt-get update && sudo apt-get upgrade
sudo dpkg --configure -a
sudo apt-get install -f

2.2 The working solution

I tried with aptitude. First ,install it:

apt install aptitude

Then use it to install packages:

sudo aptitude install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev

I got this result:

✘ ⚡ [email protected]  ~  sudo aptitude install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev
Warning: Invalid locale (please review locale settings, this might lead to problems later):
  locale::facet::_S_create_c_locale name not valid
build-essential is already installed at the requested version (12.8ubuntu1.1)
libpcre3 is already installed at the requested version (2:8.39-12build1)
zlib1g is already installed at the requested version (1:1.2.11.dfsg-2ubuntu1.2)
zlib1g-dev is already installed at the requested version (1:1.2.11.dfsg-2ubuntu1.2)
libssl-dev is already installed at the requested version (1.1.1f-1ubuntu2.10)
libxml2 is already installed at the requested version (2.9.10+dfsg-5ubuntu0.20.04.1)
libxml2-dev is already installed at the requested version (2.9.10+dfsg-5ubuntu0.20.04.1)
build-essential is already installed at the requested version (12.8ubuntu1.1)
libpcre3 is already installed at the requested version (2:8.39-12build1)
zlib1g is already installed at the requested version (1:1.2.11.dfsg-2ubuntu1.2)
zlib1g-dev is already installed at the requested version (1:1.2.11.dfsg-2ubuntu1.2)
libssl-dev is already installed at the requested version (1.1.1f-1ubuntu2.10)
libxml2 is already installed at the requested version (2.9.10+dfsg-5ubuntu0.20.04.1)
libxml2-dev is already installed at the requested version (2.9.10+dfsg-5ubuntu0.20.04.1)
The following NEW packages will be installed:
  fontconfig-config{a} fonts-dejavu-core{a} libfontconfig1{a} libfontconfig1-dev{a} libfreetype-dev{a} libfreetype6-dev{a} libgd-dev libgd3{a} libice-dev{a} libice6{a} libjbig-dev{a} libjbig0{a} libjpeg-dev{a} libjpeg-turbo8{a} libjpeg-turbo8-dev{a} libjpeg8{a} libjpeg8-dev{a}
  libpcre16-3{a} libpcre3-dev libpcre32-3{a} libpcrecpp0v5{a} libpng-dev{a} libpng-tools{a} libpthread-stubs0-dev{a} libsm-dev{a} libsm6{a} libtiff-dev{a} libtiff5{a} libtiffxx5{a} libvpx-dev{a} libvpx6{a} libwebp6{a} libx11-dev{ab} libxau-dev{a} libxcb1-dev{a} libxdmcp-dev{a}
  libxpm-dev{a} libxpm4{a} libxt-dev{a} libxt6{a} pkg-config{a} uuid-dev x11-common{a} x11proto-core-dev{a} x11proto-dev{a} xorg-sgml-doctools{a} xtrans-dev{a}
0 packages upgraded, 47 newly installed, 0 to remove and 0 not upgraded.
Need to get 8987 kB of archives. After unpacking 32.2 MB will be used.
The following packages have unmet dependencies:
 libx11-dev : Depends: libx11-6 (= 2:1.6.9-2ubuntu1.2) but 2:1.6.9-2ubuntu1.3 is installed
The following actions will resolve these dependencies:

     Keep the following packages at their current version:
1)     libgd-dev [Not Installed]
2)     libx11-dev [Not Installed]
3)     libxpm-dev [Not Installed]
4)     libxt-dev [Not Installed]



Accept this solution? [Y/n/q/?] Y
The following NEW packages will be installed:
  libpcre16-3{a} libpcre3-dev libpcre32-3{a} libpcrecpp0v5{a} pkg-config{a} uuid-dev
0 packages upgraded, 6 newly installed, 0 to remove and 0 not upgraded.
Need to get 925 kB of archives. After unpacking 3952 kB will be used.
Do you want to continue? [Y/n/?] Y
Get: 1 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 libpcre16-3 amd64 2:8.39-12build1 [150 kB]
Get: 2 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 libpcre32-3 amd64 2:8.39-12build1 [140 kB]
Get: 3 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 libpcrecpp0v5 amd64 2:8.39-12build1 [15.5 kB]
Get: 4 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 libpcre3-dev amd64 2:8.39-12build1 [540 kB]
Get: 5 http://mirrors.cloud.aliyuncs.com/ubuntu focal/main amd64 pkg-config amd64 0.29.1-0ubuntu4 [45.5 kB]
Get: 6 http://mirrors.cloud.aliyuncs.com/ubuntu focal-updates/main amd64 uuid-dev amd64 2.34-0.1ubuntu9.1 [33.6 kB]
Fetched 925 kB in 0s (2446 kB/s)
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
	LANGUAGE = (unset),
	LC_ALL = (unset),
	LC_CTYPE = "UTF-8",
	LC_TERMINAL = "iTerm2",
	LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
Selecting previously unselected package libpcre16-3:amd64.
(Reading database ... 120577 files and directories currently installed.)
Preparing to unpack .../0-libpcre16-3_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre16-3:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcre32-3:amd64.
Preparing to unpack .../1-libpcre32-3_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre32-3:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcrecpp0v5:amd64.
Preparing to unpack .../2-libpcrecpp0v5_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcrecpp0v5:amd64 (2:8.39-12build1) ...
Selecting previously unselected package libpcre3-dev:amd64.
Preparing to unpack .../3-libpcre3-dev_2%3a8.39-12build1_amd64.deb ...
Unpacking libpcre3-dev:amd64 (2:8.39-12build1) ...
Selecting previously unselected package pkg-config.
Preparing to unpack .../4-pkg-config_0.29.1-0ubuntu4_amd64.deb ...
Unpacking pkg-config (0.29.1-0ubuntu4) ...
Selecting previously unselected package uuid-dev:amd64.
Preparing to unpack .../5-uuid-dev_2.34-0.1ubuntu9.1_amd64.deb ...
Unpacking uuid-dev:amd64 (2.34-0.1ubuntu9.1) ...
Setting up libpcrecpp0v5:amd64 (2:8.39-12build1) ...
Setting up libpcre16-3:amd64 (2:8.39-12build1) ...
Setting up uuid-dev:amd64 (2.34-0.1ubuntu9.1) ...
Setting up libpcre32-3:amd64 (2:8.39-12build1) ...
Setting up pkg-config (0.29.1-0ubuntu4) ...
Setting up libpcre3-dev:amd64 (2:8.39-12build1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.4) ...

It works!

2.3 What is aptitude?

According to this document:

aptitude is a text-based interface to the Debian GNU/Linux package system.

It allows the user to view the list of packages and to perform package management tasks such as installing, upgrading, and removing packages. Actions may be performed from a visual interface or from the command-line.

2.4 What’s difference of dpkg apt-get and aptitude?

Answer from Alaa Ali :

dpkg only installs a package, so doing dpkg -i packageName.deb will only install this Deb package, and will notify you of any dependencies that need to be installed, but it will not install them, and it will not configure the packageName.deb because well…the dependencies are not there.

apt-get is a Package Management System that handles the installation of Deb packages on Debian-based Linux distributions. A Package Management System is a set of tools that will help you install, remove, and change packages easily. So apt-get is like a clever dpkg.

aptitude then came along. It uses the libraries apt-get uses and actually has an interactive UI (user interface). If you want to see this UI, simply type aptitude in the terminal. That’s aptitude. It leverages the libraries to provide more options and perks than apt-get. For example, aptitude will automatically remove eligible packages, while apt-get needs a separate command to do so. But, in the end, doing sudo aptitude install packageName.deb should at least be the same as sudo apt-get install packageName.deb. There might be subtle differences here and there that I do not know about, but they will both look for the dependencies and do all that stuff. You can read the answer here for more information on the differences between aptitude and apt-get.


BTW: It is recommended to use the aptitude command instead of apt-get. aptitude is better than apt-get when downloading or deleting dependencies. However, aptitude and apt-get cannot be mixed, otherwise they will not know what the other has done.


3. Summary

In this post, I demonstrated how to use aptitude to solve the broken packages problem on ubuntu system. That’s it, thanks for your reading.

-->