Package Management

From miki
Revision as of 12:49, 27 September 2012 by Mip (talk | contribs) (→‎Source package: managing alternatives)
Jump to navigation Jump to search

Ubuntu / Debian

Basic

Installing a package from REPOSITORIES, using aptitude:

# ----- Installing from the REPOSITORIES - APTITUDE
aptitude update                          # (OPTIONAL) update the local repository cache
aptitude install <package(s)>            # Install given package
aptitude remove <package(s)>             # Remove package (or install it if package name prefixed with - )

Note: Advantage of aptitude over apt-get is that it maintains /var/log/aptitude, and remove dependencies by default.

Installing a package from REPOSITORIES, using apt-get:

# ----- Installing from the REPOSITORIES - APT-GET
apt-get update                           # (OPTIONAL) update the local repository cache
apt-get install <package(s)>             # Install given package
apt-get install <package(s)>=<version>   # Use = to install a specific version (usually to force downgrade) !!! no space !!!
apt-get install <package(s)>/<release>   # Or use / to install from a specific distribution version !!! no space !!!
apt-get upgrade                          # Install new version of all installed package (without installing new package)
apt-get remove <package(s)>              # Remove package (or install it if package name prefixed with - )
apt-get autoremove <package(s)>          # Remove package and all its unused dependencies
apt-get purge <package(s)>               # Remove package and related configuration files
apt-get autoremove                       # Remove package installed to satisfy dependencies for some package and that are no more needed

Installing a package from a .deb PACKAGE file:

# ----- Installing from a .deb PACKAGE file
dpkg --install <debfile>                 # (or -i) Install a package from given .deb file

Querying the cache or installed packages:

# ----- Querying INSTALLED PACKAGE
dpkg --get-selections                    # Show the list of packages installed through apt-get
dpkg --get-selections | grep php         # ... filtering for some specific package keyword
dpkg -l <pattern>                        # (dpkg-query) List packages matching pattern, incl. version + description
dpkg -s <package>                        # (dpkg-query) Report status of specified package
dpkg -L <package>                        # List files delivered by a given <package>
dlocate -L <package>                     # ... same as above but much faster (require package dlocate)
dpkg -S <file>                           # List packages providing given file
dlocate -S <file>                        # ... same as above but much faster (require package dlocate)
dlocate <file>                           # ... same as dpkg -L -S combined but much faster (require package dlocate)
# ----- Querying the CACHE
apt-cache search <regex>                 # Search package cache (package name and description) for given <regex>
apt-cache search --name-only <regex>     # ... same but only search in package name
apt-cache showpkg <package(s)>           # Show version and dependencies information about given package(s)
apt-cache show <package(s)>              # Show information (description...) about given package(s)
apt-cache dotty <package(s)>|dot -Tpng|display
                                         # Generate & display dependency graph (require graphviz & imagemagick)
apt-rdepends -d <package(s)>|dot -Tpng|display
                                         # Idem (but usually much lighter)
apt-cache dotty -o APT::Cache::GivenOnly=true $(dpkg --get-selections linux*|awk '{print $1}')|dot -Tpng|display
                                         # Dependency graph restricted to package(s) given on command line
aptitude why|why-not <package>           # Explain why a package should or cannot be installed on the system
# ----- Querying a .deb PACKAGE file
dpkg --info <debfile>                    # (or -I) Show information of given .deb file
dpkg --content <debfile>                 # (or -c) Show content of given .deb file

Querying packages that are not installed yet (may require package apt-file):

