Home FAQ Create A List Of Installed Packages And Install Them Later From The List In CentOS And Ubuntu

Create A List Of Installed Packages And Install Them Later From The List In CentOS And Ubuntu

By sk
Published: Last Updated on 9.4K views

A few days ago, we have published a guide that described how to create a list of installed packages and install them later from the list in Arch Linux, and its derivatives like Antergos, Manjaro Linux etc. Today, in this guide, we will do the same, but in other popular Linux distributions such as CentOS and Ubuntu. This will be helpful when you want to re-install your Linux system. You don't have to manually install the packages one by one after re-installing your Linux box. Just take the backup list of installed packages before formatting your system, and and install all at once from the list. This will also be helpful when you wanted to install the same set of software on multiple systems running with similar OS.

Recommended Download - Free eBook: "Learning Python"

Create A List Of Installed Packages In CentOS and install them later

First, we will see how to do it in CentOS system. I tested this guide on CentOS 7 server edition.

Run the following command as root user to create the list of installed packages:

# rpm -qa | sort > pkglist.txt

Here,

  • rpm - RPM Package Manager
  • -qa - Query all installed packages
  • sort - sort lines of text files
  • pkglist.txt - The file where we save the list of installed packages.

The above command will list of all installed packages on your CentOS 7 machine and save them in a file called pkglist.txt in your current working directory.


Recommended Read:


Now, we have created the list of installed packages. Keep this list safe. We need it later. Go ahead and format your system.

After reinstalling your Linux box, copy the pkglist.txt file to your system. You can also copy this file to any remote system using scp command. Just make sure you have installed the same version OS on your remote system.

# scp pkglist user@remotesystem:/path/

Ex:

# scp pkglist.txt root@192.168.43.150:/root/

Finally, install the packages from the pkglist.txt file using the following command as root user:

# yum install $(cat /root/pkglist.txt|xargs)

Replace the path of /root/pkglist.txt file with your own.

Done! The package manager will install all packages listed in the pkglist.txt file on your CentOS box.

You can use this pkglist.txt file to all systems on your network. Please note that the package manager will only install the packages from the default official repositories. If you have enabled any other extra repositories on your old system, you need to add them first before installing the software.

Create A List Of Installed Packages in Ubuntu and install them later

Create list of all installed packages and save the in a file called pkglist.txt in DEB based systems, run:

$ dpkg-query -f '${binary:Package}\n' -W > pkglist.txt

Or,

$ dpkg --get-selections > pkglist.txt

Now, we have created a backup list of installed softwares. Keep this file in a safe location. Format and re-install your Ubuntu machine.

After reinstalling Ubuntu, run the following commands one by one to re-install all software from the backup list.

$ sudo apt-get install dselect
$ sudo dpkg --set-selections < pkglist.txt
$ sudo apt-get dselect-upgrade

Or, combine all of above commands into a single line command and all software from the pkglist.txt file as shown below.

$ sudo apt-get install $(cat /home/sk/pkglist.txt | awk '{print $1}')

Replace the path /home/sk/pkglist.txt file with your own.

Please note that if you have used any external repository or PPA in your old system, you need to add them before re-installing software from the backup list.


Suggested Read:


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

5 comments

dipanjan mukherjee May 18, 2017 - 3:23 am

If the question is reinstall the system with previously loaded pkgs then same thing we can do by using Kickstart file.Even we can do more by using it including partition,PKG install and configuration etc.

Reply
Joe K July 17, 2018 - 11:08 pm

sudo apt install `cat pkglist.txt`
is more simple than
sudo apt-get install $(cat pkglist.txt | awk ‘{print $1}’)
and it works on Ubuntu 18.04

Reply
pooky2483 September 13, 2018 - 10:41 pm

How do we create a list of PPA’s & other sw sources then?

Reply
Bernard January 27, 2019 - 5:29 pm

Unfortunately pkglist.txt will not contain the versions of the packages installed. This is important when attempting to “freeze” the state of the packages so that the exact same package versions can be installed later on.
To store the version number for each package is a common feature in most software packages (node npm, Python pip, Ruby gems, Go vendor.json, etc). I’m surprised to see the same ‘dpkg –get-selections’ answer everywhere even though it doesn’t solve that issue.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More