Ubuntu/Debian apt-get package management cheat sheet
We have various Linux/Unix distributions for various devices over world wide Ubuntu is one of them which used for various levels of devices used in Server, desktops, laptops, tablets, phones etc. Ubuntu provide wide range packages to install which helps to work and used to interact new technologies for day to day work on every level. In this post we will see how to use Ubuntu/Debian apt-get package management.
SetUP
For this i am using latest Ubuntu OS 17.10 as of now Dec 2017.
# lsb_release -d Description: Ubuntu 17.10 # uname -r 4.13.0-17-generic
Debian/Ubuntu machine provide many package management tools. With this post we will try to provide brief introduction with some examples.
- apt-get : This command used to manage package from command-line. This support install, removal, upgrade etc. It support wide range of options which make it widely used for administrative purpose and scripts.
- dpkg : This command used to install, query, removal of Debian packages.
- apt : This command is quit same as apt-get with more handy and less options which make easy for Linux beginners.
- apt-cache : This command is only used to query method from apt metadata which collect from apt update, will not changed any system’s state.
- Gui-tools : These are high levels tools used within Graphical interface
- aptitude : This is Text User interface tool used to Debian/Ubuntu Packages management.
- synaptic : This is Graphical based tools used to manage Packaging in Debian/Ubuntu Machine
So let’s start to see how to work with apt-get Ubuntu package manager to install, remove, upgrade packages on Ubuntu machine.
Installing Ubuntu packages
To Install Ubuntu package we need to use install option with apt-get command in same way as used in syntax below.
# apt-get install [Package-name]
we have same example for same mentioned below for users which has never used apt-get to install packages in Ubuntu machine.
# apt-get install bind9 Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: bind9utils libirs141 Suggested packages: bind9-doc The following NEW packages will be installed: bind9 bind9utils libirs141 0 upgraded, 3 newly installed, 0 to remove and 3 not upgraded. Need to get 604 kB of archives. After this operation, 2,996 kB of additional disk space will be used. Do you want to continue? [Y/n] y
In above command, we used to install bind9(DNS packages), it later ask to continue to install packages as it asked for, we can used -y in command itself in which case it will ask for continue it between command again, this is good while using in scripts.
Removing packages
To remove Debian/Ubuntu packages, we have to use remove option with apt-get command, This command will only remove package not its complete configuration file. This kind of features is not seen in RedHat/Fedora with Yum/RPM package manager. To remove all configuration files as well with same command we could use –purge. We could use this separately as well. Syntax show below.
Syntax
Remove Packages from Debian/Ubuntu machine # apt-get remove [Package-name] Remove configuration files from Debian/Ubuntu machine # apt-get purge [Package-name] Remove Packages and configuration files from Debian/Ubuntu machine # apt-get remove --purge [Package-name]
As shown above, we can remove packages and configuration files separately and along in apt-get commands.
Update Source index
we should always update Source list index. Which help to update apt meta-data, which get sync with source updated information that help to get updated systems apt meta-data. We could also upgrade machine further with all upgrade features and packages, in fact we should run first command apt-get updateon Ubuntu machine. Below command show update features of apt-get command.
# apt-get update Get:1 http://security.ubuntu.com/ubuntu artful-security InRelease [78.6 kB] Hit:2 http://us.archive.ubuntu.com/ubuntu artful InRelease Get:3 http://us.archive.ubuntu.com/ubuntu artful-updates InRelease [78.6 kB] Get:4 http://us.archive.ubuntu.com/ubuntu artful-backports InRelease [72.2 kB] Fetched 229 kB in 2s (112 kB/s) Reading package lists... Done
apt-get upgrade command used to upgrade any packages which has new release provided source list mentioned in /etc/apt/sources.list
# apt-get upgrade
Package upgarde
We can also do particular packages upgrade in case we know package name as mentioned below.
apt-get upgrade [packae-name]
We could install multiple packages and can also use wildcards while installing packages through apt-get command like mentioned below.
# apt-get -y install vsftpd net-tools netcat Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: netcat-traditional The following NEW packages will be installed: net-tools netcat netcat-traditional vsftpd 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded. Need to get 376 kB of archives.
In above command we saw that we could install multiple packages in series, we can also use wild-cards as well in same manner.
# apt-get -y install htop* Reading package lists... Done Building dependency tree Reading state information... Done Note, selecting 'htop' for glob 'htop*' The following NEW packages will be installed: htop 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Use tab to auto-complete
we could also use tab to complete packages name while try installing packages with apt-get install command like below.
# apt-get install bind bind9 bind9-doc bind9-dyndb-ldap bind9-host bind9utils bindechexascii bindfs bindgraph
like above way, apt-get will show all packages that start with bind and now we could install as per requirement.
Check and update package cache and broken dependencies
This is good to recheck again any update in packages information that it will updates the package cache and checks for broken dependencies like below mentioned way.
# apt-get check Reading package lists... Done Building dependency tree Reading state information... Done
Download packages
We can also download packages, sometime we need to have packages locally which could use in other way or purpose. In these cases we could download packages from Source repositories.
# apt-get download mytop Get:1 http://us.archive.ubuntu.com/ubuntu artful/universe amd64 mytop all 1.9.1-4 [31.1 kB] Fetched 31.1 kB in 1s (22.9 kB/s)
Print uris
Through this way we can print uris instead of download and install deb files, this could way to analyze and use in automation works. We can use this option in below mentioned way.
# apt-get --print-uris install unzip Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: zip The following NEW packages will be installed: unzip 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 167 kB of archives. After this operation, 558 kB of additional disk space will be used. 'http://us.archive.ubuntu.com/ubuntu/pool/main/u/unzip/unzip_6.0-21ubuntu1_amd64.deb' unzip_6.0-21ubuntu1_amd64.deb 167058 MD5Sum:0011dca8022a592148461fb2060acbd6
Remove packages that no longer required
autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed.
# apt-get autoremove Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libconfig-inifiles-perl 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 133 kB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 123323 files and directories currently installed.) Removing libconfig-inifiles-perl (2.94-1) ... Processing triggers for man-db (2.7.6.1-2) ...
Dry-run Installation/Removal/Upgrade
We can also Dry-run for installation/Removal/Upgrade packages ,this will provide great detail of packages information which will be processed though this. We could do same with two ways mentioned below.
# apt-get -s install slapd Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libltdl7 libodbc1 Suggested packages: libmyodbc odbc-postgresql tdsodbc unixodbc-bin ldap-utils libsasl2-modules-gssapi-mit | libsasl2-modules-gssapi-heimdal The following NEW packages will be installed: libltdl7 libodbc1 slapd 0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded. Inst libltdl7 (2.4.6-2 Ubuntu:17.10/artful [amd64]) Inst libodbc1 (2.3.4-1 Ubuntu:17.10/artful [amd64]) Inst slapd (2.4.45+dfsg-1ubuntu1 Ubuntu:17.10/artful [amd64]) Conf libltdl7 (2.4.6-2 Ubuntu:17.10/artful [amd64]) Conf libodbc1 (2.3.4-1 Ubuntu:17.10/artful [amd64]) Conf slapd (2.4.45+dfsg-1ubuntu1 Ubuntu:17.10/artful [amd64])
we can do removal/purge in same way
# apt-get -s purge htop Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: htop* 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. Purg htop [2.0.2-1]
We can also perform upgrade dry-run as in same way, we could also do same with –assume-no. This will also do same thing just abort before completion.
# apt-get --assume-no purge htop Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: htop* 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 216 kB disk space will be freed. Do you want to continue? [Y/n] N Abort.
See process progress
We can see process progress within apt-get command, like in install/Removal/Purge/upgrade. This will provide nice number percentage for progress which easy to analyze process status.
Installation progress # apt-get --show-progress install htop Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: htop 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 0 B/76.6 kB of archives. After this operation, 216 kB of additional disk space will be used. Selecting previously unselected package htop. (Reading database ... 122156 files and directories currently installed.) Preparing to unpack .../htop_2.0.2-1_amd64.deb ... Progress: [ 0%] Progress: [ 16%] Unpacking htop (2.0.2-1) ... Progress: [ 33%] Progress: [ 50%] Processing triggers for mime-support (3.60ubuntu1) ... Processing triggers for man-db (2.7.6.1-2) ... Setting up htop (2.0.2-1) ... Progress: [ 66%] Progress: [ 83%] Purge process progress # apt-get --show-progress purge htop Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: htop* 0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded. After this operation, 216 kB disk space will be freed. Do you want to continue? [Y/n] y (Reading database ... 122165 files and directories currently installed.) Removing htop (2.0.2-1) ... Progress: [ 0%] Progress: [ 16%] Progress: [ 33%] Progress: [ 50%] Progress: [ 66%] Progress: [ 83%] Processing triggers for mime-support (3.60ubuntu1) ... Processing triggers for man-db (2.7.6.1-2) ...
Clean cache files
clean clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.
# ls /var/cache/apt/archives/ galera-3_25.3.20-1_amd64.deb lock mariadb-common_10.1.25-1_all.deb mariadb-server-core-10.1_10.1.25-1_amd64.deb libaio1_0.3.110-4_amd64.deb mariadb-client-10.1_10.1.25-1_amd64.deb mariadb-server-10.1_10.1.25-1_amd64.deb partial libjemalloc1_3.6.0-10_amd64.deb mariadb-client-core-10.1_10.1.25-1_amd64.deb mariadb-server_10.1.25-1_all.deb socat_1.7.3.2-1_amd64.deb # apt-get clean # ls /var/cache/apt/archives/ lock partial
We could also clean it with autoclean options which also same like clean, difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period without it growing out of control.
# apt-get autoclean Reading package lists... Done Building dependency tree Reading state information... Done Del firefox 57.0+build4-0ubuntu0.16.04.6 [43.6 MB] Del python3-problem-report 2.20.1-0ubuntu2.12 [9,860 B] Del libxml2-utils 2.9.3+dfsg1-1ubuntu0.4 [35.0 kB] Del lxd-client 2.0.11-0ubuntu1~16.04.2 [1,885 kB] Del linux-libc-dev 4.4.0-101.124 [844 kB] Del linux-generic 4.4.0.103.108 [1,790 B] Del python3-apport 2.20.1-0ubuntu2.12 [79.4 kB] Del apport-gtk 2.20.1-0ubuntu2.12 [9,526 B] Del firefox-locale-en 57.0+build4-0ubuntu0.16.04.6 [692 kB] Del firefox 57.0+build4-0ubuntu0.16.04.5 [43.6 MB] Del libxml2-dev 2.9.3+dfsg1-1ubuntu0.4 [743 kB] Del firefox-locale-en 57.0+build4-0ubuntu0.16.04.5 [692 kB] Del python-libxml2 2.9.3+dfsg1-1ubuntu0.4 [140 kB] Del firefox-locale-en 57.0.1+build2-0ubuntu0.16.04.1 [692 kB] Del lxd 2.0.11-0ubuntu1~16.04.2 [3,508 kB] Del libxml2 2.9.3+dfsg1-1ubuntu0.4 [732 kB] Del libxml2 2.9.3+dfsg1-1ubuntu0.4 [698 kB] Del linux-libc-dev 4.4.0-103.126 [840 kB] Del linux-image-generic 4.4.0.103.108 [2,314 B] Del apport 2.20.1-0ubuntu2.12 [120 kB] Del google-chrome-stable 63.0.3239.84-1 [49.6 MB] Del firefox 57.0.1+build2-0ubuntu0.16.04.1 [43.6 MB] Del linux-headers-generic 4.4.0.103.108 [2,288 B]
There are many more options available in apt-get command that could easily make easy packaging Ubuntu/Debian Machines. We could get further help in apt-get Man Pages.
Leave a Reply