APT - Advanced Packaging Tool

  • by Craig Buchek
  • St. Louis GNU/Linux Users Group
  • July 21, 2011

What Is APT?

  • Debian's advanced packaging tool
  • Handles dependencies well
  • The best package manager

Finding Packages

Getting Package Details

apt-cache show $PACKAGE_NAME
apt-cache showpkg $PACKAGE_NAME
 
aptitude show $PACKAGE_NAME
 
dpkg --info $PACKAGE_NAME.deb

Installing Packages

sudo apt-get install $PACKAGE_NAME
 
# Include dependencies without prompting:
sudo apt-get install -y $PACKAGE_NAME
 
sudo dpkg --install $PACKAGE_NAME.deb

Adding a Repository

REPOSITORY_NAME='mongodb-10gen'
REPOSITORY_KEY_URL=''
REPOSITORY_KEY_SERVER='keyserver.ubuntu.com'
REPOSITORY_KEY_ID='7F0CEB10'
REPOSITORY_REPO='http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen'
 
if [ ! -z "$REPOSITORY_KEY_URL" ]; then
  wget -q -O - $REPOSITORY_KEY_URL | sudo apt-key add -
else
  sudo apt-key adv --keyserver $REPOSITORY_KEY_SERVER --recv $REPOSITORY_KEY_ID
fi
 
sudo sh -c "echo 'deb $REPOSITORY_REPO' > /etc/apt/sources.list.d/$REPOSITORY_NAME.list"
sudo apt-get update
 
# In recent versions of Ubuntu, can use this to add to sources.list:
sudo add-apt-repository "deb $REPOSITORY_REPO"

Updating Packages

# Update repository index:
sudo apt-get update
# Upgrade packages that have updates:
sudo apt-get upgrade
# Upgrade packages, and install or remove other packages required by a package upgrade:
sudo apt-get dist-upgrade
 
# Update repository index:
sudo aptitude update
# Upgrade packages that have updates:
sudo aptitude upgrade
# Upgrade packages, and install or remove other packages required by a package upgrade:
sudo aptitude dist-upgrade

Which Package Installed a File?

dpkg --search $FILENAME_PATTERN # -S
 
dlocate -S $FILENAME_PATTERN

Which Package Will Install a File?

# Requires that apt-file package has been installed:
apt-file search $FILENAME_PATTERN

What Files Will a Package Install?

dpkg --contents $PACKAGE_NAME.deb

What Packages Have Been Installed?

dpkg --list

What Files Did a Package Install?

dpkg --listfiles $PACKAGE_NAME # -L

Installing From a Source Tarball

wget $PACKAGE_TARBALL_URL
tar tfz $PACKAGE_TARBALL
tar xfz $PACKAGE_TARBALL
cd $PACKAGE_NAME-$PACKAGE_VERSION
make
sudo chkinstall make install

Configuring a Package

sudo dpkg-reconfigure $PACKAGE_NAME
 
sudo debconf
  • Edit files in /etc/default/$SERVICE_NAME
  • Edit files in /etc/$PACKAGE_NAME or similarly-named files

Starting a Service

sudo /etc/init.d/$SERVICE_NAME start
 
sudo invoke-rc.d $SERVICE_NAME start
 
# Only if chkconfig, sysvconfig, or sysvinit-tools package has been installed:
sudo service $SERVICE_NAME start

Restarting/Reloading a Service

sudo /etc/init.d/$SERVICE_NAME reload
sudo /etc/init.d/$SERVICE_NAME restart
 
sudo invoke-rc.d $SERVICE_NAME reload
sudo invoke-rc.d $SERVICE_NAME restart
 
# Only if chkconfig, sysvconfig, or sysvinit-tools package has been installed:
sudo service $SERVICE_NAME reload
sudo service $SERVICE_NAME restart

Removing a Package

sudo apt-get remove $PACKAGE_NAME
sudo apt-get purge $PACKAGE_NAME
 
sudo apt-get autoremove
 
sudo dpkg --remove $PACKAGE_NAME # -r
sudo dpkg --purge $PACKAGE_NAME  # -P
 
sudo aptitude remove $PACKAGE_NAME
sudo aptitude purge $PACKAGE_NAME

Send an Email When Updates Are Available

sudo apt-get install -y apticron

Synaptic

  • GUI front-end
  • Allows visually selecting packages
  • Shows package info
  • Other front-ends available
    • Usually distro-specific

Advanced Topics

  • Forcing / Ignoring Dependencies
  • Diverting a Package
dpkg-divert --add filename
  • Alternatives
sudo update-alternatives
  • Pinning a Version
  • Preventing Installation of a Package
sudo aptitude hold package_name
 
echo "$PACKAGE_NAME hold" | sudo dpkg --set-selections
  • Unpacking a DEB file
dpkg-deb --contents package_name.deb
dpkg-deb --extract package_name.deb
  • Upgrading to a new version of the distro
  • Installing From a Source Package
apt-get source $PACKAGE_NAME
cd $PACKAGE_NAME-$PACKAGE_VERSION
sudo apt-get build-dep $PACKAGE_NAME
dch -l local 'Blah blah blah'
debuild -us -uc
sudo dpkg -i ../*.deb

More Info