Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[HowTo] Using apt from the command line interface

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

[HowTo] Using apt from the command line interface

#1 Post by Hallvor »

Skill level: Beginner

The apt command-line interface is a powerful tool that enables users to search for packages, install or remove them, and upgrade the system. This how-to guide will explain how to use the apt package manager in Debian without needing GUI package managers like Synaptic or Discover.

Since apt can do system-wide changes, it needs root or sudo privileges. If your system has root or sudo enabled depends if you set a root password during installation. If you set a root password, you can use root. If you didn't set one, you must use sudo.

Open a terminal with Ctrl + Alt + T.

You should then get a command prompt like this:

Code: Select all

hallvor@debian:~$

Using root

You can switch from user to root by typing in the following command and pressing enter. Notice the - after su.

Code: Select all

hallvor@debian:~$ su -
After the successful entry of a root password, you will change from user to root:

Code: Select all

root@debian:~#
In order to use sudo, just type sudo before your command, like this to update the package lists:

Code: Select all

hallvor@debian:~$ sudo apt update
In this scenario, you type in your user's password. Keep in mind that sudo privileges last for 15 minutes after running a sudo command. This means that if you run a command with sudo, you should not be prompted for a password again for the next 15 minutes when running subsequent sudo commands.

In this howto, we'll use root (#) for the examples.

In order to edit your sources.list, you can use the apt edit-sources-command

Code: Select all

# apt edit-sources
Then select the editor, edit the file and save and exit. To exit nano, you can press "Ctrl + X".
To exit and save in Vim:
Press "Esc" to enter command mode. Type ":wq" to write the file out to disk and quit vim. Press "Enter" to execute the command.


Update the package lists

It is always a good idea to update the package lists before installing anything to ensure that you have the latest package information. If you have edited your sources.list

Code: Select all

# apt update

Install upgrades

If there are any updates, you can install them with

Code: Select all

# apt upgrade

Search for an application


We can search for an application with the search command. In order to search for the multimedia player VLC, we can use this command:

Code: Select all

# apt search vlc
Apt will provide a list of plugins and related packages.

Some searches will give too many results, resulting in output that is only partial. One solution is to pipe the output into less:

Code: Select all

# apt search vlc | less
This will display the search results for vlc in a pager, allowing you to view the output one page at a time. You can navigate through the output by using the PgUp and PgDn keys on your keyboard.

To exit less, you can press the q key.


Show information about a package

The show-command will give extensive information about a given package, including its dependencies, installation and download size.

Code: Select all

# apt show vlc

Package: vlc
Version: 3.0.18-0+deb11u1
Priority: optional
Section: video
Maintainer: Debian Multimedia Maintainers <debian-multimedia@lists.debian.org>
Installed-Size: 237 kB
Provides: mp3-decoder
Depends: vlc-bin (= 3.0.18-0+deb11u1), vlc-plugin-base (= 3.0.18-0+deb11u1), vlc-plugin-qt (= 3.0.18-0+deb11u1), vlc-plugin-video-output (= 3.0.18-0+deb11u1)
Recommends: vlc-l10n (= 3.0.18-0+deb11u1), vlc-plugin-access-extra (= 3.0.18-0+deb11u1), vlc-plugin-notify (= 3.0.18-0+deb11u1), vlc-plugin-samba (= 3.0.18-0+deb11u1), vlc-plugin-skins2 (= 3.0.18-0+deb11u1), vlc-plugin-video-splitter (= 3.0.18-0+deb11u1), vlc-plugin-visualization (= 3.0.18-0+deb11u1)
Suggests: vlc-plugin-fluidsynth (= 3.0.18-0+deb11u1), vlc-plugin-jack (= 3.0.18-0+deb11u1), vlc-plugin-svg (= 3.0.18-0+deb11u1)
Homepage: https://www.videolan.org/vlc/
Tag: hardware::storage, hardware::storage:cd, hardware::storage:dvd,
 hardware::video, implemented-in::c, implemented-in::c++,
 interface::commandline, interface::graphical, interface::x11,
 protocol::ipv6, protocol::ssl, role::program, scope::application,
 scope::utility, sound::compression, sound::player, uitoolkit::ncurses,
 uitoolkit::qt, use::converting, use::playing, works-with-format::TODO,
 works-with-format::mp3, works-with-format::oggtheora,
 works-with-format::oggvorbis, works-with-format::wav, works-with::TODO,
 works-with::audio, works-with::video, x11::application
Download-Size: 146 kB
APT-Manual-Installed: yes
APT-Sources: http://deb.debian.org/debian bullseye/main amd64 Packages
Description: multimedia player and streamer
 VLC is the VideoLAN project's media player. It plays MPEG, MPEG-2, MPEG-4,
 DivX, MOV, WMV, QuickTime, WebM, FLAC, MP3, Ogg/Vorbis files, DVDs, VCDs,
 podcasts, and multimedia streams from various network sources.
 .
 VLC can also be used as a streaming server that duplicates the stream it
 reads and multicasts them through the network to other clients, or serves
 them through HTTP.
 .
 VLC has support for on-the-fly transcoding of audio and video formats, either
 for broadcasting purposes or for movie format transformations. Support for
 most output methods is provided by this package, but features can be added by
 installing additional plugins:
  * vlc-plugin-access-extra
  * vlc-plugin-fluidsynth
  * vlc-plugin-jack
  * vlc-plugin-notify
  * vlc-plugin-samba
  * vlc-plugin-skins2
  * vlc-plugin-svg
  * vlc-plugin-video-splitter
  * vlc-plugin-visualization


