Linux 安装 Homebrew

有 sudo 权限

  1. 安装依赖:

    sudo apt install git curl build-essential
    
  2. 下载并安装 Homebrew:

    bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    
  3. 将下面的配置写入 ~/.bashrc

    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
    

无 sudo 权限

使用学校的超算的时候,没有 sudo 权限,但是又需要安装一些软件。之前一直通过手动编译源码的方式安装,但是当软件的依赖很多的时候,这种方法就变得费时费力。后来终于不胜其烦,决定回到 Homebrew 的怀抱。

Prerequsities

  1. 在安装 Homebrew 之前需要确保系统上装有较新的 gitcurl。可以通过 conda 安装:

    conda create -y -n build -c conda-forge git curl gcc gxx make
    conda activate build
    
  2. 将下面的命令写入 ~/.bashrc

    export HOMEBREW_GIT_PATH="$CONDA_PREFIX/bin/git"
    export HOMEBREW_CURL_PATH="$CONDA_PREFIX/bin/curl"
    

安装 Homebrew

  1. 下载:

    mkdir -p $HOME/.local/opt/homebrew
    curl -L https://github.com/Homebrew/brew/tarball/master | tar -xz --strip-components 1 -C $HOME/.local/opt/homebrew
    
  2. 安装:

    eval "$($HOME/.local/opt/homebrew/bin/brew shellenv)"
    brew update --force --quiet
    brew install -s glibc  # 最重要的依赖,且必须从源码构建
    chmod -R go-w "$(brew --prefix)/share/zsh"
    
  3. 将下面的命令加入 ~/.bashrc

    export HOMEBREW_RELOCATE_BUILD_PREFIX=1
    eval "$($HOME/.local/opt/homebrew/bin/brew shellenv)"
    
  • 关于 HOMEBREW_RELOCATE_BUILD_PREFIX 环境变量的说明:Homebrew 预构建的 bottle 使用 /home/linuxbrew/.linuxbrew 作为构建前缀。由于一些软件包在编译时会将构建前缀硬编码到其二进制文件中,因此如果修改这些程序的安装路径,会出现路径不匹配的问题。所以默认情况下,如果 Homebrew 没有安装在默认路径,那么安装包时 Homebrew 会选择重新从源码构建,但是构建失败的概率很大。设置 HOMEBREW_RELOCATE_BUILD_PREFIX 环境变量将允许 Homebrew 在安装软件包时尝试重定位这些硬编码路径,以确保软件包可以在不同的前缀下正常工作。目前这个功能对于除了 glibc 以外的包都有效。

参考:

关于 Homebrew 换源请参考博客:Homebrew 使用

杂项

Debug 建议

brew install some-package -vd  # verbose && debug

如果编译时出现无法连接 GCC 相关库的问题,使用下面的命令编译:

brew install --cc=gcc some-package
HOMEBREW_RELOCATE_BUILD_PREFIX=1 brew install some-package  # 杀手锏,启用路径重定位

一些环境变量:

export HOMEBREW_NO_ENV_FILTERING=1  # 取消环境隔离
export HOMEBREW_MAKE_JOBS=1         # 单线程编译

使用 City-Fan 源安装新版 curl

sudo vim /etc/yum.repos.d/city-fan.org.repo
[city-fan.org]
name=city-fan.org repository for Red Hat Enterprise Linux (and clones) $releasever ($basearch)
#baseurl=http://mirror.city-fan.org/ftp/contrib/yum-repo/rhel$releasever/$basearch
mirrorlist=http://mirror.city-fan.org/ftp/contrib/yum-repo/mirrorlist-rhel$releasever
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-city-fan.org

[city-fan.org-debuginfo]
name=city-fan.org debuginfo repository for Red Hat Enterprise Linux (and clones) $releasever ($basearch)
#baseurl=http://www.city-fan.org/ftp/contrib-debug/rhel$releasever/$basearch
mirrorlist=http://www.city-fan.org/ftp/contrib-debug/mirrorlist-rhel$releasever
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-city-fan.org

[city-fan.org-source]
name=city-fan.org source repository for Red Hat Enterprise Linux (and clones) $releasever
#baseurl=http://mirror.city-fan.org/ftp/contrib/yum-repo/rhel$releasever/source
mirrorlist=http://mirror.city-fan.org/ftp/contrib/yum-repo/source-mirrorlist-rhel$releasever
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-city-fan.org
sudo yum update curl

参考:centos7 升级 curl-8.2.1 支持 http2(yum update)| 博客园

使用 Wandisco 源安装 git

sudo rpm -U http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm
sudo yum install -y git

参考:Install a newer version of Git on CentOS 7 | Server Fault

部分疑难杂包

有一部分包总是编译失败,但是它们却是很多包的依赖,因此必须安装。下面记录了部分包的安装过程。

util-linux

  1. 下载 util-linux.rb 文件:

    wget https://raw.githubusercontent.com/Homebrew/homebrew-core/refs/heads/master/Formula/u/util-linux.rb
    vim util-linux.rb
    
  2. 编辑 util-linux.rb 文件:

    -    system "make", "install"
    +    system "make", "install", "LDFLAGS=-lm"
    
  3. 安装 util-linux.rb

    brew install util-linux.rb
    

openssl@3

安装 openssl@3 82 号测试失败:

