LoginSignup
0
0

homebrewを使わずstowやautoconfからimagemagick@6、openssl、curlまでソースビルド

Last updated at Posted at 2020-07-30

大量のインストールログがある為、変更と追記の予定あり。

目的 ruby gemであるkindai.rbを動かす

ruby gemであるkindai.rbを動かす為、
homebrewを使わずstowやautoconfから始めimagemagick@6、openssl、curlまでをソースからビルドしたい。他にはgemのrmagick、kindai.rbなど。

homebrewをやめたい理由はrubyのインストールとcurl、openssl、cert.pem周辺に不具合や再インストール、brew doctorなどメンテナンス多いからである。ネットを検索するとhomebrewにおけるcurlのconfigureオプションは煩雑に変わっていて、定まってない感じがある。

パッケージ管理ソフトであるstowを使いクリーンな環境を手に入れる。

手順書を書くと複雑なのでメモ書きを残す。

stowによる管理を行う。

OSアップグレード後は下記が必要。
1.
sudo chown -R $(whoami):admin /usr/local
2.Command Line Tools のインストール
xcode-select --install

stowによる管理を行う。
Stow - GNU Project

GNU Stow で 環境を汚さずに tarballのmake installでインストール・アンインストールする - Qiita

(*1)GNU Stowによるローカルパッケージ管理 – Kenchant https://senooken.jp/post/2018/03/28/

各パッケージインストール後に「stow -vv ディレクトリ名」を忘れずに。
とても簡単で便利。

インストールするパッケージ一覧

順序はいい加減で保証なし。

準備
stow-231
基本
autoconf
automake
gettext
xz-5.2.5
pkg-config-0.29.2
git-2.19.5
pcre2-10.35
zlib

ImageMagick-6911-25の前

freetype-2.10.2
jpeg-v9d
libpng-1.6.37
tiff-4.1.0
libtool-2.4.6
lcms2.11 (カラーマネジメントシステム)
cmake-3.18.1
ImageMagick-6911-25

curlの前に
openssl-111g

curl7711

rubyの前に
readline800

ruby-258

基本パッケージのダウンロードURL

Index of /gnu/autoconf https://ftp.gnu.org/gnu/autoconf/?C=M;O=D

Index of /gnu/automake https://ftp.gnu.org/gnu/automake/?C=M;O=D

Index of /gnu/gettext https://ftp.gnu.org/gnu/gettext/?C=M;O=D

XZ Utils https://git.tukaani.org/
(tukaani.org/xz/ 削除した)

Index of /releases https://pkgconfig.freedesktop.org/releases/?C=M;O=D

GitHub - git/git: Git Source Code https://github.com/git/git

PCRE - http://www.pcre.org/

zlib Home Site https://zlib.net/

ImageMagick@6に必要なパッケージのダウンロードURL。情報元はbrew info。

Build: pkg-config
Required: freetype, jpeg, libpng, libtiff, libtool, little-cms2, openjpeg, webp, xz
他にghostscript?

pkg-config https://freedesktop.org/wiki/Software/pkg-config/

XZ Utils https://git.tukaani.org/
https://tukaani.org/xz/は削除した

The FreeType Project https://www.freetype.org/

Independent JPEG Group https://www.ijg.org/

libpng Home Page http://www.libpng.org/pub/png/libpng.html

LibTIFF https://libtiff.gitlab.io/libtiff/

Libtool - GNU Project https://www.gnu.org/software/libtool/

Little Color Management System https://www.littlecms.com/

Download | CMake https://cmake.org/download/

https://www.openjpeg.org/
Build: cmake ✘, doxygen ✘
Required: libpng ✔, libtiff ✘, little-cms2

(webpはインストールしなかった。)
https://developers.google.com/speed/webp/
==> Dependencies
Required: jpeg ✘, libpng ✔, libtiff ✘

stow用の各./configureオプション

make、make install、「stow -vv ディレクトリ名」は共通。
順序など正確かどうかは不明。動作保証しない。

追記。初稿時にstowインストールを行っていなかった autoconfとautomake も2022年5月中頃に行いstowリンクした。
誤記。/usr/local/stow/gettext021に修正した。

エラーが起きないように呪文。根拠は(*1)
alias stow="stow --ignore='dir|gschemas.compiled|icon-theme.cache'"

