March 26, 2022

HomebrewをCentOS 7にインストール

macOSで便利に使っているHomebrewはLinuxにもインストールできるらしい。

AlmaLinux 8へはHomebrewトップページのスクリプトを貼り付けるだけでインストールできたけど、CentOS 7だと少し手間が必要だったから手順を残しておく。

用意したもの

vagrant up して yum update した CentOS 7.9

[vagrant@localhost ~]$ sudo yum update
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
No packages marked for update
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

Homebrewインストール

手順まとめ

  1. Gitインストール
    OS標準のGitは1.8だけど、2.7.0以降が必要。
    IUS RepoのGit 2.24をインストールする。
    $ sudo yum install https://repo.ius.io/ius-release-el7.rpm
    $ sudo yum install git224
    
  2. cURLインストール
    OS標準のcURLは7.29だけど、7.41以降が必要。
    cURL公式サイトのリンクからLinux汎用バイナリをダウンロードして使う。
    $ sudo curl https://github.com/moparisthebest/static-curl/releases/download/v7.82.0/curl-amd64 -Lo /usr/local/bin/curl
    $ sudo chmod +x /usr/local/bin/curl
    $ sudo ln -s /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/ssl/certs/ca-certificates.crt
    
  3. Homebrewトップページのインストールスクリプト実行
    $ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  4. パスを通す
    $ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ${HOME}/.bash_profile
    $ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
    
  5. Homebrewに指定したcURLを使わせる
    echo -e 'export HOMEBREW_DEVELOPER=1\nexport HOMEBREW_CURL_PATH=/usr/local/bin/curl' >> ${HOME}/.bash_profile
    $ exec $SHELL -l
    

試行錯誤の経緯

Gitが古い

そのままHomebrewトップページのスクリプトを実行するとGitをインストールしろと言われる。

[vagrant@localhost ~]$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
You must install Git before installing Homebrew. See:
  https://docs.brew.sh/Installation

sudo yum install gitでインストールしてもCentOS 7標準のGitは1.8系で、2.7.0以降をインストールしろと言われる。

[vagrant@localhost ~]$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
The version of Git that was found does not satisfy requirements for Homebrew.
Please install Git 2.7.0 or newer and add it to your PATH.

CentOS 7標準リポジトリのGit 1.8は入れずに、代わりにCentOS公式Wikiに載っているリポジトリ一覧にあるIUS RepoのGit 2.24を入れる。

[vagrant@localhost ~]$ sudo yum install https://repo.ius.io/ius-release-el7.rpm
[vagrant@localhost ~]$ sudo yum install git224

cURLが古い

Git 2.24をインストールしても、次はcURLが古いと言われる。

[vagrant@localhost ~]$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
The version of cURL that was found does not satisfy requirements for Homebrew.
Please install cURL 7.41.0 or newer and add it to your PATH.
[vagrant@localhost ~]$ rpm -q curl
curl-7.29.0-59.el7_9.1.x86_64

ググるとCity-FanにcURLパッケージがあるらしいけど、CentOS公式WikiではKnown Problem Repositories(既知の問題のあるリポジトリ)とされているから使いたくない。

この記事を書く前はSnapパッケージのcURLを使ったけど、書きつつ調べつつしてたら、cURL公式サイトにLinux用の汎用バイナリを用意してくれてあった。
ファイル1個ダウンロードするだけでいいからこっちがオススメ。

[vagrant@localhost ~]$ sudo curl https://github.com/moparisthebest/static-curl/releases/download/v7.82.0/curl-amd64 -Lo /usr/local/bin/curl
[vagrant@localhost ~]$ sudo chmod +x /usr/local/bin/curl
[vagrant@localhost ~]$ curl --version
curl 7.82.0 (x86_64-pc-linux-musl) libcurl/7.82.0 OpenSSL/1.1.1l zlib/1.2.11 libssh2/1.10.0 nghttp2/1.46.0
Release-Date: 2022-03-05
Protocols: dict file ftp ftps gopher gophers http https imap imaps mqtt pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS HSTS HTTP2 HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSockets

そのまま実行すると証明書検証用のファイルが無いと言われるので、

[vagrant@localhost ~]$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
curl: (77) error setting certificate verify locations:  CAfile: /etc/ssl/certs/ca-certificates.crt CApath: none

既存のファイルからシンボリックリンクを張っておく。

