Ubuntu 패키지 관리 툴: apt 사용법

우분투에서 패키지를 관리하는 툴로 apt-get과 apt-cache를 주로 사용해 왔습니다. 요즘에는 두 명령을 결합한 명령이 apt를 주로 사용합니다. apt는 apt-get/apt-cache를 완전히 통합하는 툴은 아닌 것 같습니다. 사용자 편리성을 위해서 apt-get과 apt-cache의 주요 기능을 결합한 툴입니다. man에서는 apt를 다음과 같이 설명합니다.

apt provides a high-level commandline 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.

사용자가 일반 리눅스 사용자라면 apt를 사용하는 것이 효과적이고, 패키지 관리의 세밀한 옵션을 주로 사용하는 스크립트 작업에서는 apt-get을 사용하는 것이 좋습니다.

이제부터 기본적인 apt 명령 사용법을 정리하겠습니다.

apt 명령과 apt-get/apt-cache 명령 비교

다음은 apt 명령과 기존의 apt-get/apt-cache를 비교한 테이블입니다.

apt 명령 기존 명령 설명
apt install apt-get install 패키지 목록
apt remove apt-get remove 패키지 삭제
apt purge apt-get purge 패키지와 관련 설정 제거
apt update apt-get update 레파지토리 인덱스 갱신
apt upgrade apt-get upgrade 업그레이드 가능한 모든 패키지 업그레이드
apt autoremove apt-get autoremove 불필요한 패키지 제거
apt full-upgrade apt-get dist-upgrade 의존성 고려한 패키지 업그레이드
apt search apt-cache search 프로그램 검색
apt show apt-cache show 패키지 상세 정보 출력

새로운 명령은 다음과 같습니다.

apt 명령 설명
apt list apt-get install
apt edit-sources 소스 리스트 편집

주요 apt 명령 사용법

권한 문제가 발생할 경우 sudo 명령을 붙여 root로 실행할 수 있습니다.

1. 패키지 설치, 삭제

1. 패키지 설치

apt install <package_name>
  • 패키지 명을 여러 게 지정하여 여러 패키지를 한꺼번에 설치할 수 있습니다. 이때 패지키 명은 공백으로 구분합니다.
