How to recover graphical UI after accidently uninstalling

I am using Xubuntu 18.04. Recently while browsing Store, I've uninstalled something. It uninstalled without problem. But next time when I turned on my PC, I found nothing but a black screen instead of login screen. I tried to open application finder, terminal etc with keyboard shortcuts but it didn't work. I've restarted my PC several times with same result. Then I booted into recovery mode and tried the option "repair broken packages(dpkg)". It found 8 packages like exo-utils, thunar, xfdesktop4, terminal etc but download & installation fails. Is there any way I can recover my desktop?


Solution 1:

This guide should work for Ubuntu or any Ubuntu based flavour.

Step 1:

Turn off your PC by pressing and holding the power button for 5 seconds. Turn it on again. Instead of selecting Ubuntu from boot screen, select Advanced options for Ubuntu. Now select the option which says (recovery mode) at the end of it’s name. If there are multiple option like that, select the first one. Wait for recovery mode to start. In the next window, select Enable Networking. Wait for a few moments to finish the process. Then select dpkg(Repair broken packages). Select Yes in the next dialogue box. The package recovery process will start and hopefully will find some of the crucial packages you’ve uninstalled. Write down the exact names of the packages shown there on a piece of paper. It will then want your permission to reinstall them, give permission by typing y and pressing Enter. If the enable networking option was able to connect you to the internet, the downloading and installing process should succeed. If that happens straightly go to Step 3.

But if you're not connected to the internet, the packages can not be downloaded and the Terminal output will show errors. Don’t worry. Go to step 2.

Step 2:

Create (or use the old one if you still have it) a bootable CD/DVD/USB Flash drive with the same version and flavour of Ubuntu you’ve got installed on your PC. Boot from it and after selecting language, select Try Ubuntu. After Ubuntu starts, mount the partition of your Ubuntu installation using Terminal. If you are not sure which partition it is, launch GParted (included in your Live bootable media) and find out. It is usually an Ext4 Partition. To mount that partition open terminal and run the following command. Replace the XY with the drive letter and partition number-

sudo mount /dev/sdXY /mnt (e.g. sudo mount /dev/sda9 /mnt)

Then run this command:

sudo mount --bind /dev /mnt/dev && sudo mount --bind /dev/pts /mnt/dev/pts && sudo mount --bind /proc /mnt/proc && sudo mount --bind /sys /mnt/sys && sudo mount --bind /run /mnt/run

Now time to use chroot:

sudo chroot /mnt

Now connect to the internet and update the package list by running-

sudo apt update 

It may show some updates but you don’t need to install them. Now install the packages you wrote down on step 1 like this-

sudo apt install PackageName1 PackageName2 PackageName3 (Separate each package name by using space)

After installation finishes, turn off your PC, remove the bootable media and turn the PC back on. Ubuntu should now start normally.

Step 3:

Recovery mode just finds out the most crucial packages needed to rescue your OS. There may be many other packages which got uninstalled when you created the problem. They are yet to be installed and you may find many missing features here and there. To find all the packages that got uninstalled open Terminal and run-

cat /var/log/apt/history.log

Look at the times of the operations, you’ll find your change-list. Again write down the names of any additional packages that you’ve missed installing at step 2. You don’t need to write the architecture of the packages at the end of their name (e.g. You can just write “gnome” instead of “gnome:amd64”). Install them as usual.