[vagrant@localhost ~]$ sudo ln -s /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem /etc/ssl/certs/ca-certificates.crt

インストールできた

これでようやくインストールが動くようになった。

[vagrant@localhost ~]$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
==> Found cURL: /usr/local/bin/curl
==> Setting HOMEBREW_DEVELOPER to use Git/cURL not in /usr/bin
==> Checking for `sudo` access (which may request your password)...
==> This script will install:
/home/linuxbrew/.linuxbrew/bin/brew
/home/linuxbrew/.linuxbrew/share/doc/homebrew
/home/linuxbrew/.linuxbrew/share/man/man1/brew.1
/home/linuxbrew/.linuxbrew/share/zsh/site-functions/_brew
/home/linuxbrew/.linuxbrew/etc/bash_completion.d/brew
/home/linuxbrew/.linuxbrew/Homebrew
==> The following new directories will be created:
/home/linuxbrew/.linuxbrew/bin
/home/linuxbrew/.linuxbrew/etc
/home/linuxbrew/.linuxbrew/include
/home/linuxbrew/.linuxbrew/lib
/home/linuxbrew/.linuxbrew/sbin
/home/linuxbrew/.linuxbrew/share
/home/linuxbrew/.linuxbrew/var
/home/linuxbrew/.linuxbrew/opt
/home/linuxbrew/.linuxbrew/share/zsh
/home/linuxbrew/.linuxbrew/share/zsh/site-functions
/home/linuxbrew/.linuxbrew/var/homebrew
/home/linuxbrew/.linuxbrew/var/homebrew/linked
/home/linuxbrew/.linuxbrew/Cellar
/home/linuxbrew/.linuxbrew/Caskroom
/home/linuxbrew/.linuxbrew/Frameworks

Press RETURN/ENTER to continue or any other key to abort:

インストールの次のステップ

インストールが完了すると次のステップが示される。

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/vagrant/.bash_profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
    sudo yum groupinstall 'Development Tools'
  For more information, see:
    https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
    brew install gcc
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh

書かれているとおりHomebrewへのパスが通っていないので実行できない。

[vagrant@localhost ~]$ brew
-bash: brew: コマンドが見つかりません

パスを通す。

[vagrant@localhost ~]$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ${HOME}/.bash_profile
[vagrant@localhost ~]$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

cURLが古い再び

確認、再びcURLが古いと言われるものの、動くことは動く。

[vagrant@localhost ~]$ brew doctor
Error: Please update your system curl.
Minimum required version: 7.41.0
Your curl version: 7.29.0
Your curl executable:
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: No developer tools installed.
Install Clang or run `brew install gcc`.

/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/brew.sh を見るに、指定したcURLをHomebrewに使わせるには、環境変数HOMEBREW_DEVELOPERに1をセットしてHOMEBREW_CURL_PATHで指定すればいい。

ここ↓

setup_curl() {
  # This is set by the user environment.
  # shellcheck disable=SC2154
  HOMEBREW_BREWED_CURL_PATH="${HOMEBREW_PREFIX}/opt/curl/bin/curl"
  if [[ -n "${HOMEBREW_FORCE_BREWED_CURL}" && -x "${HOMEBREW_BREWED_CURL_PATH}" ]] &&
     "${HOMEBREW_BREWED_CURL_PATH}" --version &>/dev/null
  then
    HOMEBREW_CURL="${HOMEBREW_BREWED_CURL_PATH}"
  elif [[ -n "${HOMEBREW_DEVELOPER}" && -x "${HOMEBREW_CURL_PATH}" ]]
  then
    HOMEBREW_CURL="${HOMEBREW_CURL_PATH}"
  else
    HOMEBREW_CURL="curl"
  fi
}

こんな感じ

[vagrant@localhost ~]$ HOMEBREW_DEVELOPER=1 HOMEBREW_CURL_PATH=/usr/local/bin/curl brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: No developer tools installed.
Install Clang or run `brew install gcc`.

毎度打たなくてもいいように.bash_profileに追記しておく

[vagrant@localhost ~]$ echo -e 'export HOMEBREW_DEVELOPER=1\nexport HOMEBREW_CURL_PATH=/usr/local/bin/curl' >> ${HOME}/.bash_profile
[vagrant@localhost ~]$ exec $SHELL -l

© 2020 nissy-lab.com