==> Installing curl dependency: openssl@3
==> perl Makefile.PL INSTALL_BASE=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1
==> make PERL5LIB=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1/lib/perl5 CC=gcc-14
==> make install
==> perl Makefile.PL INSTALL_BASE=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1
==> make PERL5LIB=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1/lib/perl5 CC=gcc-14
==> make install
==> perl Makefile.PL INSTALL_BASE=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1
==> make PERL5LIB=/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1/lib/perl5 CC=gcc-14
==> make install
==> perl ./Configure --prefix=/data/.local/opt/homebrew/Cellar/openssl@3/3.4.1 --openssldir=/data/.local/opt/homebrew/etc/openssl@3 --libdir=lib no-ssl3 no-ssl3-method no-zlib linux-x8
==> make
==> make install MANDIR=/data/.local/opt/homebrew/Cellar/openssl@3/3.4.1/share/man MANSUFFIX=ssl
==> make HARNESS_JOBS=32 test TESTS=-test_afalg
Last 15 lines from /home/centos/.cache/Homebrew/Logs/openssl@3/13.make:
sh: line 1: 11858 Killed                  ../../../../util/wrap.pl ../../../../apps/openssl cmp -config server.cnf 2> ../../../../test-runs/test_cmp_http/Mock_server_STDERR.txt80-test_cmp_http.t ...................... ok
15-test_genec.t ......................... ok

Test Summary Report
-------------------
82-test_ocsp_cert_chain.t             (Wstat: 256 (exited 1) Tests: 3 Failed: 1)
  Failed test:  3
  Non-zero exit status: 1
Files=324, Tests=3818, 71 wallclock secs (19.62 usr  1.59 sys + 410.80 cusr 245.59 csys = 677.60 CPU)
Result: FAIL
make[2]: *** [run_tests] Error 1
make[2]: Leaving directory `/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1'
make[1]: *** [_tests] Error 2
make[1]: Leaving directory `/tmp/opensslA3-20250408-23482-i3crah/openssl-3.4.1'
make: *** [tests] Error 2

READ THIS: https://docs.brew.sh/Troubleshooting

These open issues may also help:
brew install openssl@3 fails on amazon linux 2 https://github.com/Homebrew/homebrew-core/issues/212050

问题原因:设置了代理环境变量 http_proxy https_proxy,并且测试程序尝试通过代理访问 localhost 等地址。

解决方法:设置 no_proxy 环境变量。

export no_proxy='127.0.0.1,[::1],localhost'

参考:openssl@3 fails to build (80-test_cmp_http.t) on macOS Mojave 10.14.3 | GitHub

gettext

安装 gettext 时提示:Makefile: missing separator

==> Fetching gettext
==> Downloading https://ftp.gnu.org/gnu/gettext/gettext-0.24.tar.gz
Already downloaded: /home/centos/.cache/Homebrew/downloads/cc66312b61b54ac9604b91e9b9d570e5732d63227496c4806823ab07a6523bb1--gettext-0.24.tar.gz
==> ./configure --with-libunistring-prefix=/data/.local/opt/homebrew/opt/libunistring --disable-silent-rules --with-included-glib --with-included-libcroco --with-emacs --with-lispdir=/
==> cp -r /tmp/gettext* /home/centos/temp
==> make
Last 15 lines from /home/centos/.cache/Homebrew/Logs/gettext/03.make:
libtool: link: gcc-14 -DINSTALLDIR=\"/data/.local/opt/homebrew/Cellar/gettext/0.24/bin\" -g -O2 -o ngettext ngettext-ngettext.o  ../gnulib-lib/libgrt.a
libtool: link: gcc-14 -DINSTALLDIR=\"/data/.local/opt/homebrew/Cellar/gettext/0.24/bin\" -g -O2 -o gettext gettext-gettext.o  ../gnulib-lib/libgrt.a
libtool: link: gcc-14 -DINSTALLDIR=\"/data/.local/opt/homebrew/Cellar/gettext/0.24/bin\" -g -O2 -o envsubst envsubst-envsubst.o  ../gnulib-lib/libgrt.a
make[4]: Leaving directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24/gettext-runtime/src'
Making all in po
make[4]: Entering directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24/gettext-runtime/po'
Makefile:80: *** missing separator.  Stop.
make[4]: Leaving directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24/gettext-runtime/po'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24/gettext-runtime'
make[2]: *** [all] Error 2
make[2]: Leaving directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24/gettext-runtime'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/tmp/gettext-20250411-14685-adh1zu/gettext-0.24'
make: *** [all] Error 2

错误原因:make 版本太旧导致的语法不兼容。

解决方法:用 conda 安装 make,然后修改安装文件:

wget https://raw.githubusercontent.com/Homebrew/homebrew-core/refs/heads/master/Formula/g/gettext.rb

进行如下替换:

-    system "make"
+    system "/conda/installed/make"
     ENV.deparallelize # install doesn't support multiple make jobs
-    system "make", "install"
+    system "/conda/installed/make", "install"

然后使用文件安装:

brew install --build-from-source --formula gettext.rb

glib

安装 glib 时报错:undefined reference to `sinh@GLIBC_2.2.5'

/opt/homebrew/opt/binutils/bin/ld: /opt/homebrew/opt/sqlite/lib/libsqlite3.so: undefined reference to `sinh@GLIBC_2.2.5'

错误原因:编译时没有链接数学库。

解决方法:

HOMEBREW_RELOCATE_BUILD_PREFIX=1 brew install glib
posted @ 2024-11-07 02:08  Undefined443  阅读(702)  评论(0)    收藏  举报