Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to automate my setup as much as possible.

To do this I have lists of packages that I want to install, for example: banshee wireshark audacity thunderbird thunderbird-lightning calibre deluge unison-gtk usb-creator-kde ding digikam chromium-browser bleachbit soundconverter kdenlive firefox-kde-support vlc kwrite openjdk-6-jre icedtea6-plugin virtualbox virtualbox-guest-additions-iso.

I want to write a small bash script to call apt-get to install these packages only if they're not already installed.

Currently I have this but it doesn't work:

dpkg -s "$1" > /dev/null 2>&1 || apt-get -y install "$1", (where $1 is the list)

share|improve this question

apt-get will fairly silently skip over any package that is installed already so I'm not sure why it needs to get special treatment? ie:

root@bun:~# apt-get -y install  vlc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
vlc is already the newest version.

Is there a particular reason this won't work for you as-is?

share|improve this answer
I guess I could. The reason I didn't consider it first-off is I use this as part of an automated script that does all my configuration (all config files, network setup etc.). That means whenever I change anything at all I need to re-run this script on two separate machines so wanted it to run in as short a time as possible. Running apt-get, especially on the laptop which might not always have an internet connection, is quite heavy-weight in terms of the time it takes. – happyskeptic Jan 24 '12 at 14:37
Further to the above this works on rpm-based systems with rpm -q, it simply looks in the local db of installed packages and is very quick, and importantly doesn't need to time out in case there is no network. – happyskeptic Jan 24 '12 at 14:39

For a live session setup script, I had something like this:

# returns 1 if the package was already installed and 0 otherwise. The first
# argument is the package name to be checked (and installed if not already).
# other arguments are passed to apt-get
try_install() {
    dpkg -l "$1" | grep -q ^ii && return 1
    apt-get -y install "$@"
    return 0

if try_install openssh-server; then
    sed /etc/ssh/sshd_config 's/UsePAM yes/UsePAM no/' -i
    reload ssh
try_install screen && wget -O ~/.screenrc
# passing extra options and package names to apt-get
try_install firefox --no-install-recommends firefox-kde-support

If an application was already installed, I assumed it to be configured.

share|improve this answer

You may try saving a list of already installed packages via dpkg --get-selections > installed-software

Now, you may use other tools to find which packages should be installed, e.g:

vadik@ubuntu:~$ cat installed-software | grep "indicator-weather"
indicator-weather               install
vadik@ubuntu:~$ cat installed-software | grep "indicator-cpufreq"

As you may see, you can determine whether indicator-weather or indicator-cpufreq are installed using grep

share|improve this answer

I went back over my asked questions on this site and realised I never posted the commands I ended up using:

export DEBIAN_FRONTEND=noninteractive # stop annoying prompts
dpkg -s "$@" > /dev/null 2>&1 || apt-get -qq -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install "$@"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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