How can I undo or rollback an "apt-get upgrade" command on Ubuntu?

Is there a way in Ubuntu to rollback or undo the last upgrade after doing an apt-get upgrade if you don't like the results?


Solution 1:

I had to do this today on my Debian system. First, I identified the time range when the offending upgrade happened, and retrieved the log entries giving the old and new version numbers of the upgraded packages:

$ awk '$1=="2016-03-20" && $3=="upgrade"' /var/log/dpkg.log
2016-03-20 16:58:22 upgrade libwebkitgtk-3.0-0:amd64 2.4.9-3 2.4.10-1
2016-03-20 16:58:24 upgrade libjavascriptcoregtk-3.0-0:amd64 2.4.9-3 2.4.10-1
2016-03-20 16:58:26 upgrade traceroute:amd64 1:2.0.22-1 1:2.1.0-1
2016-03-20 16:58:33 upgrade ethtool:amd64 1:4.2-1 1:4.5-1
2016-03-20 16:58:34 upgrade libsdl1.2debian:amd64 1.2.15+dfsg1-3 1.2.15+dfsg1-4
2016-03-20 16:58:34 upgrade subversion:amd64 1.9.3-2+b1 1.9.3-3
2016-03-20 16:58:36 upgrade libsvn1:amd64 1.9.3-2+b1 1.9.3-3
2016-03-20 16:58:56 upgrade linux-image-amd64:amd64 4.3+70 4.4+71
2016-03-20 16:58:56 upgrade linux-libc-dev:amd64 4.3.5-1 4.4.6-1
2016-03-20 16:59:03 upgrade amd64-microcode:amd64 2.20141028.1 2.20160316.1

