I would like to write an installation bash script, where I would like to install MySQL server.

On Linux Mint I had followed code:

apt-get -y --force-yes install mysql-server-5.6

but I installed the new Debian 8 and there is no mysql-server - instead there is mariadb.

How can I find out if package exists?

I just know that there is dpkg -s which should tell whether a package is installed.

migrated from serverfault.com Apr 30 '15 at 14:04

This question came from our site for system and network administrators.

  • 3
    --force-yes deactivates all security, so you really could break your system to the point of no repair. Also: --force-yes overrides (deactivates) -y. where the latter could be the option of choice: A long form of -y is --assume-yes, and that's what it does; Saying yes until it could get really ugly, with risking havoc among core elements, fundamentals, etc. --force overrides breaks security (learned it the hard way) – erch Apr 29 '15 at 22:44
  • Addendum: You might be interested in the --simulateoption for apt-get, to do a --dry-run, just in case – erch Apr 29 '15 at 23:02
  • 1
    just to clarify: you want to find out if a package is in any of the repositories that are defined in /etc/apt/sources.list, right? – wullxz Apr 29 '15 at 23:18
  • Note that there actually is a mysql-server package in Debian 8, alongside mariadb: packages.debian.org/jessie/mysql-server – Dan Getz Apr 29 '15 at 23:59
up vote 12 down vote accepted

(the below is from Ubuntu, but the same technique obviously works on Debian as well)

$ apt-cache show screen
Package: screen
Priority: optional
Section: misc
Installed-Size: 950
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Axel Beckert <abe@debian.org>
Architecture: amd64
Version: 4.1.0~20120320gitdb59704-9
Depends: libc6 (>= 2.15), libpam0g (>= 0.99.7.1), libtinfo5
Suggests: iselect (>= 1.4.0-1) | screenie | byobu
Filename: pool/main/s/screen/screen_4.1.0~20120320gitdb59704-9_amd64.deb
Size: 645730
...

If the package exists, information will be displayed. If not, you'll see something like:

$ apt-cache show foobar
N: Unable to locate package foobar
E: No packages found

Additionally, the exit code of apt-cache will be non-zero if no matching packages are found.

Additional note: If you're using apt-cache show package where package is a virtual one (one that doesn't exist, but is, for example, referenced by other packages), you'll get:

N: Can't select versions from package 'package' as it is purely virtual
N: No packages found

The exit code of this is zero (which is a bit misleading in my opinion.)

  • 3
    just to complete this answer: you can also search the defined repositories from the sources.list with this command: apt-cache search *searchstring*. That command will also search the description of packages and could help you find packages where you don't know the exact package name. – wullxz Apr 29 '15 at 23:22
  • @wullxz, only the apt-cache search ... returns any kind of matches and the app. returns 0 even if nothing is found, which is not practical in a bash script. – Alexis Wilke Feb 20 '17 at 4:05

I would use dpkg -l mysql-server &> /dev/null && echo "mysql-server is installed".

It will check if the mysql-server package is installed and if so, it prints this fact to the screen. A more sophisticated solution would be, in bash (untested):

function package_exists() {
    return dpkg -l "$1" &> /dev/null
}

So one can do in a script:

if ! package_exists mysql-server ; then
    echo ”Please install mysql-server!"
fi
  • 5
    I think he wants to know if the package exists at all and not if it is already installed on the local machine... – wullxz Apr 29 '15 at 23:16

In a one-liner:

apt-cache pkgnames | grep -q $your_package_name

exits with 0 if it's present, 1 if not.

Bonus: efficient check for multiple packages, listed in file "packages_we_want", one per line, sorted with sort:

apt-cache pkgnames | sort | comm -13 - packages_we_want lists all packages that you want, but do not exist in the repositories.

apt-cache pkgnames | sort | comm -12 - packages_we_want lists all packages that you want and exist in the repositories.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.