Reinstall All Packages with Pacman on Arch Linux

Reinstall All Packages with Pacman on Arch Linux
At times you may need to reinstall all the packages on your Arch Linux.

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:

$ rm -rfv /usr/bin/grub-*

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:

$ pacman -S –force –noconfirm grub

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:

$ mkdir recovery

Now navigate to the newly created directory with the following command:

$ cd recovery

Now export all the package names that are installed on your Arch Linux system with the following command:

$ pacman -Qq > packages.txt

Now create a new shell script with the following command:

$ nano reinstall.sh

An empty file should be opened in nano text editor.

Now add these lines to the file:

#!/bin/bash
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:

$ chmod +x reinstall.sh

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:

$ cd recovery/

Now run the reinstall.sh script as follows:

$ ./reinstall.sh

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:

$ dhclient -v

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/sda3 /mnt
$ 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.

$ cd /root/recovery

$ ./reinstall.sh

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:

$ exit

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.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *