Let’s say you have Arch Linux installed on your machine and it is fully functional. No problem here. Now imagine, you are playing with your Arch Linux system and accidentally deleted some of the system files and folders. You’re freaked out saying, ‘Oh no! I shouldn’t have played God with sudo’. Don’t freak out just yet. There may still be a chance you can fix that using Pacman. You can use Pacman package manager to reinstall all the system packages on your Arch Linux machine.
Another scenario may be, let’s say you did a full system upgrade. Everything went well. But once you reboot your system, it won’t start and you’re getting warnings that some files are missing. This can also be fixed by reinstalling all the packages on your Arch Linux machine.
In this article, I will show you how to reinstall all the packages with Pacman on Arch Linux. Let’s get started.
Reinstallation Fixes Missing Files
In this section I will delete some files from /usr/bin and recover them by reinstalling the package to show you that reinstalling fixes issues related to missing files or corrupted files.
I am going to run the following command to remove all the grub related binary files:
As you can see in the screenshot below, the files are removed.
Now I am going to reinstall the grub package with the following command:
The grub package is reinstalled.
Now let’s do a ls -la grub-*, and as you can see in the screenshot below, the removed files are back.
So reinstalling packages can recover broken Arch Linux systems.
Creating a Script for Reinstalling All the Packages
I am not going to reinstall packages one by one. Instead, I will write a bash script to do that automatically.
First create a new directory recovery/ with the following command:
Now navigate to the newly created directory with the following command:
Now export all the package names that are installed on your Arch Linux system with the following command:
Now create a new shell script with the following command:
An empty file should be opened in nano text editor.
Now add these lines to the file:
for pkgName in $(cat packages.txt
do
pacman -S –force –noconfirm $pkgName
done
echo "Reinstalled all packages."
Now press <Ctrl> + x and then press y and then press <Enter> to save the file.
Now make the script executable with the following command:
Reinstalling All the Packages When You Can Boot Into your Arch Linux System
If you can boot into your Arch Linux machine, then it’s really easy to reinstall all the packages with Pacman package manager with the script I created earlier.
First navigate to the recovery/ directory:
Now run the reinstall.sh script as follows:
As you can see, packages are being reinstalled.
It should take a long time to completed depending on your internet connection.
Reinstalling All the Packages When You Can’t Boot into Your Arch Linux System
If you fail to boot into your Arch Linux system, then grab an Arch Linux installation CD and boot into it.
Once you boot into your Arch Linux installer CD, connect to the internet. If you’re using a wired connection and your network is configured with DHCP, then all you have to do is run the following command:
Then you have to mount the Root and Boot (also EFI partition if you’re using GPT partition table) partition of your broken Arch Linux to /mnt directory. My Root partition is /dev/sda3, Boot partition is /dev/sda2, and EFI partition is /dev/sda1.
Mount these partitions to /mnt with the following commands:
$ mount /dev/sda2 /mnt
$ mount /dev/sda3 /mnt
Now Chroot into /mnt directory.
Navigate to your recovery/ directory and execute the reinstall script. If you weren’t able to create these, then you should be able to follow Creating a Script for Reinstalling All the Packages section of this article and create it now.
The package reinstallation process should start as you can see in the screenshot below.
Once the reinstallation is complete, run the following command to exit out of Chroot:
Then reboot your computer. Your problem should be solved.
That’s how you reinstall all the packages of Arch Linux with Pacman. Thanks for reading this article.