OSETC TECH

How to Use Apt-get or Apt Commands on Ubuntu 16.04/18.04 Linux

This post will guide you how to manage packages with Apt-get or Apt commands on Ubuntu 16.04/18.04 Linux. How do I update the Package database with apt-get command? How to Install/remove/upgrade package with Apt-get command on Ubuntu Linux. How to search for the available packages with apt command.

Normally, you can install the software through the graphical Ubuntu software Manager tool on Ubuntu Linux. And you can also manage your software via the command line interface with apt-get or apt tool.

What is Apt-get or Apt


apt provides a high-level command line interface for the package management system. It is intended as an end user interface and enables some options better suited for interactive usage by default compared to more specialized APT tools like apt-get and apt-cache. And it is the main package management system in Debian Linux or Debian-based Linux distributions like Ubuntu.

The apt-get tool is a powerful and free package management utility, and it can be used to install new packages, removing the installed packages, upgrading the installed packages or upgrading the entire Ubuntu operating system. Apt-get is the command-line tool for handling packages, and may be considered the user’s “back-end” to other tools using the APT library.

Update the Package Database with Apt-get


Before doing any package operations with apt-get or apt tool on your Ubuntu Linux, you need to ensure that the local copy of the APT database is up-to-date. Or the system do not know if there are the newer version of packages available or not. So you can update the APT database with the following apt-get command, type the following command with the administrative privilege (sudo) on your command line interface:

$ sudo apt-get update

Outputs:

devops@devops-osetc:~$ sudo apt-get update
Hit:1 https://mirrors.aliyun.com/ubuntu bionic InRelease
Get:2 https://mirrors.aliyun.com/ubuntu bionic-security InRelease [88.7 kB]
Get:3 https://mirrors.aliyun.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 https://mirrors.aliyun.com/ubuntu bionic-proposed InRelease [242 kB]
Get:5 https://mirrors.aliyun.com/ubuntu bionic-backports InRelease [74.6 kB]
Reading package lists... Done

From the above output, you should see a list of servers that we are getting the information from. Now your package indexes should be up-to-date.

Note:apt-get update” is used to resynchronize the package index files from their sources. The indexes of available packages are fetched from the location(s) specified in /etc/apt/sources.list. For example, when using a Debian archive, this command retrieves and scans the Packages.gz files, so that information about new and updated packages is available. An update should always be performed before an upgrade.

Upgrade the Installed Packages with Apt-get


Once your package indexes have been updated on your Ubuntu system, you can try to upgrade the packages to the latest version on your system with the following command:

$ sudo apt-get upgrade

Or

$ sudo apt-get dist-upgrade

Outputs:

devops@devops-osetc:~$ sudo apt-get upgrade
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

If you only want to upgrade a specified package on your system, you can use the following command:

$ sudo apt-get upgrade [package_name]

Install a New Packages with Apt-get


If you know the name of a package that you want to install on your Ubuntu Linux, you can use the following command to install it, type:

$ sudo apt-get install [package_name]

Assuming that you want to install a package called wget on your system, you can install it by running the following command:

$ sudo apt-get install wget

Outputs:

