Apt get command examples to manage packages on Ubuntu/Debian

By | April 1, 2023

Manage packages from the commandline

The more common way to manage packages or softwares is from Synaptic Package Manager. Its the easiest gui tool to install and remove software from your debian/ubuntu system.

However for those who prefer the console, there are plenty of tools to do the job as easily. In this tutorial we are going to look into apt, the package management tool used on ubuntu.

Though I was a Synaptic user earlier, when I started with the command line tools, I found the command line tools easier and faster.

On Ubuntu there are 3 main commands to manage packages. These are dpkg, apt-* and aptitude.

In this tutorial we shall take a quick look at some of the examples of the apt-get command that can be used to install and manage packages.

Apt-get command examples

The apt-get command can be used to install, remove, update packages. It can also upgrade the entire distro to a new version.

1. Say Moo with apt-get

This is the most important thing to do with apt-get. That is, to say Moo....

$ apt-get moo
         (__) 
         (oo) 
   /------\/ 
  / |    ||   
 *  /\---/\ 
    ~~   ~~   
...."Have you mooed today?"...
$

2. Install a new package

This is the most common command. The apt-get install command will install the package and pull in all necessary dependencies, that is other packages that are needed to run the current required package. Usage is again, very simple

$ sudo apt-get install apache2

To install multiple packages in one go, just mention all of them separated by space. For example:

$ sudo apt-get install php-fpm php-curl

3. Upgrade selected package

To upgrade a selected package just do install again

$ sudo apt-get install nginx

This will upgrade the package if updates are available from the repository.

4. Getting the source code of a package

The source code of any package can be downloaded using the following command

$ apt-get source gbrainy

5. Remove an installed package

Use the remove option with apt-get to remove a package

$ apt-get remove apache2

Apt-cache command examples

Apt-cache is a command that comes with apt-get and can be used to check information about packages.

1. Find dependencies of a package

To find the dependencies of a certain package, use the apt-cache command

$ apt-cache depends apache2
apache2
 |Depends: apache2-mpm-worker
 |Depends: apache2-mpm-prefork
 |Depends: apache2-mpm-event
  Depends: apache2-mpm-itk
  Depends: apache2.2-common
  Conflicts: apache2:i386

It will tell what extra packages does a certain package depends on and what packages conflict with it. It will also list the packages that shall be removed on installing this package and all recommended packages to install with this package.

2. Search packages

The apt-cache command can be used to search the packages from the console. This is useful when working on a remote server where there is no gui available. But moreover, the console is more powerful and faster.

$ apt-cache search nginx

The apt-cache command by default searches both the package name and the description. So to fine tune the results we need to filter it out further by using grep.

apt-cache search nginx | grep nginx
lua-nginx-memcached - Pure Lua memcached client driver for the nginx embedded Lua language
lua-nginx-redis - Pure Lua redis client driver for the nginx embedded Lua language
nginx - small, powerful, scalable web/proxy server
nginx-common - small, powerful, scalable web/proxy server - common files
nginx-doc - small, powerful, scalable web/proxy server - documentation
nginx-extras - nginx web/proxy server (extended version)
nginx-extras-dbg - nginx web/proxy server (extended version) - debugging symbols
nginx-full-dbg - nginx web/proxy server (standard version) - debugging symbols
nginx-light - nginx web/proxy server (basic version)
nginx-light-dbg - nginx web/proxy server (basic version) - debugging symbols
nginx-naxsi - nginx web/proxy server (version with naxsi)
nginx-naxsi-dbg - nginx web/proxy server (version with naxsi) - debugging symbols
nginx-naxsi-ui - nginx web/proxy server - naxsi configuration front-end
nginx-full - nginx web/proxy server (standard version)

The apt-cache search command supports regular expression.

$ apt-cache search ^nginx$
nginx - small, powerful, scalable web/proxy server
nginx-extras - nginx web/proxy server (extended version)
nginx-light - nginx web/proxy server (basic version)
nginx-naxsi - nginx web/proxy server (version with naxsi)
nginx-naxsi-ui - nginx web/proxy server - naxsi configuration front-end
nginx-full - nginx web/proxy server (standard version)
apt-cache show <package> 
Shows the full description of <package>.