Installing an application

In order to install an application, we can use the apt install command. In order to install vlc and international translations, we can issue the following command:

Code: Select all

# apt install vlc vlc-l10n
Notice that you can install several packages as long as you have space between them.


Removing an application


In order to remove an application, we can use the apt remove command, for instance like this:

Code: Select all

# apt remove vlc vlc-l10n
A word of caution: Please don't blindly confirm removal of packages. A single package can be part of a larger metapackage, so if you remove a package, the system will ask you to remove all the packages within that metapackage. This may give you a broken system.


Purging an application


In Debian, the purge command is used to completely remove an application along with its configuration files. This means that if you reinstall the program at a later time, it will be installed with its default settings.

Code: Select all

# apt purge vlc
Removing the configuration files may be a good idea if the application isn't working as expected.


View the apt log entries


Apt logs are located in /var/log/apt/history.log. We can view them conveniently with less:

Code: Select all

# less /var/log/apt/history.log

Saving a list of all your installed packages


You can use apt to save a list of all your installed packages.

Code: Select all

# apt list --installed > packages.txt
The list is saved to a file called packages.txt, and you can view the content of the package with

Code: Select all

# nano packages.txt

As mentioned, apt is a powerful tool that lets you set up your sources.list, view, install, remove, purge and save lists of your installed packages. I hope this was useful.



Edits:
10.04.23: Edited default directory after changing to root account. Thanks to itmicp for pointing it out. Added info about piping search results into less. Thanks to CynicalDebian.
11.04.23: Added location of apt's log. Thanks to CwF.
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

CynicalDebian
Posts: 263
Joined: 2023-03-02 05:26
Location: USA
Has thanked: 50 times
Been thanked: 60 times
Contact:

Re: [HowTo] Using apt from the command line interface

#2 Post by CynicalDebian »

Nice guide! But apt search sometimes gives unusable output because of how long it is, perhaps add a section on piping into less and scrolling up with PG UP and PG DN? New users of apt may not be familiar with terminal pagers.
Be seeing you...

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Using apt from the command line interface

#3 Post by Hallvor »

Added. Thanks!
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

CwF
Global Moderator
Global Moderator
Posts: 2719
Joined: 2018-06-20 15:16
Location: Colorado
Has thanked: 41 times
Been thanked: 201 times

Re: [HowTo] Using apt from the command line interface

#4 Post by CwF »

Don't forget 'scrollback'

Pagers lock the user into its way, if using a terminal like xfce4-terminal there is the scrollback setting I've set to 1000 lines, and use it. There is also the 'unlimited' setting!

I thought there is a apt history log also for review, and maybe capture the operation in 'script' piped to file to create your own history.

itmicp
Posts: 38
Joined: 2013-05-11 04:45
Location: France
Has thanked: 6 times
Been thanked: 6 times

Re: [HowTo] Using apt from the command line interface

#5 Post by itmicp »

Hi

This is not an apt command line
but just a way I use the apt log files.

To see the list of packages I asked to install without listing the ones installed during the system installation,
I run the following command line:

Code: Select all

zgrep -h 'Commandline: apt install' /var/log/apt/history.log{,.[0-9]*.gz} | sed ':a;N;$!ba;s/\n\|Commandline: apt install/ /g; s/\( \)*/\1/g;s/ -y / /;'
But I should also take into account the packages that have been uninstalled…

User avatar
kent_dorfman766
Posts: 540
Joined: 2022-12-16 06:34
Location: socialist states of america
Has thanked: 59 times
Been thanked: 70 times

Re: [HowTo] Using apt from the command line interface

#6 Post by kent_dorfman766 »

<CTRL><ALT><T>?

What is this witchcraft? I think thee is in league with Lucifer, or at least with his minions: Gnome.

User avatar
Hallvor
Global Moderator
Global Moderator
Posts: 2044
Joined: 2009-04-16 18:35
Location: Kristiansand, Norway
Has thanked: 151 times
Been thanked: 212 times

Re: [HowTo] Using apt from the command line interface

#7 Post by Hallvor »

kent_dorfman766 wrote: 2023-04-11 23:41 <CTRL><ALT><T>?

What is this witchcraft? I think thee is in league with Lucifer, or at least with his minions: Gnome.
:lol:
[HowTo] Install and configure Debian bookworm
Debian 12 | KDE Plasma | ThinkPad T440s | 4 × Intel® Core™ i7-4600U CPU @ 2.10GHz | 12 GiB RAM | Mesa Intel® HD Graphics 4400 | 1 TB SSD

Post Reply