macOSでは、ほぼ定番になっているHomebrewですが、最近ではLinuxにもHomebrewを導入できます。
DebianやRedHatでは、aptやdnf(yum)でパッケージ管理が出来るのですが、それらのパッケージにはなくリポジトリ化されてないプログラムの導入・更新が楽になります。
例えば、私がよく利用するHugoやDockerのdocker-compose,lazydockerなどは、更新がある度に、公式のサイトからダウンロードして入れ替えたりする必要があるのですが、これがmacOS同様に、brew update hugoみたいに簡素化する事が出来るようになるのです。
また、Linuxのパッケージ管理よりも、更新が早いので、新しいバージョンのプログラムを利用できる事が多いです。
(特にRedhat系のパッケージはバージョンが古いものが多いので、Homebrewを導入するメリットは大きいかと)
Homebrew導入
導入する前に、各Linuxにて必要なプログラムを導入
Debian/Ubuntu
$ sudo apt-get install build-essential procps curl file gitRedhat/CentOS
$ sudo yum groupinstall 'Development Tools'
$ sudo yum install procps-ng curl file git
$ sudo yum install libxcrypt-compatCentOS7では、下記記事のように、Gitとcurlのバージョンを上げる必要があります
Upgrade git & curl(CentOS7)
- gitのバージョンを上げる
$ sudo yum install -y https://repo.ius.io/ius-release-el7.rpm
$ sudo yum update
$ sudo yum install git236- curlのバージョンを上げる
$ sudo curl https://github.com/moparisthebest/static-curl/releases/download/v7.85.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.crthomebrewインストール
$ curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
$ bash install.sh
…
- Run these three commands in your terminal to add Homebrew to your PATH:
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /root/.profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
sudo apt-get install build-essential
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公式通りにinstall.shを実行すると、/home/linuxbrew/.linuxbrew/内にライブラリや実行プログラムが入ります。
環境設定
rootで作業したため、/root/.profileとなってますが、ユーザーで利用する場合は、~/.profileへ変更してください。
echo '# Set PATH, MANPATH, etc., for Homebrew.' >> /root/.profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"確認
$ brew doctor
Your system is ready to brew.パッケージインストール
あとは、macOSのbrew同様にインストール
$ brew install hugo tree lazydockerシステム内のaptやyumでインストールしたパッケージよりは、brewでインストールしたパッケージが比較的新しいので、新しいバージョンのプログラムを実行したい場合は、brewの方が良さそう。
また、Linuxのシステムを汚さずに利用できるのは良いですね。
以上で導入は完了です。 下記は参考まで。
個別インストール
/home/linuxbrewのディレクトリではなく、自分の環境下に置きたい場合、install.shを使わず個別にインストールする方法です。
この個別インストールをしばらく使ってみたのですが、いくつかのプログラムのインストールが出来なかったので、公式インストールした方が賢明です
$ git clone https://github.com/Homebrew/brew ~/.linuxbrew/Homebrew
$ mkdir ~/.linuxbrew/bin
$ ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin
$ eval $(~/.linuxbrew/bin/brew shellenv)- 環境設定
この記事ではrootで作業したので、
/root/.profileのファイルは自分の環境に合わせましょう
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /root/.profile
## 自分のホームディレクトリ配下へインストールした場合
$ echo 'eval "$(~/.linuxbrew/bin/brew shellenv)"' >> ~/.profile
$ exec $HSELL -l
$ brew -v
Homebrew 3.6.5
Homebrew/homebrew-core (git revision 534720b84e9; last commit 2022-10-14)実際は、自分のユーザー名で、zshrcを利用しております
自分のディレクトリ配下へインストールした場合、brew doctorでプリフェックスが違うので、いくつかのフォーミュラーでビルドが失敗する恐れがある警告が出ます
Warning: Your Homebrew's prefix is not /home/linuxbrew/.linuxbrew.
Some of Homebrew's bottles (binary packages) can only be used with the default
prefix (/home/linuxbrew/.linuxbrew).
You will encounter build failures with some formulae.警告通り、いくつかのプログラム(curlやrsyncなど)のビルド・インストールが出来ませんでした。)
トラブル
インストール・環境設定をしたものの、brew自体が利用できない場合は、パスが通っていないかパスの優先度が間違っている場合がほとんどなので、再度確認しましょう。
また、brewでインストールしたプログラムが動作しない場合は、シェルの再読み込みで、ほぼ解決出来るかと思います。
小ネタ
- brew自体の更新
git cloneしてるので、そのままgit pullするだけ - 削除
homebrew自体を削除したい場合は、rm -rfにて/home/linuxbrewや$HOME/.linuxbreをごっそり消せば、全て削除されます