apt-cache showpkg <package> 
Shows a lot more detail about <package>, and its relationships to other packages.

3. Which repository does a package belong to

Users often add extra repositories to install software from other sources. If we want to find out, which repository a package is coming from then the apt-cache command can tell that.

$ apt-cache policy wine
wine:
  Installed: (none)
  Candidate: 1.4.1-0ubuntu5
  Version table:
     1.4.1-0ubuntu5 0
        500 http://in.archive.ubuntu.com/ubuntu/ raring/universe amd64 Packages

The above output shows that the package wine is provided by the raring ubuntu repository.
Lets take another example

$ apt-cache policy google-chrome-stable
google-chrome-stable:
  Installed: 28.0.1500.45-r205727
  Candidate: 28.0.1500.70-r209565
  Version table:
     28.0.1500.70-r209565 0
        500 http://dl.google.com/linux/chrome/deb/ stable/main amd64 Packages
 *** 28.0.1500.45-r205727 0
        100 /var/lib/dpkg/status

The above output shows that google chrome is provided by http://dl.google.com/linux/chrome/deb/ repository.

Apt-add-repository command

The software sources are stored in the file called /etc/apt/sources.list. So if you need to add a new repository

$ sudo apt-add-repository ppa:lubuntu-desktop/ppa

Or if its a full url then

add-apt-repository 'deb uri distribution [component1] [component2] [...]'

For example

$ add-apt-repository 'deb http://extras.ubuntu.com/ubuntu quantal main'
$ add-apt-repository 'deb-src http://extras.ubuntu.com/ubuntu quantal main'

Upgrade distro

The entire distro upgrade can be done from the terminal as well. For example when you need to upgrade ubuntu on your server. Here is the command

# prepare the system
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade

# upgrade distro
sudo apt-get install update-manager-core
sudo do-release-upgrade

Easy!!

List the repositories

To list the current repositories read the /etc/apt/sources.list and /etc/apt/sources.list.d/* files from the commandline and use grep to filter accordingly. Here are few examples

The following will list the deb repositories (and not the deb-src) from /etc/apt/sources.list file.

$ cat /etc/apt/sources.list | grep  "^deb\s"
deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring universe
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates universe
deb http://in.archive.ubuntu.com/ubuntu/ raring multiverse
.....

The following will list the deb repositories (and not the deb-src) from /etc/apt/sources.list file and /etc/apt/sources.d/* files.

$ grep -h "^deb\s" /etc/apt/sources.list /etc/apt/sources.list.d/*
deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring universe
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates universe
deb http://in.archive.ubuntu.com/ubuntu/ raring multiverse
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates multiverse
.....

To list both deb and deb-src repositories

$ grep -h ^deb /etc/apt/sources.list /etc/apt/sources.list.d/*
deb http://in.archive.ubuntu.com/ubuntu/ raring main restricted
deb-src http://in.archive.ubuntu.com/ubuntu/ raring main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted
deb-src http://in.archive.ubuntu.com/ubuntu/ raring-updates main restricted
deb http://in.archive.ubuntu.com/ubuntu/ raring universe

So njoye the terminal

Conclusion

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

4 Comments

Apt get command examples to manage packages on Ubuntu/Debian
  1. KAVITA TABBASSUM, PHD SCHOLAR

    i have a problem in installing simics simulator on unbuntu please guide me in this regard i am new to unbunto want to know about basic terminal commands used in installation

  2. Helena

    Hi Silver,
    How to create a script
    to determine which packages from official Ubuntu repositories need to be
    updated (in respect to current state) and to download these packages.

  3. Richard

    Hi Silver,

    If we add a new repository to /etc/apt/sources.list and install the required package say pakage-A the, After say 2 months later, if we just execute:

    # prepare the system
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get dist-upgrade

    # upgrade distro
    sudo apt-get install update-manager-core
    sudo do-release-upgrade

    would package-A be updated just based on the above commands?

    Much appreciated your advice. Thanks in advance.

    richard

    1. Silver Moon

      When doing distro upgrades, all non-standard repositories are disabled.

      After the distro upgrade completes you have to update the repository url in sources.list to match the new ubuntu version

      and again do apt-get update and apt-get upgrade to upgrade the package-A

Leave a Reply

Your email address will not be published. Required fields are marked *