apt-file update                          # (optional - needed 1st time use)
apt-file list <package>                  # Search package in repositories, and show the content
apt-file search <file>                   # Display the name of all packages within repository that contain this file
apt-file search -l <file>                # ... only print package name
apt-file search -x "/<file>$"            # ... more accurate query using regex
#View package CHANGELOG
aptitude changelog <package>             # changelog of version that was/will be installed
aptitude changelog <package>=version     # ... specific version
Note: An alternative is to use the script [{{#file: dweblocate}} dweblocate], but apt-file is more powerful actually
#!/bin/bash

# Very handy script to query online debian/ubuntu package database.
# It more or less imitates the behaviour of dlocate, but is not limited to package installed on the current system.
#
# Usage:
#
#   dweblocate -L <package>   List all files in package <package>
#   dweblocate -S <filename>  List all packages that contains <filename>
#
# Based on script at http://mydebian.blogdns.org/?p=742
# Modified by Fuujuhi, 2009.

DIST=ubuntu                      # debian | ubuntu
SUITENAME=jaunty                 # Not necessary for debian

if [ "$DIST" = "ubuntu" ] ; then
        LISTURL="http://packages.ubuntu.com/$DISTNAME/all/$2/filelist"
        SEARCHURL="http://packages.ubuntu.com/search?suite=${SUITENAME}&searchon=contents&keywords=$2"
elif [ "$DIST" = "debian" ] ; then
        LISTURL="http://packages.ubuntu.com/$DISTNAME/all/$2/filelist"
        SEARCHURL="http://packages.debian.org/search?suite=stable&searchon=contents&keywords=$2"
else
        echo -e "Unknown distribution $DIST... Aborting!"
        exit 1
fi

if [ $# -lt 2 ]; then
        echo -e "Usage:\t$0 -S file \n\t$0 -L package"
        exit 1
fi

if [ "$1" = "-L" ]; then
        wget -q "$LISTURL" -O- | sed -n '/<pre>/,/<\/pre>/ {s/^[^/]*//;/\/pre>/!p}'
elif [ "$1" = "-S" ]; then
        wget -q "$SEARCHURL" -O- | sed -n '/<table>/,/<\/table>/ { s/[[:space:]]*<a href="[^>]*>\([^<]*\)<\/a>/\1/p}'
else
        echo "Error: invalid argument \"$1\"";
    exit 2
fi

View package source repository:

apt-cache madison <package>         # Display available version of a package in each repository

View package installation history:

grep install /var/log/dpkg.log      # Also check dpkg.log.1, etc.
sudo cat /var/log/apt/terl.log      # Also check 
sudo cat /var/log/apt/history.log

Add a repository:

vi /etc/apt/sources.list            # or edit files in /etc/apt/sources.list.d/
apt-get update

Add a package authentication key:

gpg --keyserver keyserver.ubuntu.com --recv 247D1CFF  # (optional) Get the key from some keyserver
gpg --export --armor 247D1CFF | sudo apt-key add -    # Add the key

Miscellaneous:

dpkg-reconfigure popularity-contest                   # subscribe / unsubscribe to the package usage survey
apt-get moo                                           # Get super cow powers
aptitude moo                                          # Get super cow powers? try with -v, -vv

Advanced stuff

Reference:

APT with several releases

Content of file /etc/apt/apt.conf.d/00default (or /etc/apt/apt.conf if that file doesn't exist):

APT::Default-Release "testing";

This changes the order of preference for packages. Here it says default=testing=wheezy, so:

$ apt-cache policy virtualbox-osevirtualbox-ose:
  Installed: (none)
  Candidate: 4.0.2-dfsg-1
  Version table:
     4.0.4-dfsg-1+b1 0
        500 http://ftp.be.debian.org/debian/ sid/main amd64 Packages
     4.0.2-dfsg-1 0
        990 http://ftp.be.debian.org/debian/ wheezy/main amd64 Packages
     3.2.10-dfsg-1 0
        500 http://ftp.be.debian.org/debian/ squeeze/main amd64 Packages

The number (500 vs 990) is giving the priority.


APT Pinning

Reference:

RPM's

rpm -ivh package                    # Installing a package - verbose and progress bars
rpm -iv -nodeps package             # Installing a package (verbose), ignore dependencies
  • Query commands
rpm -ql package                     # List files provided by a package
  • To query a package that has not been installed, add -p option to the command:
rpm -qpl package                    # List files provided by a package

Source package

Installing from source package is usually done with

./configure
make
sudo make install

! Power Tip ! A more powerful way to install from source is to use checkinstall. checkinstall will actually create a temporary package that can then later be uninstalled with Synaptic or via the terminal:

./configure
make
sudo checkinstall                  # Creates a package and install it as if 'sudo make install' was run

#... to uninstall:
sudo dpkg -r <packagename>

Note that checkinstall can actually run any arbitrary command and monitor change on the file system.

Managing alternatives

Display current set of alternatives (e.g. here for gcc on Cygwin):

$ /usr/sbin/update-alternatives --display gcc
# gcc - status is manual.
#  link currently points to /usr/bin/gcc-3.exe
# /usr/bin/gcc-3.exe - priority 30
#  slave lib-cpp: <NONE>
#  slave cc: /usr/bin/cc-3.exe
#  slave cpp: /usr/bin/cpp-3.exe
#  slave gcov: /usr/bin/gcov-3.exe
#  slave i686-pc-cygwin-gcc-4.3.4: <NONE>
#  slave i686-pc-cygwin-gcc: /usr/bin/i686-pc-cygwin-gcc-3.exe
#  slave protoize: /usr/bin/protoize-3.exe
#  slave unprotoize: /usr/bin/unprotoize-3.exe
#  slave cpp.1.gz: /usr/share/man/man1/cpp-3.1.gz
#  slave gcc.1.gz: /usr/share/man/man1/gcc-3.1.gz
#  slave gcov.1.gz: /usr/share/man/man1/gcov-3.1.gz
#  slave ffi.3.gz: <NONE>
#  slave ffi_call.3.gz: <NONE>
#  slave ffi_prep_cif.3.gz: <NONE>
# /usr/bin/gcc-4.exe - priority 40
#  slave lib-cpp: /usr/bin/cpp-4.exe
#  slave cc: /usr/bin/gcc-4.exe
#  slave cpp: /usr/bin/cpp-4.exe
#  slave gcov: /usr/bin/gcov-4.exe
#  slave i686-pc-cygwin-gcc-4.3.4: <NONE>
#  slave i686-pc-cygwin-gcc: /usr/bin/i686-pc-cygwin-gcc-4.exe
#  slave protoize: <NONE>
#  slave unprotoize: <NONE>
#  slave cpp.1.gz: /usr/share/man/man1/cpp-4.1.gz
#  slave gcc.1.gz: /usr/share/man/man1/gcc-4.1.gz
#  slave gcov.1.gz: /usr/share/man/man1/gcov-4.1.gz
#  slave ffi.3.gz: /usr/share/man/man3/ffi-4.3.gz
#  slave ffi_call.3.gz: /usr/share/man/man3/ffi_call-4.3.gz
#  slave ffi_prep_cif.3.gz: /usr/share/man/man3/ffi_prep_cif-4.3.gz
# Current `best' version is /usr/bin/gcc-4.exe.

Create a new entry with

/usr/sbin/update-alternatives --install /usr/bin/gcc.exe gcc /usr/bin/gcc-3.exe  30 \
 --slave /usr/bin/cc.exe cc /usr/bin/cc-3.exe \
 --slave /usr/bin/cpp.exe cpp /usr/bin/cpp-3.exe \
 --slave /usr/bin/gcov.exe gcov /usr/bin/gcov-3.exe \
 --slave /usr/bin/i686-pc-cygwin-gcc.exe i686-pc-cygwin-gcc /usr/bin/i686-pc-cygwin-gcc-3.exe \
 --slave /usr/bin/protoize.exe protoize /usr/bin/protoize-3.exe \
 --slave /usr/bin/unprotoize.exe unprotoize /usr/bin/unprotoize-3.exe \
 --slave /usr/share/man/man1/cpp.1.gz cpp.1.gz /usr/share/man/man1/cpp-3.1.gz \
 --slave /usr/share/man/man1/gcc.1.gz gcc.1.gz /usr/share/man/man1/gcc-3.1.gz \
 --slave /usr/share/man/man1/gcov.1.gz gcov.1.gz /usr/share/man/man1/gcov-3.1.gz

Select an entry with

/usr/sbin/update-alternatives --auto gcc                        # Select alternative for gcc based on priority
/usr/sbin/update-alternatives --set gcc /usr/bin/gcc-3.exe      # Set gcc-3 as alternative for gcc (also set slaves, etc)
/usr/sbin/update-alternatives --config gcc                      # Select alternative via menu