Hướng dẫn sử dụng APT trên Debian/Ubuntu

Tác giả: 12/10/2022

1. Giới thiệu về lệnh apt

Lệnh apt(Advanced Package Tool) là một công cụ được sử dụng để quản lý các gói phần mềm trên các bản phân phối Linux thuộc dòng Ubuntu/Debian. Chúng ta có thể sử dụng nó để tìm và cài đặt các gói mới, cập nhật và nâng cấp gói, loại bỏ các gói, theo dõi tất cả các phần mềm được cài đặt.

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về cách sử dụng lệnh apt trên bản phân phối Ubuntu

2. Sử dụng lệnh apt

Cài đặt gói:

Để cài đặt gói chúng ta sử dụng lệnh

# sudo apt install packagename

Ví dụ sau đây là cài đặt text editor nano

# sudo apt install nano

Theo mặc định sudo apt install, sẽ nhắc chúng ta chấp nhận hoặc từ chối trước khi cài đặt các gói. Nếu bạn muốn apt cài tự động mà không cần xác nhận lại, hãy sử dụng tùy chọn -y trong cú pháp.

Trong ví dụ này chúng ta sẽ cài apache2 mà không cần xác nhận lại bằng tùy chọn -y:

# sudo apt install apache2 -y

Chúng ta có thể thực hiện cài đặt nhiều gói cùng một lúc với apt. Bạn có thể cài đặt nhiều gói cùng một lúc bằng cách cung cấp tất cả các tên, gói cú pháp như bên dưới:

# sudo apt install <package1> <package2> <package3>

Bạn có thể cài đặt một gói mà không muốn nâng cấp. Trong trường hợp đó, bạn có thể sử dụng tùy chọn --no-upgrade theo cú pháp sau:

# sudo apt install <package_name> --no-upgrade

Bạn có thể cài đặt một gói thì theo mặc định phiên bản mới nhất có sẵn trong kho sẽ được cài đặt. Nếu chúng ta không muốn cài đặt phiên bản mới nhất, chúng ta có thể chỉ định số phiên bản.

# sudo apt install <package_name>=<version_number>

Kiểm tra các phụ thuộc của một gói

Để có thể hiển thị tất cả thông tin về các phụ thuộc của một gói cụ thể chúng ta thực hiện như sau:

# sudo apt depends apache2

root@tubt:~# sudo apt depends apache2
apache2
PreDepends: dpkg (>= 1.17.14)
Depends: apache2-bin (= 2.4.41-4ubuntu3.12)
Depends: apache2-data (= 2.4.41-4ubuntu3.12)
Depends: apache2-utils (= 2.4.41-4ubuntu3.12)
Depends: lsb-base
Depends: mime-support
Depends: <perl:any>
perl
Depends: procps
Conflicts: <apache2.2-bin>
Conflicts: <apache2.2-common>
Breaks: libapache2-mod-proxy-uwsgi (<< 2.4.33)
Recommends: ssl-cert
Suggests: apache2-doc
|Suggests: apache2-suexec-pristine
Suggests: apache2-suexec-custom
Suggests: <www-browser>
dillo
edbrowse
hv3
lynx
netsurf
netsurf-fb
netsurf-gtk
chromium-browser
elinks
epiphany-browser
falkon
firefox
konqueror
links
links2
midori
netrik
surf
w3m
wslu
xemacs21-mule
xemacs21-mule-canna-wnn
xemacs21-nomule
Suggests: ufw
Replaces: <apache2.2-bin>
Replaces: <apache2.2-common>
Replaces: libapache2-mod-proxy-uwsgi (<< 2.4.33)

Tìm kiếm một gói:

Chúng ta có thể sử dụng tùy chọn search để có thể tìm kiếm một gói và hiển thị tất cả các gói phù hợp

# sudo apt search apache2

Full Text Search… Done
apache2/focal-updates,focal-security,now 2.4.41-4ubuntu3.12 amd64 [installed]
Apache HTTP Server

