Linux find if package is installed

Reliably check if a package is installed or not

I have a simple requirement. I want to define several variables that will correspond to any number of given packages I want to install via a shell script. Sample code below:

MISC="shutter pidgin" WEB="apache2 mongodb" for pkg in $MISC $WEB; do if [ "dpkg-query -W $pkg | awk = """ ]; then echo -e "$pkg is already installed" else apt-get -qq install $pkg echo "Successfully installed $pkg" fi done 

Everything kinda works, but the logic seems flawed because it’s not reliably installing the packages I want. It either says they’ve been installed already or it’s trying to install packages that have already been installed previously. I’ve also been trying with command -v or the following:

if [ "dpkg -l | awk | grep --regexp=^$pkg$ != """ ]; then 

And even with the -n and -z flags to check if the returned string was empty. Pretty sure I’m missing some good sense here. Do you have any idea what I could do to make sure a package is actually installed or not? Thanks!

What harm there is, if you call apt-get install for installed packages, too? You could just call apt-get install $MISC $WEB .

4 Answers 4

Essentially you only need to replace the if condition with

if dpkg --get-selections | grep -q "^$pkg[[:space:]]*install$" >/dev/null; then 

It is not possible to use dpkg-query , because it returns true also for packages removed but not purged.

Also I suggest to check the exit code of apt-get before giving the successful message:

if apt-get -qq install $pkg; then echo "Successfully installed $pkg" else echo "Error installing $pkg" fi 

