Macにcargo-editをインストールする


『実践Rustプログラミング入門』を買ってきたので環境を構築しましたが、MacOS Catalina(10.15.6)だとcargo-editのインストール時にエラーになってしまったので、メモ

carg-editをインストールしようとすると下記のようなエラーが出ます。

$ cargo install cargo-edit
Could not find directory of OpenSSL installation, and this `-sys` crate cannot
proceed without this knowledge. If OpenSSL is installed and this crate had
trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
compilation process.

If you're in a situation where you think the directory *should* be found
automatically, please open a bug at https://github.com/sfackler/rust-openssl
and include information about your system as well as this message.

    $HOST = x86_64-apple-darwin
    $TARGET = x86_64-apple-darwin
    openssl-sys = 0.9.58

It looks like you're compiling on macOS, where the system contains a version of
OpenSSL 0.9.8. This crate no longer supports OpenSSL 0.9.8.

As a consumer of this crate, you can fix this error by using Homebrew to
install the `openssl` package, or as a maintainer you can use the openssl-sys
0.7 crate for support with OpenSSL 0.9.8.

どうもOpenSSLのバージョンが合っていないようです。Homebrewでopensslをインストールしろと書いてあるのでそうします。

$ brew update
$ brew install openssl
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/openssl%401.1-1.1.1g.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/1926679569c6af5337de812d86f4dad2b21ff883ad3a5d2cd9e8836ac5ac7ffe?re
######################################################################## 100.0%
==> Pouring [email protected]
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/[email protected]/certs

and run
  /usr/local/opt/[email protected]/bin/c_rehash

[email protected] is keg-only, which means it was not symlinked into /usr/local,
because macOS provides LibreSSL.

If you need to have [email protected] first in your PATH run:
  echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

For compilers to find [email protected] you may need to set:
  export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
  export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

==> Summary
🍺  /usr/local/Cellar/[email protected]/1.1.1g: 8,059 files, 18MB
==> `brew cleanup` has not been run in 30 days, running now...
Pruned 1 symbolic links and 2 directories from /usr/local

指示通り、中間証明書をキーチェーンに登録します。
finderで/usr/local/etc/[email protected]/を開きます

cert.pemをクリックしてキーチェーンアクセスで証明書を登録します。

指示通り、下記コマンドを実行します。

$ /usr/local/opt/[email protected]/bin/c_rehash

このままだと、MacOSに入っているLibreSSLが使われるので、.zshrcにOpenSSLを使うように指定します。

$ echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

コンパイラが[email protected]を見つけるには下記の記載が必要とのことなので、下記も追加します。

$ vi ~/.zshrc
export LDFLAGS="-L/usr/local/opt/[email protected]/lib"
export CPPFLAGS="-I/usr/local/opt/[email protected]/include"

読み込みます。

$ source ~/.zshrc

これでcargo-editをインストールできるようになりました。

$ cargo install cargo-edit
(省略)
Installed package `cargo-edit v0.6.0` (executables `cargo-add`, `cargo-rm`, `cargo-upgrade`)