apache2-bin/focal-updates,focal-security,now 2.4.41-4ubuntu3.12 amd64 [installed,automatic]
Apache HTTP Server (modules and other binary files)

apache2-data/focal-updates,focal-security,now 2.4.41-4ubuntu3.12 all [installed,automatic]
Apache HTTP Server (common files)

apache2-dev/focal-updates,focal-security 2.4.41-4ubuntu3.12 amd64
Apache HTTP Server (development headers)

apache2-doc/focal-updates,focal-security 2.4.41-4ubuntu3.12 all
Apache HTTP Server (on-site documentation)

apache2-ssl-dev/focal-updates,focal-security 2.4.41-4ubuntu3.12 amd64
Apache HTTP Server (mod_ssl development headers)

apache2-suexec-custom/focal-updates,focal-security 2.4.41-4ubuntu3.12 amd64
Apache HTTP Server configurable suexec program for mod_suexec

Xem thông tin về gói:

Khi chúng ta cần biết thêm về một gói trước khi cài đặt hoặc gỡ bỏ, bạn có thể sử dụng tùy chọn show để có thể hiển thị thông tin về một gói. Tùy chọn này sẽ giúp chúng ta hiển thị thông tin các gói đã cho như phụ thuộc, kích thước …

# sudo apt show apache2

root@tubt:~# sudo apt show apache2
Package: apache2
Version: 2.4.41-4ubuntu3.12
Priority: optional
Section: web
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian Apache Maintainers <debian-apache@lists.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 543 kB
Provides: httpd, httpd-cgi
Pre-Depends: dpkg (>= 1.17.14)
Depends: apache2-bin (= 2.4.41-4ubuntu3.12), apache2-data (= 2.4.41-4ubuntu3.12), apache2-utils (= 2.4.41-4ubuntu3.12), lsb-base, mime-support, perl:any, procps
Recommends: ssl-cert
Suggests: apache2-doc, apache2-suexec-pristine | apache2-suexec-custom, www-browser, ufw
Conflicts: apache2.2-bin, apache2.2-common
Breaks: libapache2-mod-proxy-uwsgi (<< 2.4.33)
Replaces: apache2.2-bin, apache2.2-common, libapache2-mod-proxy-uwsgi (<< 2.4.33)
Homepage: https://httpd.apache.org/
Task: lamp-server
Download-Size: 95.6 kB
APT-Manual-Installed: yes
APT-Sources: http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
Description: Apache HTTP Server
The Apache HTTP Server Project’s goal is to build a secure, efficient and
extensible HTTP server as standards-compliant open source software. The
result has long been the number one web server on the Internet.
.
Installing this package results in a full installation, including the
configuration files, init scripts and support scripts.

N: There is 1 additional record. Please use the ‘-a’ switch to see it

Cập nhật các gói hệ thống (System Packages)

Lệnh apt hoạt động trên dữ liệu của các gói có sẵn. Nếu dữ liệu không được cập nhật, hệ thống sẽ không biết liệu có gói nào mới hơn không. Vì vậy việc cập nhật kho lưu trữ nên là điều quan trọng cần làm trong hệ thống Linux:

root@tubt:~# sudo apt update
Hit:1 http://vn.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://vn.archive.ubuntu.com/ubuntu focal-updates InRelease
Get:3 http://vn.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Hit:4 http://vn.archive.ubuntu.com/ubuntu focal-security InRelease
Fetched 108 kB in 3s (31.9 kB/s)
Reading package lists… Done
Building dependency tree
Reading state information… Done
14 packages can be upgraded. Run ‘apt list –upgradable’ to see them.

Trong đó đầu ra chúng ta thường gặp 3 đầu ra cơ bản:

  • Hit: Không có thay đổi trong phiên bản
  • Ign: Gói đang bị bỏ qua
  • Get: Có một phiên bản mới có sẵn. Nó sẽ tải thông tin về phiên bản. Bạn có thể thấy kích cỡ thông tin tải xuống

Nâng cấp hệ thống

