1

I am looking for a way to reset installed packages to default state, which includes

  • Remove all manually installed package

  • Reinstall packages included in the setup but later manually removed by me

Is it possible without reinstalling the whole system?

0

For everyone who get here from Google, I wrote the following command that seems to output a list of manually removed packages. It works for me but I don't know if it is correct.

comm -23 \
  <( comm -23 <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u) <(dpkg -l | grep ^ii | awk -F"[ :]" '{print $3}' | sort -u) ) \
  <( gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Depends: //p' | awk '{split($0, packages, ", |\| "); for (key in packages) { printf "%s\n", packages[key] } }' | awk '{print $1}' | sort -u )

The second line of the command compare the list of installed packages right after setup and the list of currently installed packages, resulting in a list of preinstalled packages which was removed by user, including dependencies.

The third line output the list of installed dependent packages right after setup.

By comparing output of the second line and the third line, we get a list of packages installed in Ubuntu setup, but later being removed by user, excluding dependencies.


If you have updated Ubuntu since installation, you should use the command below instead, which removes packages uninstalled during system update from output of the above command.

# Fill in Ubuntu version being installed to your computer in the first place
initial_ubuntu_version="19.10"

# Fill in currently installed Ubuntu version
current_ubuntu_version="20.04"

comm -23 \
  <(
    # All removed preinstalled packages, excluding dependencies (same as the command above)
    comm -23 \
      <( comm -23 <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u) <(dpkg -l | grep ^ii | awk -F"[ :]" '{print $3}' | sort -u) ) \
      <( gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Depends: //p' | awk '{split($0, packages, ", |\| "); for (key in packages) { printf "%s\n", packages[key] } }' | awk '{print $1}' | sort -u )
  ) \
  <(
    # Packages being removed during system update
    comm -23 \
      <( wget "http://releases.ubuntu.com/$initial_ubuntu_version/ubuntu-$initial_ubuntu_version-desktop-amd64.manifest" -q -O - | cut -f 1 | awk -F"[ :]" '{print $1}' | sort -u ) \
      <( wget "http://releases.ubuntu.com/$current_ubuntu_version/ubuntu-$current_ubuntu_version-desktop-amd64.manifest" -q -O - | cut -f 1 | awk -F"[ :]" '{print $1}' | sort -u )
  )

After running the above command, you can reinstall those packages one by one with sudo apt install [package-name], apt will install dependencies for you automatically.

You can also get a list of manually installed packages with the command below (source) and remove them one by one with sudo apt remove [package-name].

comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.