Could I remove apt with apt?

13,221

Solution 1

APT lets you simulate your commands using the option -s. You can try this yourself, issuing the command apt-get -s remove apt (no sudo needed).

This yields the following output:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apturl-common xul-ext-ubufox
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  apt apt-utils apturl nautilus-share python3-software-properties
  software-properties-common software-properties-gtk ubuntu-desktop
  unattended-upgrades
WARNING: The following essential packages will be removed.
This should NOT be done unless you know exactly what you are doing!
  apt
0 upgraded, 0 newly installed, 9 to remove and 0 not upgraded.
Remv ubuntu-desktop [1.341]
Remv nautilus-share [0.7.3-1ubuntu5]
Remv apturl [0.5.2ubuntu9]
Remv software-properties-gtk [0.96.13.1]
Remv software-properties-common [0.96.13.1]
Remv python3-software-properties [0.96.13.1]
Remv unattended-upgrades [0.86.2ubuntu1]
Remv apt-utils [1.0.10.2ubuntu1]
Remv apt [1.0.10.2ubuntu1]

So the answer should be: yes, you can.

Solution 2

You can...

sudo apt-get remove apt
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following packages will be REMOVED:
  apt apt-utils apturl nautilus-share python3-software-properties
  software-center software-properties-common software-properties-gtk
  ubuntu-desktop ubuntu-extras-keyring ubuntu-minimal unattended-upgrades
WARNING: The following essential packages will be removed.
This should NOT be done unless you know exactly what you are doing!
  apt
0 upgraded, 0 newly installed, 12 to remove and 0 not upgraded.
After this operation, 9,031 kB disk space will be freed.
You are about to do something potentially harmful.
To continue type in the phrase 'Yes, do as I say!'
?] Yes, do as I say!
(Reading database ... 179817 files and directories currently installed.)
Removing ubuntu-desktop (1.327) ...
Removing nautilus-share (0.7.3-1ubuntu5) ...
Removing apturl (0.5.2ubuntu4) ...
dpkg: warning: while removing apturl, directory '/usr/lib/python3/dist-packages/AptUrl/gtk/backend' not empty so not removed
Removing software-properties-gtk (0.94) ...
dpkg: warning: while removing software-properties-gtk, directory '/usr/lib/python3/dist-packages/softwareproperties/gtk' not empty so not removed
Removing software-properties-common (0.94) ...
Removing python3-software-properties (0.94) ...
Removing unattended-upgrades (0.82.8) ...
Removing ubuntu-minimal (1.327) ...
Removing apt-utils (1.0.9.2ubuntu2) ...
Removing software-center (13.10-0ubuntu4.1) ...
Removing ubuntu-extras-keyring (2010.09.27) ...
OK
Removing apt (1.0.9.2ubuntu2) ...
Processing triggers for man-db (2.7.0.2-2) ...
Processing triggers for gconf2 (3.2.6-2ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.13-1) ...
Processing triggers for shared-mime-info (1.2-0ubuntu3) ...
Processing triggers for gnome-menus (3.10.1-0ubuntu2) ...
Processing triggers for desktop-file-utils (0.22-1ubuntu2) ...
Processing triggers for bamfdaemon (0.5.1+14.10.20140925-0ubuntu1) ...
Rebuilding /usr/share/applications/bamf-2.index...
Processing triggers for mime-support (3.55ubuntu1) ...
Processing triggers for dbus (1.8.8-1ubuntu2) ...
Processing triggers for libc-bin (2.19-10ubuntu2) ...

and will be warned you are about to do something very destructive. I must say ... the list of packages looks horrific for a space saving of less then 6000kb :D

It does finish but there is no way back using "apt-get". Ubuntu Software Center will no longer work and you would need to use "dpkg" to re-install a package manager (and manually also need to install all the dependencies).

Solution 3

Once, back when I ran CoreUbuntu, I installed a buggy package from source which apt decided obsoleted apt. Next time I ran apt autoremove, I didn't actually look at the list of software to be removed and apt was in the list.

Imagine my surprise next time I typed apt install <package-name> and got The program 'apt' is currently not installed. You can install it by typing: sudo apt-get install apt.

