LoginSignup
15
15

More than 1 year has passed since last update.

Ubuntuの高速化~リポジトリ参照先の変更

Last updated at Posted at 2020-05-30

はじめに

先日 wsl2 上に導入した Ubuntu 20.04 で遊ぶ前にパッケージインストールを高速化すべく、aptにてミラーサイトを参照するように設定したのでメモ。

環境

  • Ubuntu 20.04 ( on Windows10 2004 wsl2)

注意(2022.8.15更新)

この後の手順で/etc/apt/sources.list を編集すると、将来Ubuntu 20.04 LTS から22.04 LTSへのアップグレードしたくなった場合に支障が出る。そのため事前に/etc/apt/sources.list のバックアップを取得し、いつでも元に戻せるようにしておくことを推奨。特に困っていなければ編集しないのも選択肢のうち。

支障の内容は以下の記事の後半を参照。

やったこと

/etc/apt/sources.list の編集。インストール直後の状態では海外のサーバにパッケージを取りに行くらしいので国内のサーバを参照するようにする。

インストール直後の状態

コメント行が非常に多いのでsedで有効な行のみ表示。

/etc/apt/sources.list(インストール直後)
% cat /etc/apt/sources.list | sed -e "/^#/d" -e "/^$/d"
deb http://archive.ubuntu.com/ubuntu/ focal main restricted
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ focal universe
deb http://archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://archive.ubuntu.com/ubuntu/ focal multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted
deb http://security.ubuntu.com/ubuntu/ focal-security universe
deb http://security.ubuntu.com/ubuntu/ focal-security multiverse

ミラーサイトを参照するよう変更

これもsedにて実施。元のファイルは/etc/apt/sources.list.bakとして残る。

ミラーサイト利用するよう変更
sudo sed -i.bak -r 's!deb \S+!deb mirror://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list
/etc/apt/sources.list(変更後)
% cat /etc/apt/sources.list | sed -e "/^#/d" -e "/^$/d"
deb mirror://mirrors.ubuntu.com/mirrors.txt focal main restricted
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates main restricted
deb mirror://mirrors.ubuntu.com/mirrors.txt focal universe
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates universe
deb mirror://mirrors.ubuntu.com/mirrors.txt focal multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-backports main restricted universe multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-security main restricted
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-security universe
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-security multiverse

以下の2つのURLが mirror://mirrors.ubuntu.com/mirrors.txt に置換される。

1対1じゃないのがちょっと気持ち悪いが、他でも同じことやっているようなのでよしとする。

apt update / apt upgrade 実行

sudo apt update でパッケージリストの更新。エラーがなければ完了。
sudo apt upgrade でパッケージの最新化。

パッケージリスト更新(※初回ではない)
% sudo apt update
Get:1 http://mirrors.ubuntu.com/mirrors.txt Mirrorlist [332 B]
Hit:3 http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu focal-updates InRelease
Hit:2 http://ftp.riken.jp/Linux/ubuntu focal InRelease
Hit:4 http://ftp.jaist.ac.jp/pub/Linux/ubuntu focal-backports InRelease
Hit:5 http://ubuntutym.u-toyama.ac.jp/ubuntu focal-security InRelease
Fetched 332 B in 2s (145 B/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.

mirrors.txtの内容

http://mirrors.ubuntu.com/mirrors.txtに記載されたURLからパッケージを取得する模様。

mirrors.txt
http://ftp.riken.jp/Linux/ubuntu/
https://linux.yz.yamagata-u.ac.jp/ubuntu/
http://ubuntu-ashisuto.ubuntulinux.jp/ubuntu/
http://ubuntutym.u-toyama.ac.jp/ubuntu/
http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu/
http://ftp.jaist.ac.jp/pub/Linux/ubuntu/
http://www.ftp.ne.jp/Linux/packages/ubuntu/archive/
http://archive.ubuntu.com/ubuntu/

7つの国内サイト + インストール直後に記載されていたURLが記載されている。
海外からこのURLにアクセスするとリストの内容が変わるのかな?

おまけ

1対1でない変換が気持ち悪かったので、Securityの行を変更しない正規表現も作った。
インストール直後の状態からの変更用。

securityの行は保持
sudo sed -i.bak -r 's!deb http://archive\S+!deb mirror://mirrors.ubuntu.com/mirrors.txt!' /etc/apt/sources.list
実行結果
% cat /etc/apt/sources.list | sed -e "/^#/d" -e "/^$/d"
deb mirror://mirrors.ubuntu.com/mirrors.txt focal main restricted
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates main restricted
deb mirror://mirrors.ubuntu.com/mirrors.txt focal universe
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates universe
deb mirror://mirrors.ubuntu.com/mirrors.txt focal multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-updates multiverse
deb mirror://mirrors.ubuntu.com/mirrors.txt focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu/ focal-security main restricted
deb http://security.ubuntu.com/ubuntu/ focal-security universe
deb http://security.ubuntu.com/ubuntu/ focal-security multiverse

もう一つ。sedでミスって.bakファイルから戻したいときに使用したコマンド。

.bakから元に戻す
sudo mv -f /etc/apt/sources.list{.bak,}

おわりに

sed 地味に便利。

【補足】

  • 手で打ち込むときはaptスクリプト内に記載するときはapt-get -qと使い分けている
  • 実のところsudo apt updateの初回実行時に以下のようなエラーが出たが、再現しないので放置中
パッケージリスト更新時のエラー
E: The repository 'http://security.ubuntu.com/mirrors.txt focal-security Release' does not have a Release file.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.
15
15
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
15
15