How to Bulk Install Yum Packages from a Text File in CentOS/RHEL


10 views

As a system administrator working with CentOS or RHEL systems, I frequently encounter scenarios where I need to install multiple packages simultaneously. Manually typing each package name with yum install becomes tedious when dealing with dozens of packages. A more efficient approach is to install from a predefined package list file.

The most straightforward method combines xargs with yum:

cat packages.txt | xargs sudo yum install -y

Breakdown:

  • cat packages.txt reads the package list file
  • xargs converts the input into command arguments
  • sudo yum install -y executes the installation without confirmation prompts

For more control over the installation process:

while read pkg; do
  sudo yum install -y "$pkg" || echo "Failed to install $pkg"
done < packages.txt

This approach:

  • Processes each package individually
  • Continues installation even if some packages fail
  • Provides feedback for troubleshooting

Your packages.txt should contain one package per line:

bash
bc
binutils
bzip2
bzip2-libs
ca-certificates
cairo

For environments requiring specific package versions:

bash-4.2.46-34.el7
bc-1.06.95-13.el7
binutils-2.27-43.base.el7

After installation, verify all packages were installed successfully:

while read pkg; do
  rpm -q "$pkg" || echo "$pkg not installed"
done < packages.txt

Combine with ssh for remote installations:

for server in server{1..5}; do
  ssh $server "cat packages.txt | xargs sudo yum install -y"
done
  • Ensure the file uses Unix line endings (LF)
  • Remove comments or empty lines if present
  • Check for whitespace in package names
  • Verify repository availability before batch installation

For large package lists, the transaction becomes more efficient when installed in a single yum command (xargs method) rather than multiple separate commands (loop method).


As a Linux system administrator, I frequently encounter scenarios where I need to install multiple packages simultaneously. Manually typing each package name with yum install becomes tedious when dealing with dozens of packages. A more efficient approach would be to read package names from a text file.

While yum doesn't have a direct -f parameter for file input, we have several effective workarounds:


# Method 1: xargs approach
cat packages.txt | xargs yum install -y

# Method 2: Command substitution
yum install $(cat packages.txt) -y

Your package list file should contain one package per line. Here's a complete example:


bash
bc
binutils
bzip2
bzip2-libs
ca-certificates
cairo
coreutils
curl

When installing from a file, you might encounter dependency issues. Consider these enhanced commands:


# Skip broken packages
yum install --skip-broken $(cat packages.txt)

# Install in a transaction
yum shell <<EOF
install $(tr '\n' ' ' < packages.txt)
run
EOF

For environments requiring frequent identical installations, consider creating a local repo:


# Download packages
mkdir -p /var/local-repo
repotrack -a x86_64 -p /var/local-repo $(cat packages.txt)

# Create repo metadata
createrepo /var/local-repo

# Add to yum configuration
echo "[local-repo]
name=Local Repository
baseurl=file:///var/local-repo
enabled=1
gpgcheck=0" > /etc/yum.repos.d/local.repo

For systems using dnf (Fedora/RHEL8+), the process is similar but with additional features:


dnf install $(cat packages.txt)

DNF also supports direct file reading in some versions:


dnf install < packages.txt