Luckily, for some reason, autoremove didn't remove any of apt's dependencies so all I had to do was wget apt's .deb archive and reinstall using dpkg -i.


As shown in the other answers, if you remove apt with apt, you'll be in more trouble because of the dependencies it tries to resolve.

I find it interesting but it is indeed the case that (certainly for Debian, and perhaps Fedora/openSUSE to an extent?) many modern distros are defined and built largely upon the infrastructure provided by their package manager of choice.

Solution 4

Technically, apt can't remove apt... because apt doesn't know how to remove, install or upgrade packages. The tasks of installing, removing, upgrading, configuring packages are left to dpkg. Although you can tell apt to remove the package called "apt", what it does is checks the reverse dependencies of the apt package, take note of those packages and orders dpkg to remove them. Which is what can be seen in the other answers.

Even without apt you can use dpkg to install, remove or upgrade packages, just that it will be more painful to track dependencies and upgrades needed, which is the raison d'être of apt.

Solution 5

Of course you can. Apt and dpkg are themselves packages, and they are meant to be able to be updated via themselves, so there are provisions for removal; otherwise /var/lib/dpkg/info/dpkg.prerm and /var/lib/dpkg/info/dpkg.postrm would not have reason to exist :)

If you accidentally removed them in a way that left you without dpkg, you could still manually unpack the .deb archive for dpkg unless you also got rid of binutils, tar, gzip/bzip2.

--purge on any packages involved with the apt toolchain could give you nasty problems, though; hard to tell what owns certain files in /var/lib/dpkg. If /var/lib/dpkg/status got deleted and there was no current backup, then yes, the package manager would be beyond repair on that system.

Share:
13,221

Related videos on Youtube

Tim
Author by

Tim

My name is Tim. I've graduated from the University of Nottingham with a First Class Computer Science BSc with Hons. In my spare time I do computer programming, often C or JavaScript, but also shell scripts, and answering questions on Stack Exchange. I used to spend most of my time on Ask Ubuntu; now I mostly browse the HNQ or Meta Stack Exchange. If you want to contact me it's [email protected] Do you need a reliable VPS? Try Digital Ocean. Sign up with this link for $10 free. One of my favourite sites is Kiva.org, a micro-funding organisation that allows people to lend money via the Internet to low-income entrepreneurs and students in over 80 countries. Kiva's mission is “to connect people through lending to alleviate poverty.” With just $25 you can make a difference - and 99% of loans are paid back in full - so you can lend again and again!

Updated on September 18, 2022

