How to Install & Uninstall a list of Packages from a file

In some case you may need to install a list of packages from one server to another.

For example, you have installed 15 packages on Server-A, and those packages needs to be installed on Server-B, Server-C, etc.

We can manually install all the packages but it’s time consuming process. It can be done for one or two servers, what if you have say 10 servers? Isn’t that time consuming?

Don’t worry we are here to help you out in this situation. We will show you four methods in this article to overcome this situation.

I hope this will help you to fix your issue. I have tested these commands on CentOS7 and Ubuntu 18.04 systems. Hope this will work with other distributions too. Make sure to replace the distribution official package manager command depending on your OS.

(Navigate to this article if you want to check  list of installed packages in Linux system)

Example: if you would like to create a package list from RHEL based system then use the following steps. Do the same for other distributions too:

# rpm -qa --last | head -15 | awk '{print $1}' > /tmp/pack1.txt

# cat /tmp/pack1.txt 
mariadb-server-5.5.60-1.el7_5.x86_64
perl-DBI-1.627-4.el7.x86_64
perl-DBD-MySQL-4.023-6.el7.x86_64
perl-PlRPC-0.2020-14.el7.noarch
perl-Net-Daemon-0.48-5.el7.noarch
perl-IO-Compress-2.061-2.el7.noarch
perl-Compress-Raw-Zlib-2.061-4.el7.x86_64
mariadb-5.5.60-1.el7_5.x86_64
perl-Data-Dumper-2.145-3.el7.x86_64
perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64
httpd-2.4.6-88.el7.centos.x86_64
mailcap-2.1.41-2.el7.noarch
httpd-tools-2.4.6-88.el7.centos.x86_64
apr-util-1.5.2-6.el7.x86_64
apr-1.4.8-3.el7_4.1.x86_64

Method-1 : How to install a list of packages from a file in Linux?

Create a file and add the list of packages that you want to install.

For testing purpose, we are going to add only the below three packages into the file:

# cat /tmp/pack1.txt

apache2
mariadb-server
nano

Simply run the following apt command to install the packages in a single shot from a file in Ubuntu/Debian systems:

# apt -y install $(cat /tmp/pack1.txt)

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libopts25 sntp
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom spell
The following NEW packages will be installed:
  apache2 mariadb-server nano