Bạn có thể nâng cấp các gói đã cài đặt sử dụng lệnh dưới đây:

sudo apt upgrade

root@tubt:~# sudo apt update
Hit:1 http://vn.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://vn.archive.ubuntu.com/ubuntu focal-updates InRelease
Get:3 http://vn.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Hit:4 http://vn.archive.ubuntu.com/ubuntu focal-security InRelease
Fetched 108 kB in 3s (31.9 kB/s)
Reading package lists… Done
Building dependency tree
Reading state information… Done
14 packages can be upgraded. Run ‘apt list –upgradable’ to see them.
root@tubt:~# sudo apt upgrade -y
Reading package lists… Done
Building dependency tree
Reading state information… Done
Calculating upgrade… Done
The following NEW packages will be installed:
linux-headers-5.4.0-128 linux-headers-5.4.0-128-generic linux-image-5.4.0-128-generic linux-modules-5.4.0-128-generic
linux-modules-extra-5.4.0-128-generic
The following packages will be upgraded:
libnss-systemd libpam-systemd libsystemd0 libtss2-esys0 libudev1 linux-generic linux-headers-generic linux-image-generic
systemd systemd-sysv systemd-timesyncd ubuntu-advantage-tools udev zlib1g
14 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
3 standard security updates
Need to get 83.3 MB of archives.
After this operation, 380 MB of additional disk space will be used.
Get:1 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libnss-systemd amd64 245.4-4ubuntu3.18 [96.1 kB]
Get:2 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 udev amd64 245.4-4ubuntu3.18 [1,367 kB]
Get:3 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libudev1 amd64 245.4-4ubuntu3.18 [76.3 kB]
Get:4 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 systemd-sysv amd64 245.4-4ubuntu3.18 [10.3 kB]
Get:5 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 systemd-timesyncd amd64 245.4-4ubuntu3.18 [28.1 kB]
Get:6 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libpam-systemd amd64 245.4-4ubuntu3.18 [186 kB]
Get:7 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 systemd amd64 245.4-4ubuntu3.18 [3,808 kB]
Get:8 http://vn.archive.ubuntu.com/ubuntu focal-updates/main amd64 libsystemd0 amd64 245.4-4ubuntu3.18 [269 kB]

Qua lệnh này thì tất cả các gói sẽ được nâng cấp

Ngoài ra chúng ta có thể nâng cấp tất cả các gói hoàn chỉnh bằng cách sử dụng lệnh sau:

sudo apt full-upgrade

Loại bỏ các gói không sử dụng

Khi chúng ta thực hiện cài đặt các gói trên hệ thống, phần phụ thuộc cũng được cài đặt và sử dụng một số thư viện hệ thống với cái gói khác. Sau khi loại bỏ gói cụ thể thì các phụ thuộc của nó vẫn còn tồn tại trên hệ thống, do đó để loại bỏ chúng, chúng ta sử dụng chế độ tự động sau:

# sudo apt autoremove

root@tubt:~# sudo apt autoremove
Reading package lists… Done
Building dependency tree
Reading state information… Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Để có thể xóa tất cả kho lưu trữ cũ của các gói đã tải xuống, chúng ta chạy lệnh sau:

# sudo apt autoclean

root@tubt:~# sudo apt autoclean
Reading package lists… Done
Building dependency tree
Reading state information… Done

Loại bỏ các gói với tập tin cấu hình của nó

Khi chúng ta chạy lệnh apt với tùy chọn remove thì nó xóa các tệp gói nhưng các tệp cấu hình vẫn còn trên hệ thống. Do đó, để xóa cả tệp gói và tệp cấu hình của nó, chúng ta cần sử dụng tùy chọn purge như bên dưới:

# sudo apt purge apache2