./configure --prefix=/usr/local/stow/autoconf271
./configure --prefix=/usr/local/stow/automake1165
./configure --prefix=/usr/local/stow/gettext021

./configure --prefix=/usr/local/stow/xz-5.2.5

./configure --prefix=/usr/local/stow/pkg-config-0.29.2 --with-internal-glib
./configure --prefix=/usr/local/stow/git-2.19.5
./configure --prefix=/usr/local/stow/pcre2-10.35

./configure --prefix=/usr/local/stow/freetype-2.10.2
./configure --prefix=/usr/local/stow/jpeg-v9d
./configure --prefix=/usr/local/stow/libpng-1.6.37
./configure --prefix=/usr/local/stow/tiff-4.1.0
./configure --prefix=/usr/local/stow/libtool-2.4.6
./configure --prefix=/usr/local/stow/lcms2.11 --disable-shared
./configure --prefix=/usr/local/stow/cmake-3.18.1
./configure --prefix=/usr/local/stow/openjpeg-2.3.1

###ImageMagick@6インストール

./configure --prefix=/usr/local/stow/ImageMagick-6911-25
make
make install
--prefixを除けば普通だった。もっと難しいかと想像したが拍子抜けした。

opensslインストール

openssl https://www.openssl.org/source/

sharedを忘れないように。
./Configure darwin64-x86_64-cc --prefix=/usr/local/stow/openssl shared no-ssl3 no-ssl3-method
make
make install
terminalを開き直すか/usr/local/bin/c_rehashをすること

■opensslインストールConfigureオプション
CentOS7.x で、httpsの「TLSv1.3」を。openssl 1.1.1、php7.4.x、nginx 1.20 | サーバーレシピ
https://server-recipe.com/3543/

■openssl動作確認1
opensslコマンドの使い方 - hana_shinのLinux技術ブログ
https://hana-shin.hatenablog.com/entry/2022/01/29/184741

■openssl動作確認2
WebサーバーなどのSSL証明書 検証方法 – インフォサーカス・インコーポレイテッド – Info Circus, Inc.
https://www.infocircus.jp/2019/10/23/openssl_cert_verify/

参考
DSAS開発者の部屋:システム管理者の心得? ~ OpenSSLコマンドで証明書をチェック
http://dsas.blog.klab.org/archives/50477013.html

DSAS開発者の部屋:システム管理者の心得? ~ OpenSSLコマンドで証明書をチェック(2)
http://dsas.blog.klab.org/archives/50482460.html

readlineインストール

./configure --prefix=/usr/local/stow/readline-8.0

rubyインストール

「--with-opt-dir」と「--enable-shared」であること。
./configure --prefix=/usr/local/stow/ruby-258 --with-opt-dir=/usr/local/stow/openssl --with-readline-dir=/usr/local/stow/readline800 --enable-shared
make
make install

ドキュメントなし。通常より早くインストールが終わる。
$ CONFIGURE_OPTS="--disable-install-rdoc"

/usr/local/stow/ruby-258/bin
インストール後このパスを通すこと。

ruby切り替えもstowのリンク機能と、$PATHのパス設定変更で行う。

###rubyからのopenssl動作確認1

irbでopenssl動作確認

$ irb
> require 'openssl'
=> true

エラーが発生したらextconf.rb。
extディレクトリはrubyのソースを解凍したフォルダ直下。
$ cd ext/openssl
$ ruby extconf.rb --with-opt-dir=/usr/local/stow/openssl --enable-shared
これでもう一度irbでopensslがrequireして出来ればOK。

参考1
正しい CONFIGURE_OPTS
いつもいつも忘れてしまうのでruby-buildのconfigureオプションの設定についてメモっておく - Qiita
GitHub - rbenv/ruby-build: Compile and install Ruby

下は問題を起こす?私も問題を生じた。(RUBY_CONFIGURE_OPTS)
rbenv installがopensslで失敗する - Qiita

rubyとopensslをインストール後に、SSLの設定と詳細確認を行う。

2020年08月15日追記

コンパイル時に指定した --openssldir の値は
openssl version -a
コマンドで表示できる。
OpenSSL がデフォルトで参照する証明書ファイルはこのディレクトリの cert.pem である。