Comments

  • Tim
    Tim almost 2 years

    I know that I can use apt-get remove <package> to remove a program.

    But apt is a program itself. Could I use apt-get remove apt to remove it, or would it get confused part way through?

    • Byte Commander
      Byte Commander over 8 years
      Is that an actual or just a hypothetical question? In case 1: Try it out. Else: I think it should be able to purge itself if it doesn't try to launch any of its components after having removed them. Files that are already opened can be removed without problems.
    • Tim
      Tim over 8 years
      @ByteCommander It's both, but I can't really try it right now.
    • Shadur
      Shadur over 8 years
      You can but you probably shouldn't. Linux rarely asks why its user wants to shoot themselves in the foot, it just assumes you have a good reason and lets you pull the trigger...
    • Davidmh
      Davidmh over 8 years
      For completeness, Fedoras' dnf won't let you do it. # dnf remove dnf Dependencies resolved. Error: The operation would result in removing the following protected packages: dnf.
    • Gaurav Agarwal
      Gaurav Agarwal over 8 years
      What an interestingly evil question that is! A logical follow-up is: can you recover from that, or does it require a complete re-installation?
  • user1717828
    user1717828 over 8 years
    would need to use "dpkg" to re-install a package manager (and manually also need to install all the dependencies), so in other words, sudo apt-get remember-what-2002-was-like
  • Sparhawk
    Sparhawk over 8 years
    Couldn't you boot into a Live CD, then chroot and apt-get, much like Arch does?
  • Sparhawk
    Sparhawk over 8 years
    I'm not sure that apt's simulate mode covers the full fallout for something like this. Surely this would be an edge case that the code may or may not address.
  • Fake Name
    Fake Name over 8 years
    This doesn't require sudo (-s mode doesn't make any changes), so it's probably better to run it without sudo as a precaution.
  • user253751
    user253751 over 8 years
    Apt might well be able to determine how to remove apt. But can it actually do it? It's easy to imagine that it might delete a file that it later depends on, leaving the installation in an inconsistent state.
  • GeorgiaTv
    GeorgiaTv over 8 years
    @immibis File caching and lack of locking like Windows does should handle that. And Cauon, I think you meant "lets".
  • user
    user over 8 years
    @Sparhawk Not sure what you're aiming for; after you chroot, you are in your normal system's root, and would be looking for apt-get there, which you removed. I think apt-get (or maybe it is dpkg) allows specifying an alternate root directory, but I can't seem to find quickly how you would do that.
  • Dmitry Grigoryev
    Dmitry Grigoryev over 8 years
    I bet the reason apt didn't remove the dependencies is that the buggy package you have installed listed them as required. Makes sense if that package was meant as a replacement.
  • cauon
    cauon over 8 years
    @FakeName you are right, I changed the command text accordingly. Thanks!
  • Barb Hammond
    Barb Hammond over 8 years
    @immibis I'd be more concerned about that for removing dpkg. Apt itself just tells dpkg what to do, dpkg is what actually performs the (un)installations.
  • Barb Hammond
    Barb Hammond over 8 years
    None of the packages it removes are particularly "scary" IMO except for, of course, apt itself.
  • SuperSluether
    SuperSluether over 8 years
    So if apt tells dpkg what to do, what happens when you remove dpkg... lol
  • user253751
    user253751 over 8 years
    @CeesTimmerman That is nothing to do with file caching nor locking. Well, locking might cause the file to not be delete-able, in which case uninstallation would still fail for a different reason!
  • Sparhawk
    Sparhawk over 8 years
    @MichaelKjörling I was thinking that you could run the manual path to apt-get, installing in the new "root", but I forgot that you'd need all the config/cache files too. Your possible suggestion would certainly be cleaner though (if there is that option).
  • Joshua
    Joshua over 8 years
    dpkg goes bye-bye. It's kind of a pill to put it back but there is a way involving using an external dpkg. In some cases you can use an external dpkg for the wrong architecture, but only a few packages besides the essential ones really support that case.
  • GeorgiaTv
    GeorgiaTv over 8 years
    @immibis I was too late to correct it to "RAM", but that's similar to caching, and you invalidated your own point about locking. Say, programs in RAM are locked cache aren't they?
  • cat
    cat over 8 years
    Yeah, apt is really just a simpler frontend for dpkg, and if you remove dpkg then is it really Ubuntu anymore?
  • Braiam
    Braiam over 8 years
    @cat I think it will still be... as long as you don't purge the package or remove the /var/lib/dpkg/status file. You can recover dpkg if you like too.
  • Tim
    Tim over 8 years
    @Random832 "ubuntu-desktop"?
  • Barb Hammond
    Barb Hammond over 8 years
    @Tim I guess "scary" is relative. In fact ubuntu-desktop is a placeholder to make sure all of the default GUI stuff gets installed, it'll uninstall itself if you remove almost literally anything from a default system - and it doesn't do anything at all itself.
  • Rinzwind
    Rinzwind over 8 years
    @Random832 yeah well someone who wants to remove "apt-get" probably will 1st think "ehhhhh ubuntu-desktop?"
  • Braiam
    Braiam over 8 years
    @MichaelKjörling read the part after "If your system is unbootable from hard disk"
  • rackandboneman
    rackandboneman over 8 years
    If anything coming from an official distribution package ends up in /usr/local/bin, file a bug against that package.
  • Micheal Johnson
    Micheal Johnson over 8 years
    The thing is that it wasn't coming from a package; it was installed separately from the package manager with the express purpose of avoiding the "apt-get remove apt" and "apt-get remove dpkg" problems. I think it was Linux Mint actually, which I used to use as my main system.
  • Jatin-CBS
    Jatin-CBS almost 6 years
    Removing apt will break the system i guess