root@tubt:~# sudo apt purge apache2 -y
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following packages were automatically installed and are no longer required:
apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.2-0
ssl-cert
Use ‘sudo apt autoremove’ to remove them.
The following packages will be REMOVED:
apache2*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 543 kB disk space will be freed.
(Reading database … 123731 files and directories currently installed.)
Removing apache2 (2.4.41-4ubuntu3.12) …
Processing triggers for man-db (2.9.1-1) …
Processing triggers for ufw (0.36-6ubuntu1) …
(Reading database … 123681 files and directories currently installed.)
Purging configuration files for apache2 (2.4.41-4ubuntu3.12) …
Processing triggers for systemd (245.4-4ubuntu3.18) …
Processing triggers for ufw (0.36-6ubuntu1) …

3. Lệnh apt-cache

apt-cache là một công cụ được sử dụng để tìm kiếm các gói phần mềm, thu thập thông tin của các gói và cũng được sử dụng để tìm kiếm những gói có sẵn và đã sẵn sàng để cài đặt trên hệ thống.

Ngoài ra nó được sử dụng để có được thông tin của các gói phần mềm. Công cụ này sử dụng bộ đệm của apt (Advanced Packaging Tool) để tìm các gói và tên gói

4. Sử dụng apt-cache

Để có thể liệt kê tất cả các gói có sẵn chúng ta chạy lệnh bên dưới:

# sudo apt-cache pkgnames

Tìm kiếm tên gói và mô tả của gói

Để tìm ra tên gói và mô tả của gói trước khi cài đặt chúng ta sử dụng tùy chọn search kết hợp với lệnh apt-cache. Nó sẽ hiển thị các gói phù hợp với mô tả phù hợp

# sudo apt-cache search python3

Tìm và liệt kê các gói bắt đầu bằng apache

# sudo apt-cache pkgnames apache

Kiểm tra thông tin của gói

# sudo apt-cache show mysql

root@tubt:~# sudo apt-cache show mysql-server
Package: mysql-server
Architecture: all
Version: 8.0.30-0ubuntu0.20.04.2
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 111
Depends: mysql-server-8.0
Filename: pool/main/m/mysql-8.0/mysql-server_8.0.30-0ubuntu0.20.04.2_all.deb
Size: 9540
MD5sum: 0dd6c33e4912035c93eadbc0e212be6a
SHA1: 57ba218495c6ca25287343d5df50ab34e0e7e405
SHA256: 8862e65bbb85a9c67e3c8c90ab43691349341332dfbf710bbf3bacb6963cd86b
SHA512: cc1da9712c667884bf584b8394f3f98bc511d8178562c8671d457b5ad90f90cd2de9e441fa4a9a27b93583e6f9699e79c053796c87dec34a0504892e4ba4eb80
Homepage: http://dev.mysql.com/
Description-en: MySQL database server (metapackage depending on the latest version)
This is an empty package that depends on the current “best” version of
mysql-server (currently mysql-server-8.0), as determined by the MySQL
maintainers. Install this package if in doubt about which MySQL
version you need. That will install the version recommended by the
package maintainers.
.
MySQL is a fast, stable and true multi-user, multi-threaded SQL database
server. SQL (Structured Query Language) is the most popular database query
language in the world. The main goals of MySQL are speed, robustness and
ease of use.

Để kiểm tra số liệu thông kê của bộ nhớ đệm chúng ta sử dụng tùy chọn stats sẽ hiển thị số liệu thống kê tổng thể về bộ đệm

# sudo apt-cache stats

root@tubt:~# sudo apt-cache stats
Total package names: 114597 (3,209 k)
Total package structures: 104581 (4,602 k)
Normal packages: 75383
Pure virtual packages: 1655
Single virtual packages: 15990
Mixed virtual packages: 421
Missing: 11132
Total distinct versions: 84682 (7,452 k)
Total distinct descriptions: 160594 (3,854 k)
Total dependencies: 510561/122708 (12.2 M)
Total ver/file relations: 39454 (947 k)
Total Desc/File relations: 29522 (709 k)
Total Provides mappings: 43346 (1,040 k)
Total globbed strings: 220258 (5,433 k)
Total slack space: 77.0 k
Total space accounted for: 39.9 M
Total buckets in PkgHashTable: 50503
Unused: 6380
Used: 44123
Utilization: 87.3671%
Average entries: 2.37022
Longest: 59
Shortest: 1
Total buckets in GrpHashTable: 50503
Unused: 5166
Used: 45337
Utilization: 89.7709%
Average entries: 2.52767
Longest: 11
Shortest: 1