参考
man SSL_CTX_load_verify_locations
https://www.openssl.org/docs/man1.1.1/man3/SSL_CTX_load_verify_locations.html

(例)
export SSL_CERT_FILE=/usr/local/stow/openssl/ssl/cacert.pem

OpenSSL が実際にデフォルトで使う証明書ファイルは SSL_CERT_FILE 環境変数で変更出来る。

$ ruby -r openssl -e 'p OpenSSL::X509::DEFAULT_CERT_FILE'
"/usr/local/stow/openssl/ssl/cert.pem"

この OpenSSL::X509::DEFAULT_CERT_FILE は SSL_CERT_FILE 環境変数がセットされていなかった場合に読み込むべきファイルを指している。DEFAULT_CERT_FILE は環境変数をセットしても不変。

curlインストール

curl公式 https://curl.se/

curlのconfigureオプションはhomebrewでも頻繁に変わっているようだ。この為、試行錯誤を数多く繰り返した。

参考 macOSのconfigureオプション
macOS Sierra 10.12.4 デフォルトのcurl-config結果。
curl-config --configure
--disable-static --enable-hidden-symbols --enable-threaded-resolver --with-gssapi --with-darwinssl --without-libssh2

初稿時にはhomebrewのconfigureオプションを元にした形だったが
2022年5月にmacOSのconfigureオプションに近くした。

configureオプションはもっと短く出来るかも?
./configure --enable-libcurl-option --without-libssh2 --disable-static --enable-hidden-symbols --enable-threaded-resolver --with-gssapi --with-ssl=/usr/local/stow/openssl --prefix=/usr/local/stow/curl-7831
make
make install

(注2)2022年5月22日追記
10.11のappleデフォルトの curl-config --configure 結果を入手し記事の下にペーストした。apple公式には --with-secure-transport オプションが付いている。必須かどうか不明。
--with-secure-transport はつけていたが、enable Apple OS native SSL/TLS とあるので、2022年に外した。

$ curl --version
curl 7.71.1 (x86_64-apple-darwin13.4.0) libcurl/7.71.1 SecureTransport (OpenSSL/1.1.1g) zlib/1.2.5
Release-Date: 2020-07-01
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS GSS-API IPv6 Kerberos Largefile libz MultiSSL NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets

$ curl --version
curl 7.83.1 (x86_64-apple-darwin15.6.0) libcurl/7.83.1 OpenSSL/1.1.1o zlib/1.2.12
Release-Date: 2022-05-11
Protocols: dict file ftp ftps gopher gophers http https imap imaps ldap ldaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS GSS-API HSTS HTTPS-proxy IPv6 Kerberos Largefile libz NTLM NTLM_WB SPNEGO SSL TLS-SRP UnixSockets

■curlの動作確認1
curlオプション覚え書き - karakaram-blog https://www.karakaram.com/notes-on-curl-options/

rubyインストール後はgem install rmagick とgem install kindai

kindai.rb https://〜実行後、色々なSSLエラーを繰り返す。

SSL_connect returned=1 errno=0 state=error: certificate verify failed (unable to get local issuer certificate) (OpenSSL::SSL::SSLError)

原因1
curlのconfigureオプションの最適化が不明で試行錯誤した。
原因2
調べると以下の情報が見つかった。cert.pemが未設定だった。
$ ruby -ropenssl -e "p OpenSSL::X509::DEFAULT_CERT_FILE"
"/usr/local/stow/openssl/ssl/cert.pem"

kindai.rb https://〜でSSL通信に失敗、SSLの対策

cert.pemがディレクトリになかった為、以下実行。
cd /usr/local/stow/openssl/ssl/
curl -LO https://curl.haxx.se/ca/cacert.pem
mv cacert.pem cert.pem

irbでopensslとpemを調べる場合は下記参照。

rb(main):003:0> require 'openssl'
=> true
irb(main):004:0> p OpenSSL::X509::DEFAULT_CERT_FILE
"/usr/local/stow/openssl/ssl/cert.pem"
=> "/usr/local/stow/openssl/ssl/cert.pem"

stowは/usr/local/ssl関連で正しくリンクしている様に思える。
やはりhomebrewのSSLディレクトリは怪しくないか?

結果