0 upgraded, 3 newly installed, 0 to remove and 24 not upgraded.
Need to get 339 kB of archives.
After this operation, 1,377 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 apache2 amd64 2.4.29-1ubuntu4.6 [95.1 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 nano amd64 2.9.3-2 [231 kB]                                                                                                                           
Get:3 http://in.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 mariadb-server all 1:10.1.38-0ubuntu0.18.04.1 [12.9 kB]                                                                                   
Fetched 339 kB in 19s (18.0 kB/s)                                                                                                                                                                                 
Selecting previously unselected package apache2.
(Reading database ... 290926 files and directories currently installed.)
Preparing to unpack .../apache2_2.4.29-1ubuntu4.6_amd64.deb ...
Unpacking apache2 (2.4.29-1ubuntu4.6) ...
Selecting previously unselected package nano.
Preparing to unpack .../nano_2.9.3-2_amd64.deb ...
Unpacking nano (2.9.3-2) ...
Selecting previously unselected package mariadb-server.
Preparing to unpack .../mariadb-server_1%3a10.1.38-0ubuntu0.18.04.1_all.deb ...
Unpacking mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Setting up apache2 (2.4.29-1ubuntu4.6) ...
Processing triggers for ureadahead (0.100.0-20) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Setting up nano (2.9.3-2) ...
update-alternatives: using /bin/nano to provide /usr/bin/editor (editor) in auto mode
update-alternatives: using /bin/nano to provide /usr/bin/pico (pico) in auto mode
Processing triggers for systemd (237-3ubuntu10.20) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Setting up mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...

For removal, use the same format with appropriate option as show below:

# apt -y remove $(cat /tmp/pack1.txt)
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  apache2-bin apache2-data apache2-utils galera-3 libaio1 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libconfig-inifiles-perl libdbd-mysql-perl libdbi-perl libjemalloc1 liblua5.2-0
  libmysqlclient20 libopts25 libterm-readkey-perl mariadb-client-10.1 mariadb-client-core-10.1 mariadb-common mariadb-server-10.1 mariadb-server-core-10.1 mysql-common sntp socat
Use 'apt autoremove' to remove them.
The following packages will be REMOVED:
  apache2 mariadb-server nano
0 upgraded, 0 newly installed, 3 to remove and 24 not upgraded.
After this operation, 1,377 kB disk space will be freed.
(Reading database ... 291046 files and directories currently installed.)
Removing apache2 (2.4.29-1ubuntu4.6) ...
Removing mariadb-server (1:10.1.38-0ubuntu0.18.04.1) ...
Removing nano (2.9.3-2) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/editor (editor) in auto mode
Processing triggers for ufw (0.36-0ubuntu0.18.04.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

Use the following yum command to install/uninstall listed packages from a file on RHEL based systems, such as CentOS, RHEL (Redhat) and OEL (Oracle Enterprise Linux):

# yum -y install $(cat /tmp/pack1.txt)  [For Installation]
# yum -y remove $(cat /tmp/pack1.txt)   [For Un-Installation]

Use the following dnf command to install/uninstall listed packages from a file on Fedora, CentOS 8 & RHEL 8 systems:

# dnf -y install $(cat /tmp/pack1.txt)  [For Installation]
# dnf -y remove $(cat /tmp/pack1.txt)   [For Un-Installation]

Use the following zypper command to install/uninstall listed packages from a file on openSUSE system:

# zypper -y install $(cat /tmp/pack1.txt)  [For Installation]
# zypper -y remove $(cat /tmp/pack1.txt)   [For Un-Installation]

Use the following pacman command to install/uninstall listed packages from a file on Arch Linux based systems such as Manjaro, etc.

# pacman -S $(cat /tmp/pack1.txt)  [For Installation]
# pacman -Rs $(cat /tmp/pack1.txt)   [For Un-Installation]

Method-2 : How to install a list of packages from a file in Linux

I prefer to go with this method because this is very simple and straightforward.

Use the following ‘apt’ command to install/uninstall listed packages from a file on Debian based systems such as Debian, Ubuntu and Linux Mint.

# cat /tmp/pack1.txt | xargs apt -y install  [For Installation]
# cat /tmp/pack1.txt | xargs apt -y remove   [For Un-Installation]

Use the following ‘yum‘ command to install/uninstall listed packages from a file on RHEL based systems such as CentOS, RHEL (Redhat) and OEL (Oracle Enterprise Linux):

# cat /tmp/pack1.txt | xargs yum -y install  [For Installation]
# cat /tmp/pack1.txt | xargs yum -y remove   [For Un-Installation]

Use the following ‘dnf‘ command to install/uninstall listed packages from a file on Fedora, RHEL 8 & CentOS 8 systems:

# cat /tmp/pack1.txt | xargs dnf -y install  [For Installation]
# cat /tmp/pack1.txt | xargs dnf -y remove   [For Un-Installation]

Use the following ‘zypper‘ command to install/uninstall listed packages from a file on openSUSE system:

# cat /tmp/pack1.txt | xargs zypper -y install  [For Installation]
# cat /tmp/pack1.txt | xargs zypper -y remove   [For Un-Installation]

Use the following ‘pacman‘ command to install/uninstall listed packages from a file on Arch Linux based systems such as Manjaro and Antergos system:

# cat /tmp/pack1.txt | xargs pacman -S    [For Installation]
# cat /tmp/pack1.txt | xargs pacman -Rs   [For Un-Installation]

Method-3 : How to install list of packages from a file in Ubuntu?

Alternatively, we can use the “For Loop” command to achieve this.

To install the packages in bulk, use the below format to run a “For Loop” in a single line:

# for pack in `cat /tmp/pack1.txt` ; do apt -y install $i; done

To install the packages in bulk using a shell script, use the following “For Loop”:

# vi /opt/scripts/bulk-package-install.sh

#!/bin/bash
for pack in `cat /tmp/pack1.txt`
do apt -y install $pack
done

Set an executable permission to bulk-package-install.sh file:

# chmod + bulk-package-install.sh

Finally, run the below script to achieve this:

# sh bulk-package-install.sh

Method-4 : How to install list of packages from a file in Ubuntu?

Use the “While Loop” command to achieve this.

To install the packages in bulk, use the below format to run a “While Loop” on a single line:

# file="/tmp/pack1.txt"; while read -r pack; do apt -y install $pack; done < "$file"

To install packages in bulk with a shell script, use the “While Loop” in the following way:

# vi /opt/scripts/bulk-package-install.sh

#!/bin/bash
file="/tmp/pack1.txt"
while read -r pack
do apt -y install $pack
done < "$file"

Set an executable permission to bulk-package-install.sh file:

# chmod + bulk-package-install.sh

Finally, run the script to achieve this:

# sh bulk-package-install.sh

Wrapping Up

In this guide, we have shown you several examples to Install & Uninstall a list of Packages from a file

If you have any questions or feedback, feel free to comment below, and we will get back to you as soon as we can. Happy learning!

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

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