LoginSignup
1286
1347

More than 5 years have passed since last update.

[Ubuntu] apt-get まとめ

Last updated at Posted at 2015-01-28

summary

コマンド 内容
apt-get install [package] パッケージのインストール/更新
apt-get update パッケージリストの更新
apt-get upgrade インストールされてるパッケージの更新
apt-get dist-upgrade インストールされてるカーネルの更新(Ubuntu)/ディストリビューションの更新(Debian)
dpkg -l [package] インストールされてるパッケージの一覧
dpkg -L インストールした時のファイルの一覧
apt-cache search [query] パッケージの検索
apt-cache policy [query] パッケージの検索 (インストール可能なバージョンの表示)
apt-cache madison [query] パッケージの検索 (インストール可能なバージョンの一覧)
apt-get remove [package] パッケージの削除
apt-get autoremove 使ってないパッケージの削除
apt-get purge [package] パッケージの削除(設定ファイルも)
apt-get clean アーカイブファイルの削除
apt-get autoclean 使ってないパッケージのアーカイブファイルの削除

# apt-get install [package]

パッケージのインストール用。
パッケージ指定して更新したい時にも使えます。バージョンを指定するには =バージョン をライブラリ名の後ろに付与。

使用例)

$ sudo apt-get install ntp ntpdate
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Use 'apt-get autoremove' to remove them.
Suggested packages:
  ntp-doc
The following packages will be upgraded:
  ntp
1 upgraded, 0 newly installed, 0 to remove and 93 not upgraded.
Need to get 613 kB of archives.
After this operation, 0 B of additional disk space will be used.
Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates/main ntp amd64 1:4.2.6.p3+dfsg-1ubuntu3.2 [613 kB]
Fetched 613 kB in 2s (242 kB/s)                                        
(Reading database ... 93849 files and directories currently installed.)
Preparing to replace ntp 1:4.2.6.p3+dfsg-1ubuntu3.1 (using .../ntp_1%3a4.2.6.p3+dfsg-1ubuntu3.2_amd64.deb) ...
 * Stopping NTP server ntpd
   ...done.
Unpacking replacement ntp ...
Processing triggers for ureadahead ...
Processing triggers for man-db ...
Setting up ntp (1:4.2.6.p3+dfsg-1ubuntu3.2) ...
 * Starting NTP server ntpd
   ...done.
... (以下略)...

# apt-get update

パッケージを管理しているDBを更新して、最新の状態にする。パッケージの更新自体はしません。
参照する場所は /etc/apt/sources.list に記述。
こんな感じ↓

...
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise main
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise main

## Major bug fix updates produced after the final release of the
## distribution.
deb http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates main
deb-src http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ precise-updates main
...

使用例)

$ sudo apt-get update
Get:1 http://security.ubuntu.com precise-security Release.gpg [198 B]
Get:2 http://security.ubuntu.com precise-security Release [53.0 kB]
Hit http://ap-northeast-1.ec2.archive.ubuntu.com precise Release.gpg
Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com precise-updates Release.gpg [198 B]
Get:4 http://security.ubuntu.com precise-security/main Sources [124 kB]
Fetched 4,884 kB in 7s (622 kB/s)
Reading package lists... Done # DB更新して終了

# apt-get upgrade

インストールされているパッケージを全更新。
追加でパッケージのインストールが必要になった場合は更新処理を止めてくれる。

使用例)

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

# apt-get dist-upgrade

# apt-get upgrade と似てるが、違いとしては、追加で新規のパッケージが必要になればインストールしたり不要になったパッケージは削除してくれる。とにかく最新にしてくれるコマンド。
ディストリビューションのバージョン更新もするので、ディストリビューターからのセキュリティパッチを充てる時とかによく使う。

使用例)

$ sudo apt-get dist-upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  linux-headers-3.2.0-75 linux-headers-3.2.0-75-virtual ...
The following packages will be upgraded:
  apport apt-transport-https apt-utils bind9-host ...
73 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 106 MB of archives.
After this operation, 104 MB of additional disk space will be used.
Do you want to continue [Y/n]? # Y で更新開始

# dpkg -l [package]

apt-get ではないけど、インストール済のパッケージを表示する時に使う。
[package] を省略すると全パッケージを表示。
yum だと

# yum list installed

とかできるけど apt-get だとできないので不便。

使用例)

$ sudo dpkg -l
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                             Version                             Description
+++-================================-===================================-==============================================================================
ii  accountsservice                  0.6.15-2ubuntu9.7                   query and manipulate user account information
ii  acpid                            1:2.0.10-1ubuntu3                   Advanced Configuration and Power Interface event daemon
ii  adduser                          3.113ubuntu2                        add and remove users and groups
...(以下略)...

# dpkg -L [package]

【追記】似てるけどちょっと違います。L です。
パッケージインストール時の、「ファイルの一覧」を表示します。

使用例)

$ dpkg -L nginx
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/nginx
/usr/share/doc/nginx/README.Debian
/usr/share/doc/nginx/copyright
/usr/share/doc/nginx/changelog.Debian.gz
/usr/share/doc/nginx/CHANGES.gz

# apt-cache search [query]

【追記】パッケージの検索用。
パッケージの名前と説明がクエリの検索対象となるようです。

使用例)

$ sudo apt-cache search snoopy 
libphp-snoopy - Snoopy is a PHP class that simulates a web browser
snoopy - execve() wrapper and logger

# apt-cache policy [query] / apt-cache madison [query]

【追記】 Ansible や Chef 使って使用パッケージのバージョン管理していると、特定のパッケージが使用できなくなることがたまにあります。
インストール可能なパッケージのバージョンを知りたい時に使えるコマンド。

使用例)

$ sudo apt-cache policy nginx
nginx:
  Installed: (none)
  Candidate: 1.4.6-1ubuntu3.5
  Version table:
     1.4.6-1ubuntu3.5 0
        500 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
        500 http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
     1.4.6-1ubuntu3 0
        500 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

$ sudo apt-cache madison nginx
     nginx | 1.4.6-1ubuntu3.5 | http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
     nginx | 1.4.6-1ubuntu3.5 | http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
     nginx | 1.4.6-1ubuntu3 | http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages
     ...

# apt-get remove [package]

指定したパッケージの削除。

# apt-get autoremove

使ってないパッケージを見つけて削除。

使用例)

$ sudo apt-get autoremove
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  mecab-utils
0 upgraded, 0 newly installed, 1 to remove and 53 not upgraded.
After this operation, 79.3 MB disk space will be freed.
Do you want to continue [Y/n]? Y 
...(以下略)...

# apt-get purge [package]

【追記】指定したパッケージの削除。 remove だと設定ファイルが残るが、 purge はそちらも削除。

# apt-get clean

パッケージをインストールするために拾ってきたアーカイブファイル( *.tar.gz とか)を削除

# apt-get autoclean

使ってないパッケージのアーカイブファイルを削除

使用例)

$ sudo apt-get autoclean
Reading package lists... Done
Building dependency tree       
Reading state information... Done
1286
1347
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1286
1347