@Taymon: redirect both stdin and stderr to the given file ( /dev/null in this case), because we don’t need the output, only the exit code. It only works in bash (the first line of the script has to be #!/bin/bash ) otherwise use >/dev/null 2>&1 .

Beware: if using bash and the pipefail option is set, then the grep -q can generate Heisenbugs. Basically, grep exits before dpkg finishes writing (you want a 0 exit status in that case) so dpkg fails writing to the pipe (which instead generates a non-0 exit status). Either make sure pipefail is not set, or abandon the (probably tiny) efficiency gains of the «-q» option.

This answer internally produces as many lines as there are packages known by the system (2870 on my machine), to be then filtered by grep (thus potentially slow), while @jarno’s answers uses dpkg-query that returns only one line (potentially faster). On a Raspberry Pi 3, the one based on dpkg-query appears (though inconsistently) faster.

You can test it by dpkg-query:

if dpkg-query -W -f'$' "$pkg" 2>/dev/null | grep -q "ok installed"; then 

Note that * and ? are wildcards, if they appear in $pkg. I guess dpkg-query may print «reinst-required installed» instead of «ok installed», if package is broken and needs to be reinstalled by command apt-get install —reinstall which can be used to install new packages as well.

#to check package is installed or not without distribution dependency #!/bin/bash read -p "Package Name: " pkg which $pkg > /dev/null 2>&1 if [ $? == 0 ] then echo "$pkg is already installed. " else read -p "$pkg is not installed. Answer yes/no if want installation_ " request if [ $request == "yes" ] then yum install $pkg fi fi 

This seems similar to what OP wanted to do so I thought I’d share:

function package_check() < # Tell apt-get we're never going to be able to give manual feedback: export DEBIAN_FRONTEND=noninteractive sudo apt-get update #PKG_LIST='build-essential devscripts debhelper' #if input is a file, convert it to a string like: #PKG_LIST=$(cat ./packages.txt) PKG_LIST=$1 for package in $PKG_LIST; do CHECK_PACKAGE=$(sudo dpkg -l \ | grep --max-count 1 "$package" \ | awk '') if [[ ! -z "$CHECK_PACKAGE" ]]; then echo "$package" 'IS installed'; pkg_installed="yes" else echo "$package" 'IS NOT installed, installing'; sudo apt-get --yes install --no-install-recommends "$package" pkg_installed="no" package_install "$package" fi done # Delete cached files we don't need anymore sudo apt-get clean > 
$ source ./package_check.bash $ PACKAGES=$(cat ./packages.txt) && package_check "$PACKAGES" Hit:1 http://deb.debian.org/debian bullseye-updates InRelease Hit:2 http://security.debian.org/debian-security bullseye-security InRelease Hit:3 http://deb.debian.org/debian bullseye InRelease Hit:4 http://mirrors.rit.edu/mxlinux/mx-packages/mx/repo bullseye InRelease Reading package lists. Done build-essential IS installed devscripts IS installed debhelper IS installed 2048 IS NOT installed, installing Reading package lists. Done Building dependency tree. Done Reading state information. Done The following NEW packages will be installed: 2048 

Источник

Читайте также:  Linux интерфейс что это

How can I find out if a specific program is installed? [duplicate]

I want to find out if a program — Chromium for example — is installed on Ubuntu or not. Manually or as a package. How do I know if a program is installed via command line?

5 Answers 5

And there’s always apt-cache policy (no sudo needed).

oli@bert:/$ apt-cache policy gnuift gnuift: Installed: (none) Candidate: 0.1.14-11 Version table: 0.1.14-11 0 500 http://archive.ubuntu.com/ubuntu/ oneiric/universe amd64 Packages 
oli@bert:/$ apt-cache policy firefox firefox: Installed: 8.0+build1-0ubuntu0.11.10.3 Candidate: 8.0+build1-0ubuntu0.11.10.3 Version table: *** 8.0+build1-0ubuntu0.11.10.3 0 500 http://archive.ubuntu.com/ubuntu/ oneiric-updates/main amd64 Packages 500 http://archive.ubuntu.com/ubuntu/ oneiric-security/main amd64 Packages 100 /var/lib/dpkg/status 7.0.1+build1+nobinonly-0ubuntu2 0 500 http://archive.ubuntu.com/ubuntu/ oneiric/main amd64 Packages 

Or dpkg : dpkg -l | grep -E ‘^ii’ | grep . When it’s not installed it won’t show output. When it is, it’ll show something like:

oli@bert:~$ dpkg -l | grep -E '^ii' | grep firefox ii firefox 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla ii firefox-branding 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla - transitional package ii firefox-globalmenu 8.0+build1-0ubuntu0.11.10.3 Unity appmenu integration for Firefox ii firefox-gnome-support 8.0+build1-0ubuntu0.11.10.3 Safe and easy web browser from Mozilla - GNOME support ii firefox-locale-en 8.0+build1-0ubuntu0.11.10.3 English language pack for Firefox 

It’s obviously a fuzzier search but handy if you’re not sure which package you’re looking for.

For manually installed things.

A bit harder but if they’re on the current path, you could just run them. That’s a bit of mission so I’d rather just run:

oli@bert:/$ which chromium-browser /usr/bin/chromium-browser 
oli@bert:/$ which gnuift # returns nothing 

Which is better?

That depends on the sanity of user. There’s nothing to stop somebody installing something called chromium-browser that isn’t Chromium. They could even package it up incorrectly and install that. Neither method can be 100% certain.

But assuming the owner is sane — packages should be good enough for most people.

Читайте также:  Отличия операционных систем семейства linux от windows

e,g, Chromium, Run in terminal chromium-browser if it’s install, it will be open. If it’s not you will get

chromium-browser: command not found 

To check whether a package is install also

dpkg -l | grep chromium-browser 

You will get like this if it is installed:

enter image description here

To listing all installed packages, just use

Use Ubuntu Software Center type chromium

If you see the green icon like this:

enter image description here

That means it is installed 🙂

nice answer helpful. but here when i hit dpkg -l to check all package installed, it listed all but i was unable to see list till 1st program. Do you knw how can i see it till 1st program

For a graphical view, open the Software Centre , and click on the Installed button at the top:

enter image description here

You may want to click the Show X technical items button if you’re interested in system stuff, but Chromium would be there on the list anyway.

If you want a command line solution, then dpkg is your friend:

$ dpkg -l Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Description +++-==============-==============-============================================ ii accountsservic 0.6.14-1git1ub query and manipulate user account informatio ii acl 2.2.51-3 Access control list utilities ii acpi-support 0.138 scripts for handling many ACPI events ii acpid 1:2.0.10-1ubun Advanced Configuration and Power Interface e ii acroread 9.4.6~enu-0one Adobe Reader ii acroread-commo 9.4.6~enu-0one Adobe Reader - Common Files ii adduser 3.112+nmu1ubun add and remove users and groups ii adium-theme-ub 0.3.1-0ubuntu1 Adium message style for Ubuntu ii aisleriot 1:3.2.1-0ubunt Solitaire card games ii alacarte 0.13.2-2ubuntu easy GNOME menu editing tool ii alsa-base 1.0.24+dfsg-0u ALSA driver configuration files ii alsa-utils 1.0.24.2-0ubun Utilities for configuring and using ALSA . 

Источник

How to Find Out Whether a Package Is Installed in Linux

Linux Packages Featured

Linux “packages” are just collections of software, and how you install them varies from distro to distro. There are a few ways to quickly check whether a package is currently installed or not. Whether you’re comfortable using the terminal or you’d prefer a more visual approach, here’s how you can check whether or not a package is installed in Linux .

Using Package Managers

Each Linux distro includes a package manager. This, as the name suggests, is the software you use to install or remove software packages. They also include commands that let you see if certain packages are already installed.

On Debian and Ubuntu-based Linux distros, you can check for every installed package with the following command:

If you want to find a specific package, add the package name after the —installed flag. For example:

Linux Packages Apt

If the package is installed, you’ll see a brief line with the package name and installed version number. Packages that aren’t installed won’t appear at all.

You can also use dpkg to check for installed packages by typing:

This will give you a description of the package, including version and size, but it’ll also display whether or not it’s installed.

Linux Packages Dpkg

Arch Linux users can check using pacman , the Arch package manager. Open a terminal and type:

If you’re using Fedora, you can find out the same by using dnf and typing:

These commands will require you to know the name of the package you’re looking for, but certain package managers, like dnf , allow you to use wildcards like * to help you search.

Читайте также:  Dallas lock astra linux установка

Using “which” or “has” on Any Linux Distribution

There are other ways to use the terminal to find out whether a package is installed. The which command is one example, which shows you the installation location of any package.

If you search for sudo , for instance, it will display the location of sudo in “/usr/bin/sudo.” To use it, type:

Replace packagename with the name of your package.

Linux Packages Which

Alternatively, you can also use a third-party solution called has.

You can install it to your Linux machine directly or, if you trust the script, run the script directly from the Internet. You’ll need to have the curl package installed to be able to do this. Open the terminal and type:

curl -sL https://git.io/_has | bash -s packagename1 packagename2

You can check a single package or several packages at once. Just replace packagename with the name of your package.

Linux Packages Has

Installed packages will have a green tick next to it along with the version number. Packages that aren’t installed will be displayed with a red cross.

Visual Methods for Checking Installed Packages

If you’d rather avoid the terminal, most Linux package managers come with a GUI alternative to perform the same tasks.

One of the best options, and one that should work across multiple distros, is GNOME Software. This will work with various package managers like apt or pacman , and comes pre-installed with Ubuntu.

Linux Packages Gnome Software

It has a simple GUI with an “installed” section that lists installed software on your PC, although it’s simplistic and won’t list every package.

If you need more detailed information, Debian and Ubuntu-based distributions can use the Synaptic Package Manager. This is a GUI wrapper for apt that lets you search through and install packages, as well as see which packages you already have installed.

Linux Packages Synaptic

Installed packages will be displayed with a green checkbox next to the package name, as well as the package version, listed under the “Installed Version” category.

You may need to install it first (which will involve opening the terminal), but the installation is quick. Open the terminal and type:

sudo apt install synaptic

Arch Linux users have a variety of GUI wrappers for pacman , their package manager, to choose from. You can find out more about these from the Arch Linux wiki.

Easily Identifying Installed Packages

One of the biggest benefits of Linux is choice. You can fall back on your distro’s package manager, or you can use third-party solutions like has to find out if a package is already installed.

If you’re not comfortable using the terminal, you can use your GUI to check installed packages instead. Some of the best Linux distros for beginners make it easy to avoid the terminal completely, thanks to their own GUI software installers like GNOME Software.

Which method do you prefer? Let us know in the comments below.

Ben is a UK based tech writer with a passion for gadgets, gaming, and general geekiness.

Our latest tutorials delivered straight to your inbox

Источник

Оцените статью
Adblock
detector