Next, I tried to find the still-cached package files on disk (luckily for me I hadn't run autoclean):

$ awk '$1=="2016-03-20" && $3=="upgrade" {gsub(/:/, "%3a", $5); split($4, f, ":"); print "/var/cache/apt/archives/" f[1] "_" $5 "_" f[2] ".deb"}' /var/log/dpkg.log | xargs -r ls -ld
ls: cannot access '/var/cache/apt/archives/ethtool_1%3a4.2-1_amd64.deb': No such file or directory
-rw-r--r-- 1 root root   28820 Dec 18  2014 /var/cache/apt/archives/amd64-microcode_2.20141028.1_amd64.deb
-rw-r--r-- 1 root root 1978874 Dec 10 18:22 /var/cache/apt/archives/libjavascriptcoregtk-3.0-0_2.4.9-3_amd64.deb
-rw-r--r-- 1 root root  185006 Mar 12 00:41 /var/cache/apt/archives/libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb
-rw-r--r-- 1 root root 1317644 Mar  3 11:30 /var/cache/apt/archives/libsvn1_1.9.3-2+b1_amd64.deb
-rw-r--r-- 1 root root 7679400 Dec 10 18:22 /var/cache/apt/archives/libwebkitgtk-3.0-0_2.4.9-3_amd64.deb
-rw-r--r-- 1 root root    6108 Dec 14 06:59 /var/cache/apt/archives/linux-image-amd64_4.3+70_amd64.deb
-rw-r--r-- 1 root root 1075506 Feb  7 21:36 /var/cache/apt/archives/linux-libc-dev_4.3.5-1_amd64.deb
-rw-r--r-- 1 root root  983174 Mar  3 11:30 /var/cache/apt/archives/subversion_1.9.3-2+b1_amd64.deb
-rw-r--r-- 1 root root   53376 Feb 28 18:35 /var/cache/apt/archives/traceroute_1%3a2.0.22-1_amd64.deb

It looks like I don't have the older ethtool package for some reason. Still, let's carry on by force-installing the older package files:

$ sudo dpkg -i /var/cache/apt/archives/amd64-microcode_2.20141028.1_amd64.deb /var/cache/apt/archives/libjavascriptcoregtk-3.0-0_2.4.9-3_amd64.deb /var/cache/apt/archives/libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb /var/cache/apt/archives/libsvn1_1.9.3-2+b1_amd64.deb /var/cache/apt/archives/libwebkitgtk-3.0-0_2.4.9-3_amd64.deb /var/cache/apt/archives/linux-image-amd64_4.3+70_amd64.deb /var/cache/apt/archives/linux-libc-dev_4.3.5-1_amd64.deb /var/cache/apt/archives/subversion_1.9.3-2+b1_amd64.deb /var/cache/apt/archives/traceroute_1%3a2.0.22-1_amd64.deb
dpkg: warning: downgrading amd64-microcode from 2.20160316.1 to 2.20141028.1
(Reading database ... 139632 files and directories currently installed.)
Preparing to unpack .../amd64-microcode_2.20141028.1_amd64.deb ...
Unpacking amd64-microcode (2.20141028.1) over (2.20160316.1) ...
dpkg: warning: downgrading libjavascriptcoregtk-3.0-0:amd64 from 2.4.10-1 to 2.4.9-3
Preparing to unpack .../libjavascriptcoregtk-3.0-0_2.4.9-3_amd64.deb ...
Unpacking libjavascriptcoregtk-3.0-0:amd64 (2.4.9-3) over (2.4.10-1) ...
dpkg: warning: downgrading libsdl1.2debian:amd64 from 1.2.15+dfsg1-4 to 1.2.15+dfsg1-3
Preparing to unpack .../libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb ...
Unpacking libsdl1.2debian:amd64 (1.2.15+dfsg1-3) over (1.2.15+dfsg1-4) ...
dpkg: warning: downgrading libsvn1:amd64 from 1.9.3-3 to 1.9.3-2+b1
Preparing to unpack .../libsvn1_1.9.3-2+b1_amd64.deb ...
Unpacking libsvn1:amd64 (1.9.3-2+b1) over (1.9.3-3) ...
Preparing to unpack .../libwebkitgtk-3.0-0_2.4.9-3_amd64.deb ...
Unpacking libwebkitgtk-3.0-0:amd64 (2.4.9-3) over (2.4.9-3) ...
dpkg: warning: downgrading linux-image-amd64 from 4.4+71 to 4.3+70
Preparing to unpack .../linux-image-amd64_4.3+70_amd64.deb ...
Unpacking linux-image-amd64 (4.3+70) over (4.4+71) ...
dpkg: warning: downgrading linux-libc-dev:amd64 from 4.4.6-1 to 4.3.5-1
Preparing to unpack .../linux-libc-dev_4.3.5-1_amd64.deb ...
Unpacking linux-libc-dev:amd64 (4.3.5-1) over (4.4.6-1) ...
dpkg: warning: downgrading subversion from 1.9.3-3 to 1.9.3-2+b1
Preparing to unpack .../subversion_1.9.3-2+b1_amd64.deb ...
Unpacking subversion (1.9.3-2+b1) over (1.9.3-3) ...
dpkg: warning: downgrading traceroute from 1:2.1.0-1 to 1:2.0.22-1
Preparing to unpack .../traceroute_1%3a2.0.22-1_amd64.deb ...
Unpacking traceroute (1:2.0.22-1) over (1:2.1.0-1) ...
Setting up amd64-microcode (2.20141028.1) ...
update-initramfs: deferring update (trigger activated)
amd64-microcode: microcode will be updated at next boot
Setting up libjavascriptcoregtk-3.0-0:amd64 (2.4.9-3) ...
Setting up libsdl1.2debian:amd64 (1.2.15+dfsg1-3) ...
Setting up libsvn1:amd64 (1.9.3-2+b1) ...
dpkg: dependency problems prevent configuration of libwebkitgtk-3.0-0:amd64:
 libwebkitgtk-3.0-0:amd64 depends on libwebkitgtk-3.0-common (>= 2.4.9); however:
  Package libwebkitgtk-3.0-common is not installed.

dpkg: error processing package libwebkitgtk-3.0-0:amd64 (--install):
 dependency problems - leaving unconfigured
Setting up linux-image-amd64 (4.3+70) ...
Setting up linux-libc-dev:amd64 (4.3.5-1) ...
Setting up subversion (1.9.3-2+b1) ...
Setting up traceroute (1:2.0.22-1) ...
update-alternatives: using /usr/bin/traceroute.db to provide /usr/bin/traceroute (traceroute) in auto mode
update-alternatives: using /usr/bin/lft.db to provide /usr/bin/lft (lft) in auto mode
update-alternatives: using /usr/bin/traceproto.db to provide /usr/bin/traceproto (traceproto) in auto mode
update-alternatives: using /usr/sbin/tcptraceroute.db to provide /usr/sbin/tcptraceroute (tcptraceroute) in auto mode
Processing triggers for libc-bin (2.22-3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for initramfs-tools (0.123) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-1-amd64
Errors were encountered while processing:
 libwebkitgtk-3.0-0:amd64

As the error message said, one of my packages depended on a -common package just before the upgrade, but the upgrade removed it (and apt-get can't find it any more). Luckily, its package file is still in /var/cache/apt so I can just add it to the list and try again:

$ ls -ld /var/cache/apt/archives/libwebkitgtk-3.0-common*
-rw-r--r-- 1 root root 452278 Dec 10 18:22 /var/cache/apt/archives/libwebkitgtk-3.0-common_2.4.9-3_all.deb
$ sudo dpkg -i /var/cache/apt/archives/amd64-microcode_2.20141028.1_amd64.deb /var/cache/apt/archives/libjavascriptcoregtk-3.0-0_2.4.9-3_amd64.deb /var/cache/apt/archives/libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb /var/cache/apt/archives/libsvn1_1.9.3-2+b1_amd64.deb /var/cache/apt/archives/libwebkitgtk-3.0-0_2.4.9-3_amd64.deb /var/cache/apt/archives/linux-image-amd64_4.3+70_amd64.deb /var/cache/apt/archives/linux-libc-dev_4.3.5-1_amd64.deb /var/cache/apt/archives/subversion_1.9.3-2+b1_amd64.deb /var/cache/apt/archives/traceroute_1%3a2.0.22-1_amd64.deb /var/cache/apt/archives/libwebkitgtk-3.0-common_2.4.9-3_all.deb
(Reading database ... 139632 files and directories currently installed.)
Preparing to unpack .../amd64-microcode_2.20141028.1_amd64.deb ...
Unpacking amd64-microcode (2.20141028.1) over (2.20141028.1) ...
Preparing to unpack .../libjavascriptcoregtk-3.0-0_2.4.9-3_amd64.deb ...
Unpacking libjavascriptcoregtk-3.0-0:amd64 (2.4.9-3) over (2.4.9-3) ...
Preparing to unpack .../libsdl1.2debian_1.2.15+dfsg1-3_amd64.deb ...
Unpacking libsdl1.2debian:amd64 (1.2.15+dfsg1-3) over (1.2.15+dfsg1-3) ...
Preparing to unpack .../libsvn1_1.9.3-2+b1_amd64.deb ...
Unpacking libsvn1:amd64 (1.9.3-2+b1) over (1.9.3-2+b1) ...
Preparing to unpack .../libwebkitgtk-3.0-0_2.4.9-3_amd64.deb ...
Unpacking libwebkitgtk-3.0-0:amd64 (2.4.9-3) over (2.4.9-3) ...
Preparing to unpack .../linux-image-amd64_4.3+70_amd64.deb ...
Unpacking linux-image-amd64 (4.3+70) over (4.3+70) ...
Preparing to unpack .../linux-libc-dev_4.3.5-1_amd64.deb ...
Unpacking linux-libc-dev:amd64 (4.3.5-1) over (4.3.5-1) ...
Preparing to unpack .../subversion_1.9.3-2+b1_amd64.deb ...
Unpacking subversion (1.9.3-2+b1) over (1.9.3-2+b1) ...
Preparing to unpack .../traceroute_1%3a2.0.22-1_amd64.deb ...
Unpacking traceroute (1:2.0.22-1) over (1:2.0.22-1) ...
Selecting previously unselected package libwebkitgtk-3.0-common.
Preparing to unpack .../libwebkitgtk-3.0-common_2.4.9-3_all.deb ...
Unpacking libwebkitgtk-3.0-common (2.4.9-3) ...
Setting up amd64-microcode (2.20141028.1) ...
update-initramfs: deferring update (trigger activated)
amd64-microcode: microcode will be updated at next boot
Setting up libjavascriptcoregtk-3.0-0:amd64 (2.4.9-3) ...
Setting up libsdl1.2debian:amd64 (1.2.15+dfsg1-3) ...
Setting up libsvn1:amd64 (1.9.3-2+b1) ...
Setting up linux-image-amd64 (4.3+70) ...
Setting up linux-libc-dev:amd64 (4.3.5-1) ...
Setting up subversion (1.9.3-2+b1) ...
Setting up traceroute (1:2.0.22-1) ...
update-alternatives: using /usr/bin/traceroute.db to provide /usr/bin/traceroute (traceroute) in auto mode
update-alternatives: using /usr/bin/lft.db to provide /usr/bin/lft (lft) in auto mode
update-alternatives: using /usr/bin/traceproto.db to provide /usr/bin/traceproto (traceproto) in auto mode
update-alternatives: using /usr/sbin/tcptraceroute.db to provide /usr/sbin/tcptraceroute (tcptraceroute) in auto mode
Setting up libwebkitgtk-3.0-common (2.4.9-3) ...
Setting up libwebkitgtk-3.0-0:amd64 (2.4.9-3) ...
Processing triggers for libc-bin (2.22-3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for initramfs-tools (0.123) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-1-amd64

Success! Actually this didn't solve my problem. But it successfully downgraded the packages, QED.

Solution 2:

aptitude gives you access to all versions of a package if available according to Debian package management.