$ sudo apt install docker.io
# 로그 생략
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu xenial/main i386 bridge-utils i386 1.5-9ubuntu1 [29.4 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial/universe i386 cgroupfs-mount all 1.2 [4,970 B]
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 runc i386 1.0.0~rc2+docker1.13.1-0ubuntu1~16.04.1 [1,390 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 containerd i386 0.2.5-0ubuntu1~16.04.1 [2,750 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 docker.io i386 1.13.1-0ubuntu1~16.04.2 [11.1 MB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/main i386 ubuntu-fan all 0.12.8~16.04.2 [35.6 kB]
Fetched 15.3 MB in 11s (1,295 kB/s)
# 로그 생략
Setting up bridge-utils (1.5-9ubuntu1) ...
Setting up cgroupfs-mount (1.2) ...
Setting up runc (1.0.0~rc2+docker1.13.1-0ubuntu1~16.04.1) ...
Setting up containerd (0.2.5-0ubuntu1~16.04.1) ...
Setting up docker.io (1.13.1-0ubuntu1~16.04.2) ...
Adding group `docker' (GID 117) ...
Done.
Setting up ubuntu-fan (0.12.8~16.04.2) ...
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
vagrant@vagrant:~$

2. 패키지 제거

apt remove <package_name>

아래는 docker.io 패키지 제거 실행 예제입니다.

$ sudo apt remove docker.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  bridge-utils cgroupfs-mount containerd runc ubuntu-fan
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  docker.io
0 upgraded, 0 newly installed, 1 to remove and 51 not upgraded.
After this operation, 50.4 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 38476 files and directories currently installed.)
Removing docker.io (1.13.1-0ubuntu1~16.04.2) ...
'/usr/share/docker.io/contrib/nuke-graph-directory.sh' -> '/var/lib/docker/nuke-graph-directory.sh'
Warning: Stopping docker.service, but it can still be activated by:
  docker.socket
Processing triggers for man-db (2.7.5-1) ...
$

remove 명령은 저장한 패키지만을 제거합니다. 해당 패키지 설정 파일은 삭제되지 않습니다. 아래와 같이 관련 파일은 유지됩니다.

$ls -al /etc/docker/
total 12
drwxr-xr-x  2 root root 4096 May 26 08:15 .
drwxr-xr-x 97 root root 4096 May 26 08:15 ..
-rw-------  1 root root  244 May 26 08:15 key.json
$

3. 패키지와 설정 파일 제거

purge 명령은 패키지와 관련 파일을 모두 제거합니다.

apt purge <package_name>

아래는 wget 패키지 제거 실행 예제입니다.

$ sudo apt purge wget
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  bridge-utils cgroupfs-mount containerd python3-requests python3-urllib3 runc ubuntu-fan
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  ssh-import-id* wget*
0 upgraded, 0 newly installed, 2 to remove and 50 not upgraded.
After this operation, 990 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 38380 files and directories currently installed.)
Removing ssh-import-id (5.5-0ubuntu1) ...
Purging configuration files for ssh-import-id (5.5-0ubuntu1) ...
Removing wget (1.17.1-1ubuntu1.3) ...
Purging configuration files for wget (1.17.1-1ubuntu1.3) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
$

위 실행 로그에서 Purging configuration files for ssh-import-id (5.5-0ubuntu1) ...와 같이 설정 파일을 삭제한 것을 확인할 수 있습니다.


4. 사용하지 않는 패키지 제거

autoremove 명령을 사용하여 현재 사용되지 않는 패키지를 제거합니다.

apt autoremove

아래는 autoremove의 실행 예제입니다.

$ sudo apt autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 51 not upgraded.
$

실행 로그에서 업그레이드, 신규 설치 및 제거, 업그레이드 대상 패키지 등의 정보를 출력하고 있습니다. 위 실행 예제는 미사용 패키지가 없는 상태를 보입니다.


2. 패키지 정보 조회

2.1. 패키지 목록 검색

패키지지 검색 명령은 다음과 같습니다.

$ apt search <key word>

아래는 docker 문자열의 검색 결과입니다.

$ apt search docker
Sorting... Done
Full Text Search... Done
docker/trusty-updates 1.5-1 amd64
  System tray for KDE3/GNOME2 docklet applications

docker.io/trusty-updates 1.6.2~dfsg1-1ubuntu4~14.04.1 amd64
  Linux container runtime

golang-docker-dev/trusty-updates 1.6.2~dfsg1-1ubuntu4~14.04.1 all
  Externally reusable Go packages included with Docker

karbon/trusty 1:2.8.1-1-0ubuntu3 amd64
  vector graphics application for the Calligra Suite

kdocker/trusty 4.6-2 amd64
  lets you dock any application into the system tray

pidgin/trusty-updates,trusty-security 1:2.10.9-0ubuntu3.4 amd64
  graphical multi-protocol instant messaging client for X

vim-syntax-docker/trusty-updates 1.6.2~dfsg1-1ubuntu4~14.04.1 all
  Docker container engine - Vim highlighting syntax files
$

2.2. 패키지 상세 정보 검색

패키지의 주요 정보를 출력합니다.

  • 버전
  • 분류
  • 의존성 패키지
  • 다운로드 위치
  • 사이트
$ apt show <package_name>

아래는 docker.io 패키지 정보 조회 결과 입니다.

$ apt show docker.io
Package: docker.io
Version: 1.13.1-0ubuntu1~16.04.2
Built-Using: glibc (= 2.23-0ubuntu9)
Priority: optional
Section: universe/admin
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Paul Tagliamonte <paultag@debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 50.4 MB
Depends: adduser, containerd (>= 0.2.5~), iptables, runc (>= 1.0.0~rc2-0ubuntu1~), init-system-helpers (>= 1.18~), lsb-base (>= 4.1+Debian11ubuntu7), libapparmor1 (>= 2.6~devel), libc6 (>= 2.4), libdevmapper1.02.1 (>= 2:1.02.97), libseccomp2 (>= 2.1.0)
Recommends: ca-certificates, cgroupfs-mount | cgroup-lite, git, ubuntu-fan, xz-utils, apparmor
Suggests: aufs-tools, btrfs-tools, debootstrap, docker-doc, rinse, zfs-fuse | zfsutils
Breaks: docker (<< 1.5~)
Replaces: docker (<< 1.5~)
Homepage: https://dockerproject.org
Download-Size: 11.1 MB
APT-Manual-Installed: yes
APT-Sources: http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages
Description: Linux container runtime
 Docker complements kernel namespacing with a high-level API which operates at
 the process level. It runs unix processes with strong guarantees of isolation
 and repeatability across servers.
 .
 Docker is a great building block for automating distributed systems:
 large-scale web deployments, database clusters, continuous deployment systems,
 private PaaS, service-oriented architectures, etc.
 .
 This package contains the daemon and client. Using docker.io on non-amd64 hosts
 is not supported at this time. Please be careful when using it on anything
 besides amd64.
 .
 Also, note that kernel version 3.8 or above is required for proper operation of
 the daemon process, and that any lower versions may have subtle and/or glaring
 issues.

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

2.2. 패키지 목록 조회(상태별)

apt list는 레파지토리에 등록된 패키지 목록을 조회합니다.

$ apt list

다음은 apt list의 실행 결과입니다.

$ apt list
## 로그 생략
zynaddsubfx-dbg/xenial 2.5.2-2ubuntu1 i386
zynaddsubfx-dssi/xenial 2.5.2-2ubuntu1 i386
zyne/xenial 0.1.2-2 all
zynjacku/xenial 6-4build1 i386
zypper/xenial-updates 1.12.4-1build0.1 i386
zypper-common/xenial-updates 1.12.4-1build0.1 all
zypper-doc/xenial-updates 1.12.4-1build0.1 all
zziplib-bin/xenial-updates,xenial-security 0.13.62-3ubuntu0.16.04.1 i386

apt list는 옵션을 지정하여 상태별 패키지 목록을 조회할 수 있습니다.

옵션 설명
–installed 설치된 패키지 목록 조회
–upgradable 설치된 패키지 중 업그레이드 대상 패키지 목록 조회
–all-versions 패키지의 모든 버전 목록 조회

패키지 목록에 옵션을 지정하여 패키지 조회 목록의 수는 다음과 같습니다.

$ apt list |wc -l

55384
$ apt list --installed |wc -l

481
$ apt list --upgradable |wc -l

51
$ apt list --all-versions |wc -l

118156
$

패키지 조회 목록 결과는 다음과 같습니다.

  • 레파지토리에 등록된 패키지 수: 55,384개
  • 설치된 패키지 수: 481개
  • 업그레이드 대상 패키지 수: 51개
  • 레파지토리의 등록된 패키지의 모든 버전 수: 118,156개

3. 패키지 업데이트

3.1. 시스템 패키지 업데이트

/var/lib/apt/lists에 패키지 목록을 업데이트 합니다. 레파지토리 목록은 /etc/apt/sources.list 파일에서 관리합니다.

$ apt update

다음은 update 명령의 실행 결과입니다.

$ sudo apt update
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
Hit:2 http://archive.ubuntu.com/ubuntu xenial InRelease
Get:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
Get:5 http://archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages [718 kB]
Get:6 http://archive.ubuntu.com/ubuntu xenial-updates/universe i386 Packages [577 kB]
Fetched 1,618 kB in 3s (410 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
50 packages can be upgraded. Run 'apt list --upgradable' to see them.
$

다음은 현재 업그레이드 대상 패키지를 조회하는 apt list --upgradable의 실행 결과입니다.

$ apt list --upgradable
Listing... Done
apport/xenial-updates 2.20.1-0ubuntu2.17 all [upgradable from: 2.20.1-0ubuntu2.15]
cloud-guest-utils/xenial-updates 0.27-0ubuntu25.1 all [upgradable from: 0.27-0ubuntu25]
curl/xenial-updates,xenial-security 7.47.0-1ubuntu2.8 i386 [upgradable from: 7.47.0-1ubuntu2.7]
## 로그 생략
ubuntu-release-upgrader-core/xenial-updates,xenial-security 1:16.04.25 all [upgradable from: 1:16.04.24]
update-manager-core/xenial-updates 1:16.04.13 all [upgradable from: 1:16.04.12]
vlan/xenial-updates 1.9-3.2ubuntu1.16.04.5 i386 [upgradable from: 1.9-3.2ubuntu1.16.04.4]
xdg-user-dirs/xenial-updates 0.15-2ubuntu6.16.04.1 i386 [upgradable from: 0.15-2ubuntu6]
vagrant@vagrant:~$

3.2. 시스템 업그레이드

전체 패키지를 최신 버전으로 업데이트합니다.

$ apt upgrade

다음은 apt upgrade의 실행로그입니다.

$ sudo apt upgrade
#로그생략
Installing new version of config file /etc/vmware-tools/suspend-vm-default ...
Installing new version of config file /etc/vmware-tools/vm-support ...
Setting up vlan (1.9-3.2ubuntu1.16.04.5) ...
Installing new version of config file /etc/network/if-pre-up.d/vlan ...
Setting up python3-distupgrade (1:16.04.25) ...
Setting up python3-update-manager (1:16.04.13) ...
Setting up ubuntu-release-upgrader-core (1:16.04.25) ...
Setting up update-manager-core (1:16.04.13) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
Processing triggers for initramfs-tools (0.122ubuntu8.11) ...
update-initramfs: Generating /boot/initrd.img-4.4.0-87-generic
W: mdadm: /etc/mdadm/mdadm.conf defines no arrays.
Processing triggers for systemd (229-4ubuntu21.2) ...
Processing triggers for ureadahead (0.100.0-19) ...
vagrant@vagrant:~$

3.3. 시스템 업그레이드

전체 시스템을 새 버전으로 업그레이드합니다. 시스템 업그레이드가 필요할 경우 기존 패키지를 삭제하기도 합니다.

$ apt full-upgrade

sudo apt-get dist-upgrade와 같은 기능을 제공합니다. 일반적인 업데이트 방법은 아닙니다.


apt/apt-get/aptitude 명령 비교

apt, apt-get 및 aptitude의 명령 맵핑은 다음과 같습니다.

목록 apt apt-get aptitude 비고
패키지 설치 apt install {package_name} apt-get install {package_name} aptitude install {package_name}
패키지 삭제 apt remove {package_name} apt-get remove {package_name} aptitude remove {package_name} 패키지 파일 제거
패키지 정보 확인 apt show {package_name} aptitude show {package_name}
패키지 검색 apt search {package_name} aptitude search {package_name}
전체 패키지 목록 조회 apt list
설치된 패키지 목록 조회 apt list –installed dpkg -l
패키지 삭제2 apt purge {package_name} apt-get purge {package_name} aptitude purge {package_name} 패키지+설정파일 제거
패키지 다운로드 apt-get download {package_name} aptitude download {package_name} 패키지 .deb파일

참고자료