kindai.rb https://〜実行でSSL通信出来た。

メリット
homebrewよりもstowのほうが使いやすい。

homebrew
homebrewはssl/tsl通信ができなくなる不具合が多発した。切り分けができない。他に2、3年に一回くらい再インストールが必要になる不具合があり、さらに brew doctorで修正が時々必要だった。これの不具合は stow を使うことで皆無になった。
リンク切れを起こすhomebrewって怪しいのでは?
homebrewだとwebp、python、ghostscriptがインストールされるが、なくても動作したので空き容量が増えた。

環境変数
PATH変数が短くなった。
LDFLAGS、CPPFLAGS、PKG_CONFIG_PATH変数が短くなった。
~/.curlrcの
capath=/usr/local/stow/openssl/ssl/
cacert=/usr/local/stow/openssl/ssl/cert.pem
二行は無くてもkindai.rbは動作する。現時点では~/.curlrcは不要と思われる。

課題

(済)1.openssl-111gとreadline-8.0を簡単なディレクトリパス(openssl、readline)にしたい。
openssl-111g、readline-8.0とcurlとrubyの再インストール。

2.curl -kOや-LOは成功してもrbenvのruby〜tar.gzダウンロードやkindai.rbのSSL通信(curl経由?)は失敗することがある。

(済)切り分けのため、curlとopensslの通信動作確認をすること。次にrubyからcurl経由で動作確認をする。2022年5月にそれぞれ動作確認のURLを幾つか追記した。

3.brew install を使うことでopensslとcurl、cacert.pemのバージョン管理は不要だった。今後は更新のタイミングが問題になるだろう。
優先順位は、1.cacert.pem 2,openssl 3,curlだろうか?

4.私の場合100程のrubyのプログラムとkundai.rbが動けばいい。
2.3.xのrubyプログラムを2.5.x系統に修正する必要がある?5つくらい?

デメリット
homebrewを使うといくつかのコマンドを入力するだけで良いが、自前ビルドは時間と手間がかかる。
brew install を使うことでruby、opensslとcurl、cacert.pemのバージョン管理され不具合が検証されている可能性がある。自前ビルドすると全てが自己責任。(但し私の過去記事を読むと分かると思うが、kindai.rbに至るまでにhomebrewのopenssl、curl、ruby、rbenvのconfigureオプション周辺で不具合は確実に存在すると思われる。)

(注2)2022年5月22日追記
$ /usr/bin/curl --version
curl 7.43.0 (x86_64-apple-darwin15.0.0) libcurl/7.43.0 SecureTransport zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz UnixSockets
$ curl-config --version
libcurl 7.43.0
$ curl-config --configure
--disable-static --enable-hidden-symbols --enable-threaded-resolver --with-gssapi --with-darwinssl --without-libssh2
$ ls -la /usr/bin/curl
-rwxr-xr-x 1 root wheel 168K 6 29 2018 /usr/bin/curl
google検索でcurl-config --configureを検索すると、2010年から2013年のリンクが見つかる。curl-config --configureをもっと早く知りたかった。

●疑惑?
やはりhomebrewのSSLディレクトリは怪しいかも?

imagemagickソースインストールする参考リンク

circleci(Ubuntu 12.04)にimagemagickをパッケージからインストールする - Qiita

CentOS 7x OSの初期状態からImageMagickを動かす方法 | Search Light(CakePHP PHP jQuery JavaScript CSS Linux MySQLMachttps://www.searchlight8.com/centos-7x-imagimagick-install/#ImageMagick

2020-08-02 追記
RMagick をビルドする(下準備編) - 続・夕陽のプログラマ

ImageMagickの最新版をソースからコンパイルしてインストールする - 文系プログラマによるTIPSブログ

Rubyをソースインストールの参考リンク

MacにRubyをソースからインストールする - Qiita

openssl とruby のSSLについての参考リンク

質問 ruby OpenSSL::X509::DEFAULT_CERT_FILEを上書きする方法は?(解決済) - Qiita
https://qiita.com/shiro_yone/questions/68aab045861ae4024fc0

opensslにおけるpemファイル設定についての質問(解決済) - Qiita
https://qiita.com/shiro_yone/questions/3621d5608ea6cc8c95ff

以上

0
0
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
0
0