5. Giới thiệu về apt-get

apt-get là một tiện ích quản lý gói mãnh mẽ được sử dụng để làm việc với thư viện Ubuntu APT (Advanced Packaging Tool) thực hiện cài đặt, tìm kiếm gói, cập nhật, loại bỏ các gói phần mềm hiện có, nâng cấp các gói phần mềm hiện tại và sử dụng để nâng cấp toàn bộ hệ điều hành

apt-get yêu cầu các đặc quyền quản trị superuser, vì nó liên quan đến các khía cạnh cốt

6. Sử dụng apt-get

Trước khi bắt đầu với apt-get chúng ta cần cập nhật. Nếu không cập nhật thì hệ thống sẽ không biết các gói mới hơn có sẵn hay không để thực hiện cập nhật. Để cập nhật ta chạy lệnh bên dưới:

# sudo apt-get update

root@tubt:~# sudo apt-get update
Hit:1 http://vn.archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://vn.archive.ubuntu.com/ubuntu focal-updates InRelease
Hit:3 http://vn.archive.ubuntu.com/ubuntu focal-backports InRelease
Hit:4 http://vn.archive.ubuntu.com/ubuntu focal-security InRelease
Reading package lists… Done

Trong đó đầu ra chúng ta thường sẽ bắt gặp 3 thông tin cơ bản:

  • Hit: Không có thay đổi trong phiên bản
  • Ign: Gói đang bị bỏ qua.
  • Get: Có một phiên bản mới có sẵn, nó sẽ tải thông tin phiên bản. Bạn có thể thấy kích thước của phiên bản tải xuống

Nâng cấp các gói đã được cài trên máy

# sudo apt-get upgrade

Nâng cấp một chương trình cụ thể thay vì toàn bộ hệ thống, hãy chạy lệnh bên dưới:

# sudo apt-get upgrade [package_name]

Cài đặt gói

# sudo apt-get install apache2

Cài đặt nhiều gói cùng một lúc

# sudo apt-get install php mysql-server firefox

Cài đặt gói mà không cần nâng cấp

# sudo apt-get install firefox --no-upgrade

Chỉ nâng cấp các gói đã cài mà không cài đặt thêm gói mới

# sudo apt-get install firefox --only-upgrade

Khi chúng ta muốn cài đặt một phiên bản cụ thể của các gói thì chúng ta sử dụng tùy chọn = để nối tên gói với phiên bản mong muốn

# sudo apt-get install vsftpd=3.0.3-12

Gỡ cài đặt ra khỏi hệ thống mà không xóa các tệp cấu hình ta sử dụng lệnh sau:

# sudo apt-get remove apache2

Xóa bỏ các gói và đồng thời xóa các tệp cấu hình được liên kết:

# sudo apt-get purge apache2

Ngoài ra bạn có thể kết hợp cả hai lệnh với nhau như bên dưới

# sudo apt-get remove --purge apache2

Để có thể dọn sạch không gian đĩa chúng ta sử dụng option clean:

# sudo apt-get clean

Kiểm tra nhật ký thay đổi của gói ta cần sử dụng tùy chọn changelog:

# sudo apt-get changelog apache2

7. Lời kết

Qua bài trên giúp chúng ta hiểu hơn về các lệnh apt, apt-get, apt-cache trên Ubuntu. Từ các ví dụ trên giúp cho chúng ta có thể cập nhật hệ thống, cài đặt các gói … Từ đây chúng ta có thể thao tác với các gói trên hệ thống một cách dễ dàng hơn

Chúc các bạn thành công và hẹn gặp lại các bạn trong các bài viết tiếp theo tại wiki.nhanhoa.com

Trả lời