Techioz Blog

tiktoken_ruby のインストール時に Heroku で「libclang が見つかりません」というメッセージが表示される

概要

Ruby 3.3.0 と tiktoken_ruby gem を備えたアプリを heroku-22 にデプロイしようとすると、次のエラーが発生します。

remote: -----> Building on the Heroku-22 stack
remote: -----> Using buildpack: heroku/ruby
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-3.3.0
remote:        Ruby version change detected. Clearing bundler cache.
remote:        Old: ruby 3.1.0p0 (2021-12-25 revision fb4df44d16) [x86_64-linux]
remote:        New: ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
remote: -----> Installing dependencies using bundler 2.3.25
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Fetching gem metadata from https://rubygems.org/.........
...
remote:        Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
remote:        
remote:        current directory:
remote:        /tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby
remote:        /tmp/build_529a3ba8/vendor/ruby-3.3.0/bin/ruby extconf.rb
remote:        checking for gcc... yes
remote:        checking for g++... yes
remote:        checking for gcc-ar... yes
remote:        checking for cargo... no
remote:        
remote:        current directory:
remote:        /tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby
remote:        make DESTDIR\= sitearchdir\=./.gem.20240118-209-dqfxjm
remote:        sitelibdir\=./.gem.20240118-209-dqfxjm clean
remote:        
remote:        current directory:
remote:        /tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby
remote:        make DESTDIR\= sitearchdir\=./.gem.20240118-209-dqfxjm
remote:        sitelibdir\=./.gem.20240118-209-dqfxjm
remote:        info: downloading installer
remote:        info: profile set to 'minimal'
remote:        info: default host triple is x86_64-unknown-linux-gnu
remote:        info: skipping toolchain installation
remote:        
remote:        
remote:        Rust is installed now. Great!
remote:        
remote:        To get started you need Cargo's bin directory 
remote:        (/tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby/.rb-sys/stable/cargo/bin)
remote:        in your PATH
remote:        environment variable. This has not been done automatically.
remote:        
remote:        To configure your current shell, run:
remote:        source 
remote:        "/tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby/.rb-sys/stable/cargo/env"
remote:        info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
remote:        info: latest update on 2023-12-28, rust version 1.75.0 (82e1608df 2023-12-21)
remote:        info: downloading component 'cargo'
remote:        info: downloading component 'rust-std'
remote:        info: downloading component 'rustc'
remote:        info: installing component 'cargo'
remote:        info: installing component 'rust-std'
remote:        info: installing component 'rustc'
remote:        
remote:        stable-x86_64-unknown-linux-gnu installed - rustc 1.75.0 (82e1608df
remote:        2023-12-21)
remote:        
remote:        info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
remote:        info: checking for self-update
remote:        info: using existing install for 'stable-x86_64-unknown-linux-gnu'
remote:        info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
remote:        
remote:        stable-x86_64-unknown-linux-gnu unchanged - rustc 1.75.0 (82e1608df
remote:        2023-12-21)
remote:        
remote:        info: note that the toolchain 'stable-x86_64-unknown-linux-gnu' is currently i
remote:        use (environment override by RUSTUP_TOOLCHAIN)
remote:        generating target/release/libtiktoken_ruby.so (release)
remote:        /tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby/.rb-sys/stable/cargo/bin/cargo
remote:        rustc  --manifest-path ./Cargo.toml --target-dir target --lib --profile releas
remote:        -- -C linker=gcc -L native=/tmp/build_529a3ba8/vendor/ruby-3.3.0/lib -C
remote:        link-arg=-lm -l pthread
remote:            Updating crates.io index
remote:            Updating git repository `https://github.com/IAPark/tiktoken-rs.git`
remote:            Updating git submodule `https://github.com/zurawiki/tiktoken`
remote:         Downloading crates ...
remote:          Downloaded glob v0.3.1
remote:          Downloaded nom v7.1.3
remote:          Downloaded rb-sys-build v0.9.81
remote:          Downloaded parking_lot_core v0.9.7
remote:          Downloaded regex v1.7.1
remote:          Downloaded bindgen v0.66.1
remote:          Downloaded bstr v1.4.0
remote:          Downloaded libc v0.2.140
remote:          Downloaded syn v2.0.31
remote:          Downloaded regex-syntax v0.6.28
remote:          Downloaded minimal-lexical v0.2.1
remote:          Downloaded magnus v0.6.1
remote:          Downloaded serde v1.0.157
remote:          Downloaded anyhow v1.0.70
remote:          Downloaded regex-automata v0.1.10
remote:          Downloaded cfg-if v1.0.0
remote:          Downloaded proc-macro2 v1.0.66
remote:          Downloaded fancy-regex v0.11.0
remote:          Downloaded lazycell v1.3.0
remote:          Downloaded clang-sys v1.6.0
remote:          Downloaded aho-corasick v0.7.20
remote:          Downloaded unicode-ident v1.0.8
remote:          Downloaded smallvec v1.10.0
remote:          Downloaded seq-macro v0.3.5
remote:          Downloaded rb-sys v0.9.81
remote:          Downloaded parking_lot v0.12.1
remote:          Downloaded memchr v2.5.0
remote:          Downloaded base64 v0.21.0
remote:          Downloaded bit-vec v0.6.3
remote:          Downloaded lock_api v0.4.9
remote:          Downloaded libloading v0.7.4
remote:          Downloaded autocfg v1.1.0
remote:          Downloaded bitflags v2.4.0
remote:          Downloaded peeking_take_while v0.1.2
remote:          Downloaded shlex v1.1.0
remote:          Downloaded shell-words v1.1.0
remote:          Downloaded scopeguard v1.1.0
remote:          Downloaded rustc-hash v1.1.0
remote:          Downloaded rb-sys-env v0.1.2
remote:          Downloaded quote v1.0.33
remote:          Downloaded once_cell v1.17.1
remote:          Downloaded magnus-macros v0.6.0
remote:          Downloaded lazy_static v1.4.0
remote:          Downloaded cexpr v0.6.0
remote:          Downloaded bit-set v0.5.3
remote:           Compiling memchr v2.5.0
remote:           Compiling proc-macro2 v1.0.66
remote:           Compiling unicode-ident v1.0.8
remote:           Compiling glob v0.3.1
remote:           Compiling clang-sys v1.6.0
remote:           Compiling libc v0.2.140
remote:           Compiling quote v1.0.33
remote:           Compiling minimal-lexical v0.2.1
remote:           Compiling cfg-if v1.0.0
remote:           Compiling nom v7.1.3
remote:           Compiling libloading v0.7.4
remote:           Compiling syn v2.0.31
remote:           Compiling aho-corasick v0.7.20
remote:           Compiling bindgen v0.66.1
remote:           Compiling regex-syntax v0.6.28
remote:           Compiling regex v1.7.1
remote:           Compiling cexpr v0.6.0
remote:           Compiling peeking_take_while v0.1.2
remote:           Compiling lazycell v1.3.0
remote:           Compiling shlex v1.1.0
remote:           Compiling bitflags v2.4.0
remote:           Compiling rustc-hash v1.1.0
remote:           Compiling lazy_static v1.4.0
remote:           Compiling shell-words v1.1.0
remote:           Compiling autocfg v1.1.0
remote:           Compiling lock_api v0.4.9
remote:           Compiling rb-sys-build v0.9.81
remote:           Compiling parking_lot_core v0.9.7
remote:           Compiling rb-sys v0.9.81
remote:           Compiling bit-vec v0.6.3
remote:        error: failed to run custom build command for `rb-sys v0.9.81`
remote:        
remote:        Caused by:
remote:        process didn't exit successfully:
remote:        `/tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby/target/release/build/rb-sys-0b34e8f38d6837b7/build-script-main`
remote:        (exit status: 101)
remote:          --- stdout
remote:          cargo:rerun-if-env-changed=RUBY
remote:          cargo:rerun-if-env-changed=RBCONFIG_CROSS_COMPILING
remote:          cargo:rerun-if-env-changed=RBCONFIG_RUBY_PROGRAM_VERSION
remote:          cargo:rerun-if-env-changed=RBCONFIG_platform
remote:          cargo:rerun-if-env-changed=RUBY_ROOT
remote:          cargo:rerun-if-env-changed=RUBY_VERSION
remote:          cargo:rerun-if-env-changed=RUBY
remote:          cargo:rerun-if-changed=build/main.rs
remote:          cargo:rerun-if-changed=build/stable_api_config.rs
remote:          cargo:rerun-if-changed=build/version.rs
remote:          cargo:rerun-if-changed=build/features.rs
remote:          cargo:rerun-if-env-changed=RUBY_STATIC
remote:          cargo:rerun-if-env-changed=RBCONFIG_ENABLE_SHARED
remote:          cargo:rerun-if-env-changed=RBCONFIG_rubyhdrdir
remote:          cargo:rerun-if-env-changed=RBCONFIG_rubyarchhdrdir
remote:          cargo:rerun-if-env-changed=RBCONFIG_CPPFLAGS
remote:          cargo:rerun-if-env-changed=RBCONFIG_rubyhdrdir
remote:          cargo:rerun-if-env-changed=TARGET
remote:          cargo:rerun-if-env-changed=TARGET
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64-unknown-linux-gnu
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64-unknown-linux-gnu
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
remote:          cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
remote:        
remote:          --- stderr
remote:        INFO: using bindgen with clang args:
remote:        ["-I/tmp/build_529a3ba8/vendor/ruby-3.3.0/include/ruby-3.3.0",
remote:        "-I/tmp/build_529a3ba8/vendor/ruby-3.3.0/include/ruby-3.3.0/x86_64-linux",
remote:        "-fms-extensions", "-O3", "-fno-fast-math", "-g", "-Wall", "-Wextra",
remote:        "-Wdeprecated-declarations", "-Wdiv-by-zero", "-Wduplicated-cond",
remote:        "-Wimplicit-function-declaration", "-Wimplicit-int", "-Wpointer-arith",
remote:        "-Wwrite-strings", "-Wold-style-definition", "-Wimplicit-fallthrough=0",
remote:        "-Wmissing-noreturn", "-Wno-cast-function-type",
remote:        "-Wno-constant-logical-operand", "-Wno-long-long",
remote:        "-Wno-missing-field-initializers", "-Wno-overlength-strings",
remote:        "-Wno-packed-bitfield-compat", "-Wno-parentheses-equality", "-Wno-self-assign"
remote:        "-Wno-tautological-compare", "-Wno-unused-parameter", "-Wno-unused-value",
remote:        "-Wsuggest-attribute=format", "-Wsuggest-attribute=noreturn",
remote:        "-Wunused-variable", "-Wmisleading-indentation", "-Wundef"]
remote:        thread 'main' panicked at
remote:        /tmp/build_529a3ba8/vendor/bundle/ruby/3.3.0/bundler/gems/tiktoken_ruby-cd53cc74f6a4/ext/tiktoken_ruby/.rb-sys/stable/cargo/registry/src/index.crates.io-6f17d22bba15001f/bindgen-0.66.1/lib.rs:604:31:
remote:        Unable to find libclang: "couldn't find any valid shared libraries matching:
remote:        ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the
remote:        `LIBCLANG_PATH` environment variable to a path where one of these files can be
remote:        found (invalid: [])"
remote:          note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

解決策

Clang をインストールする必要がありました。

heroku buildpacks:add --index 1 heroku-community/apt

次に、リポジトリのルートにある Aptfile で次のようにします。

clang