How to check if a package is installed before installing it with yay AUR helper?

I’m using yay AUR helper to install packages from a list, but it’s not skipping the packages that are already installed. How can I check if a package is already installed before installing it with yay?

Can I check packages from the AUR with pacman like this?

if ! pacman -Qs package_name > /dev/null; then
   yay -S package_name
install_packages() {
  local dir="$1"
  local error_log="${dir}/errorlog.txt"
  local pkg_list="${dir}/pkglist.txt"
  local failed_packages=()

  # Install packages with yay
  echo "Installing packages..."
  while read -r package; do
    if yay -S --noconfirm --norebuild "$package"; then
      echo "Installed $package"
      echo "$package" >> "$error_log"
  done < "$pkg_list"

  # Check for failures
  if [[ ${#failed_packages[@]} -eq 0 ]]; then
    echo "All packages installed successfully"
    echo "Failed to install the following packages:"
    printf '%s\n' "${failed_packages[@]}"

  # Clean up
  rm "$error_log"
man pacman
man yay

It looks like you are making scripts dealing with pkglists … you may be interested in referencing mapare:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.