devops@devops-osetc:~$ sudo apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
wget
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 316 kB of archives.
After this operation, 954 kB of additional disk space will be used.
Get:1 https://mirrors.aliyun.com/ubuntu bionic-security/main amd64 wget amd64 1.19.4-1ubuntu2.1 [316 kB]
Fetched 316 kB in 2s (176 kB/s)
Selecting previously unselected package wget.
(Reading database ... 213924 files and directories currently installed.)
Preparing to unpack .../wget_1.19.4-1ubuntu2.1_amd64.deb ...
Unpacking wget (1.19.4-1ubuntu2.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Setting up wget (1.19.4-1ubuntu2.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

If you want to install multiple packages at one time on your system, you can install them by using the following syntax:

$ sudo apt-get install [package1] [package2] [package3] ...

You should know that the apt-get command is not only install the requested software, but also install any software needed to install. and I you try to install an installed package on your system, it will check the package indexes if there is a newer version of that package, if so, it will upgrade the installed package to its newer version. For example, you can try to reinstall wget package with the following command:

$ sudo apt-get install wget

Outptus:

devops@devops-osetc:~$ sudo apt-get install wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
wget is already the newest version (1.19.4-1ubuntu2.1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

From the above output, you should see that apt-get command will check the database firstly, then return the result.

If you want to install a specific version of a package, you can use the following command:

$ sudo apt-get install [package-name]=[version_number]

Get The Source code for An Installed Package


If you want to get the source code for an installed package on your Ubuntu system, you can use the following command to get it, type:

$ sudo apt-get source package_name

For example, you want to get the source code of wget package, type:

$ sudo apt-get source wget

Outputs:

devops@devops-osetc:~$ sudo apt-get source wget
Reading package lists... Done
Need to get 4,378 kB of source archives.
Get:1 http://mirrors.aliyun.com/ubuntu bionic-security/main wget 1.19.4-1ubuntu2.1 (dsc) [2,226 B]
Get:2 http://mirrors.aliyun.com/ubuntu bionic-security/main wget 1.19.4-1ubuntu2.1 (tar) [4,311 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic-security/main wget 1.19.4-1ubuntu2.1 (asc) [1,241 B]
Get:4 http://mirrors.aliyun.com/ubuntu bionic-security/main wget 1.19.4-1ubuntu2.1 (diff) [63.7 kB]
Fetched 4,378 kB in 8s (528 kB/s)
dpkg-source: info: extracting wget in wget-1.19.4
dpkg-source: info: unpacking wget_1.19.4.orig.tar.gz
dpkg-source: info: unpacking wget_1.19.4-1ubuntu2.1.debian.tar.xz
dpkg-source: info: applying wget-doc-remove-usr-local-in-sample.wgetrc
dpkg-source: info: applying wget-doc-remove-usr-local-in-wget.texi
dpkg-source: info: applying wget-passive_ftp-default
dpkg-source: info: applying disable-SSLv3.patch
dpkg-source: info: applying CVE-2018-0494.patch

devops@devops-osetc:~$ ls wget*
wget_1.19.4-1ubuntu2.1.debian.tar.xz wget_1.19.4-1ubuntu2.1.dsc wget_1.19.4.orig.tar.gz wget_1.19.4.orig.tar.gz.asc

Note: if you get the following error while you get package source code, you should remove the comment from the deb-src line from the /etc/apt/sources.list file.

Reading package lists... Done
E: You must put some 'source' URIs in your sources.list

Reinstall an Installed Package


If an installed application isn’t working or has broken problem, you can easily reinstall it to fix the problem with the following command:

$ sudo apt-get install wget --reinstall

Outputs:

devops@devops-osetc:~$ sudo apt-get install wget --reinstall
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 0 B/316 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 213934 files and directories currently installed.)
Preparing to unpack .../wget_1.19.4-1ubuntu2.1_amd64.deb ...
Unpacking wget (1.19.4-1ubuntu2.1) over (1.19.4-1ubuntu2.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Setting up wget (1.19.4-1ubuntu2.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

Remove a Package with Apt-get


If you want to remove a package from your Ubuntu system, you can use the following command:

$ sudo apt-get remove package-name

For example, you want to remove a package called wget, type:

$ sudo apt-get remove wget

Ouputs:

devops@devops-osetc:~$ sudo apt-get remove wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
wget
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 954 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 213932 files and directories currently installed.)
Removing wget (1.19.4-1ubuntu2.1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...

Note: this command only removes the wget package, and it will keep the configuration files on your system. So in cased you want to reinstall the same package on your system again, the configuration file still can be used again. And If you want to remove both the pakcage and its configuration file, you can use the following command:

$ sudo apt-get purge wget

If you want to remove any packages that were installed automatically to support another application that are no longer needed, you can use the following command to remove any unused configuration and packages you do not need, type:

$ sudo apt-get autoremove

Outputs:

devops@devops-osetc:~$ sudo apt-get autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Search for a Package with Apt-cache


You should know that the apt-get command can be used to update, upgrade, install and remove packags. and there is another apt command called apt-cache in the Ubuntu Linux, it can be used to query the package database to get the package information. And the apt-cache command does not need the administrative privileges, so you do not need to use the sudo command before of your running command.

$ apt-cache search [package name keyword]

For example, you want to search for a package called wget, you can issue the following command:

$ apt-cache search "wget"

Outputs:

devops@devops-osetc:~$ apt-cache search wget
devscripts - scripts to make the life of a Debian Package maintainer easier
wget - retrieves files from the web
abcde - A Better CD Encoder
apt-mirror - APT sources mirroring tool
axel - light command line download accelerator
filetea - Web-based file sharing system
getdata - management of external databases
libcupt4-2-downloadmethod-wget - flexible package manager -- wget download method
libwget0 - Download library for files and recursive websites
ow-shell - shell utilities to talk to an 1-Wire owserver
puf - Parallel URL fetcher
pwget - downloader utility which resembles wget (implemented in Perl)
python-wget - pure Python download utility for Python 2
python3-wget - pure Python download utility for Python 3
snarf - command-line URL grabber
tcllib - Standard Tcl Library
texlive-latex-extra - TeX Live: LaTeX additional packages
wget2 - file and recursive website downloader
wget2-dev - development file for libwget2
wput - tiny wget-like ftp-client for uploading files

Note: this command allow you to search for terms for a package, so you do not need to know the exact the name of a package that you want to search for. And if you want to search for packages that contain your search term with their name, you can use the following command:

$ apt-cache showpkg wget

Outputs:

devops@devops-osetc:~$ apt-cache showpkg wget
Package: wget
Versions:
1.19.4-1ubuntu2.1 (/var/lib/apt/lists/mirrors.aliyun.com_ubuntu_dists_bionic-security_main_binary-amd64_Packages) (/var/lib/apt/lists/mirrors.aliyun.com_ubuntu_dists_bionic-updates_main_binary-amd64_Packages) (/var/lib/dpkg/status)
......
Reverse Depends:
usbutils,wget 1.8.1-6
......
Dependencies:
1.19.4-1ubuntu2.1 - libc6 (2 2.17) libidn2-0 (2 0.6) libpcre3 (0 (null)) libpsl5 (2 0.16.0) libssl1.1 (2 1.1.0) libuuid1 (2 2.16) wget-ssl (0 (null)) ca-certificates (0 (null)) wget:i386 (32 (null))
1.19.4-1ubuntu2 - libc6 (2 2.17) libidn2-0 (2 0.6) libpcre3 (0 (null)) libpsl5 (2 0.16.0) libssl1.1 (2 1.1.0) libuuid1 (2 2.16) wget-ssl (0 (null)) ca-certificates (0 (null)) wget:i386 (32 (null))
Provides:
1.19.4-1ubuntu2.1 - wget:i386 (= 1.19.4-1ubuntu2.1)
1.19.4-1ubuntu2 - wget:i386 (= 1.19.4-1ubuntu2)
Reverse Provides:
wget:i386 1.19.4-1ubuntu2.1 (= 1.19.4-1ubuntu2.1)
wget:i386 1.19.4-1ubuntu2 (= 1.19.4-1ubuntu2)

View Package Information with Apt-cache


If you want to view the information of a package on the Ubuntu Linux, you can use the following command with apt-cache:

$ apt-cache show package-name

For example, viewing the information of vim package, issue the following command:

$ apt-cache show vim

Outputs:

Package: vim
Architecture: amd64
Version: 2:8.0.1453-1ubuntu1
Priority: optional
Section: editors
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Vim Maintainers <pkg-vim-maintainers@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 2785
Provides: editor
Depends: vim-common (= 2:8.0.1453-1ubuntu1), vim-runtime (= 2:8.0.1453-1ubuntu1), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libgpm2 (>= 1.20.7), libpython3.6 (>= 3.6.4~rc1), libselinux1 (>= 1.32), libtinfo5 (>= 6)
Suggests: ctags, vim-doc, vim-scripts
Filename: pool/main/v/vim/vim_8.0.1453-1ubuntu1_amd64.deb
Size: 1151956
MD5sum: 4042292f658b09ba5913da09bb52d496
SHA1: e9e923385bca6ab4441229422786c89a872df985
SHA256: 23fbbf49a0ef4c4537311efe446f82d6c6c8c3ac0c233460b669843dfa382a17
Homepage: https://vim.sourceforge.io/
Description-en: Vi IMproved - enhanced vi editor
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a version of vim compiled with a rather
standard set of features. This package does not provide a GUI
version of Vim. See the other vim-* packages if you need more
(or less).
Description-md5: 59e8b8f7757db8b53566d5d119872de8
Task: cloud-image, server
Supported: 5y

Conclusion


You should know that how to use the apt-get and apt-cache commands to manage your software package on Ubuntu 16.04/18.04 Linux server. If you want to learn more about the apt commands, you can use the following commands:

$ man apt
$ man apt-get
$ man apt-cache