At our university we can get almost any ubuntu package installed we want, but we are not superusers ourselves (we need to request packages being installed).
With some libraries it is not always easy to know whether the package is already installed or not. Is there a simple way/command to check this?
I always just use this from the command line:
so the above asks dpkg to list all the installed packages and then I grep for only those that have mysql in the name.
One more variant, using aptitude this time:
Tab completion works here as well.
You can use dselect. It provides non-su read-only access.
Also,
dpkg -s <package name>
provides a lot of details related to a package. Eg"You may use
dpkg-query -s <package> 2>/dev/null | grep -q ^"Status: install ok installed"$
in scripts, since it returns exit code 1, if the<package>
is not installed, and 0 if the<package>
is installed.dpkg-query --showformat='${db:Status-Status}'
This produces a small output string which is unlikely to change and is easy to compare deterministically without
grep
:The
$? = 0
check is needed because if you've never installed a package before, and after you remove certain packages such ashello
,dpkg-query
exits with status 1 and outputs to stderr:instead of outputting
not-installed
. The2>&1
captures that error message too when it comes preventing it from going to the terminal.For multiple packages:
The possible statuses are documented in
man dpkg-query
as:The single letter versions are obtainable with
db:Status-Abbrev
, but they come together with the action and error status, so you get 3 characters and would need to cut it.So I think it is reliable enough to rely on the uncapitalized statuses (
Config-files
vsconfig-files
) not changing instead.dpkg -s
exit statusThis unfortunately doesn't do what most users want:
because for some packages, e.g.
certbot
, doing:leaves
certbot
in stateconfig-files
, which means that config files were left in the machine. And in that state,dpkg -s
still returns0
, because the package metadata is still kept around so that those config files can be handled more nicely.To actually make
dpkg -s
return 1 as desired,--purge
would be needed:which actually moves it into
not-installed
/dpkg-query: no packages found matching
.Note that only certain packages leave config files behind. A simpler package like
hello
goes directly frominstalled
tonot-installed
without--purge
.Tested on Ubuntu 20.10.
Python
apt
packageThere is a pre-installed Python 3 package called
apt
in Ubuntu 18.04 which exposes an Python apt interface!A script that checks if a package is installed and installs it if not can be seen at: https://stackoverflow.com/questions/17537390/how-to-install-a-package-using-the-python-apt-api/17538002#17538002
Here is a copy for reference:
Check if an executable is in
PATH
insteadSee: https://stackoverflow.com/questions/592620/how-can-i-check-if-a-program-exists-from-a-bash-script/22589429#22589429
See also
Simpler solution:
There is now an
apt list
command that lists installed packages. You can also search for a specific package withSee
man apt
for more information.You need to check the status printed by
dpkg -l
, example :Here both
vim
andfirefox-esr
are installed, therefore you can type :Example to use specific value as var in shell scripts (eg
test.sh
)Make it executable and go start with:
Or do whatever you want with in your scripts