はじめに

こんにちは!今回はaptコマンドについて解説します。

aptはDebianやUbuntuなどのLinuxディストリビューションで、ソフトウェアパッケージを管理するコマンドです。アプリケーションのインストール、アップデート、削除など、日々必ず使うコマンドですね。

パッケージ管理が簡単になると、システム管理がずっと楽になります。

apt コマンドとは

aptは、Advanced Packaging Tool の略で、Debian系Linuxのパッケージ管理の中心となる外部コマンドです。

apt-getapt-cacheなど、複数のコマンドの機能を統合した使いやすいインターフェースを提供します。ソフトウェアのインストール、アップデート、依存関係の管理を自動的に行ってくれるんです。

基本構文

1
apt [オプション] サブコマンド [パッケージ名]

主なサブコマンドはinstallupdateupgraderemoveなどです。

主なオプション

オプション 説明
update パッケージリストを更新
upgrade インストール済みパッケージをアップグレード
install 指定パッケージをインストール
remove 指定パッケージを削除
autoremove 不要な依存パッケージを削除
search パッケージを検索
show パッケージ情報を表示
-y, --yes 質問に対して常に「yes」と答える
-s, --simulate 実行内容をシミュレーション

使用例

例1: パッケージリストの更新

1
sudo apt update

実行結果:

1
2
3
4
Reading package lists... Done
Building dependency tree... Done
Get:1 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]

パッケージリストを最新に更新します。新しいパッケージをインストールする前に必ず実行しましょう。

例2: インストール済みパッケージのアップグレード

1
sudo apt upgrade

実行結果:

1
2
3
4
5
6
Reading package lists... Done
Building dependency tree... Done
The following packages will be upgraded:
  git python3 vim
3 upgraded, 0 newly installed, 0 to remove
Do you want to continue? [Y/n] Y

システムのすべてのパッケージを最新版にアップデートします。

例3: 特定のパッケージをインストール

1
sudo apt install git

実行結果:

1
2
3
4
5
6
7
Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  git
0 upgraded, 1 newly installed, 0 to remove
Setting up git (1:2.25.1-1ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...

gitをインストールします。依存パッケージがあれば自動的にインストールされます。

例4: 複数のパッケージを一度にインストール

1
sudo apt install git curl wget

実行結果:

1
2
3
4
Reading package lists... Done
The following NEW packages will be installed:
  curl git wget
0 upgraded, 3 newly installed, 0 to remove

複数のパッケージをまとめてインストールできます。スペース区切りで指定します。

例5: パッケージを削除

1
sudo apt remove git

実行結果:

1
2
3
4
5
6
Reading package lists... Done
Building dependency tree... Done
The following packages will be REMOVED:
  git
0 upgraded, 0 newly installed, 1 to remove
Do you want to continue? [Y/n] Y

gitを削除します。設定ファイルは残ります。

例6: パッケージを検索

1
apt search text-editor

実行結果:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Sorting... Done
Full Text Search... Done
gedit/focal 3.36.3-1ubuntu1 amd64
  GNOME text editor

nano/focal 4.9.3-1ubuntu1 amd64
  small, friendly text editor

vim/focal 2:8.1.2269-1ubuntu5.16 amd64
  Vi IMproved - enhanced vi editor

「text-editor」に関連するパッケージを検索します。

例7: パッケージ情報を表示

1
apt show git

実行結果:

1
2
3
4
5
6
7
8
Package: git
Version: 1:2.25.1-1ubuntu3.11
Priority: optional
Section: vcs
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian Git Maintainers <[email protected]>
Installed-Size: 23.4 MB
Depends: git-man (= 1:2.25.1-1ubuntu3.11), perl-modules-5.30, liberror-perl, ...

gitの詳細情報を表示します。バージョンや依存関係などが確認できます。

例8: yes オプション付きで自動確認

1
sudo apt install -y curl

実行結果:

1
2
Reading package lists... Done
Setting up curl (7.68.0-1ubuntu2.20) ...

質問に「yes」と答える必要がなく、自動的にインストールされます。スクリプトに便利ですね。

Tips・注意点

sudo が必要

apt updateapt installapt removeなどの操作にはroot権限が必要です。sudoをつけて実行しましょう。

1
sudo apt install vim

キャッシュのクリア

長期間使用していると、ダウンロード済みのパッケージキャッシュが溜まります。ディスク容量を節約したい場合:

1
2
sudo apt clean      # すべてのキャッシュを削除
sudo apt autoclean  # 古いキャッシュのみ削除

apt vs apt-get

aptapt-getの改良版です。新しい方のaptの使用がお勧めです。

実践的な使い方

システム全体のアップデート

1
2
sudo apt update
sudo apt upgrade

システムを最新の状態に保つ基本的なコマンドです。定期的に実行しましょう。

開発環境のセットアップ

1
2
sudo apt update
sudo apt install build-essential git curl

C/C++開発に必要な基本ツールをインストールします。

古いパッケージの自動削除

1
sudo apt autoremove

不要になった依存パッケージを自動的に削除し、ディスク容量を節約できます。

まとめ

aptコマンドのポイント:

  • Debian系Linuxのパッケージ管理の中心コマンド
  • update: パッケージリストを更新
  • upgrade: パッケージをアップグレード
  • install: パッケージをインストール
  • remove: パッケージを削除
  • search: パッケージを検索
  • よく使う組み合わせ: sudo apt update && sudo apt upgrade

パッケージ管理をマスターすると、Linuxの操作がぐんと快適になります。毎日のようにお世話になるコマンドですよ。