19

Sometimes a package is broken and needs to be reinstalled from scratch. I can use apt-get purge followed by apt-get install, but then I have to watch if any dependent packages got uninstalled and remember to re-install them.

Is there a command to purge and reinstall a package, that handles dependencies? If a package depends on the one being reinstalled, that package should either be left in place, or it too should be reinstalled.

2

2 Answers 2

13

It's not advisable, but you can undercut apt and go directly to dpkg.

For example, taking postfix and postfix-dev:

$ sudo apt-get purge postfix
Reading package lists... Done
Building dependency tree
Reading state information... Done.
The following packages will be REMOVED:
  postfix* postfix-dev*
0 upgraded, 0 newly installed, 2 to remove and 99 not upgraded.
After this operation, 4449 kB disk space will be freed.
Do you want to continue? [Y/n] n
Abort.

Now we use dpkg to purge postfix without removing dependents:

$ sudo dpkg --purge --force-depends postfix
dpkg: postfix: dependency problems, but removing anyway as you requested:
 postfix-dev depends on postfix (>= 3.1.0-0).
 postfix-dev depends on postfix (<< 3.1.0.0-0).
 postfix-dev depends on postfix (>= 3.1.0-0).
 postfix-dev depends on postfix (<< 3.1.0.0-0).

(Reading database ... 293223 files and directories currently installed.)
Removing postfix (3.1.0-3) ...
Purging configuration files for postfix (3.1.0-3) ...
Processing triggers for libc-bin (2.23-0ubuntu5) ...
Processing triggers for man-db (2.7.5-1) ...

And now re-install postfix:

$ sudo apt-get install postfix
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  procmail postfix-mysql postfix-pgsql postfix-ldap postfix-pcre sasl2-bin dovecot-common postfix-cdb postfix-doc
The following NEW packages will be installed:
  postfix
0 upgraded, 1 newly installed, 0 to remove and 99 not upgraded.
Need to get 0 B/1152 kB of archives.
After this operation, 3697 kB of additional disk space will be used.
Preconfiguring packages ...
Selecting previously unselected package postfix.
(Reading database ... 293035 files and directories currently installed.)
Preparing to unpack .../postfix_3.1.0-3_amd64.deb ...
Unpacking postfix (3.1.0-3) ...
...
1

After upgrading to ubuntu 21.04, I got in issue in postfix package that caused multiple apps to behave strangely.

I had to purge post fix package altogether

 sudo apt-get purge postfix

than I reinstalled it

 sudo apt-get install postfix

It will ask for mail configuration during postfix installation but you can choose

no configuration

I then restarted my device and it all came back to normal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .