How to fix Rust failed to run custom build command for `ethercat-sys v0.3.1`

Problem

While trying to install the ethercat or the ethercat-sys crate, you encounter the following issue:

ethercat-sys-build-error.txt
error: failed to run custom build command for `ethercat-sys v0.3.1`

Caused by:
  process didn't exit successfully: `/home/uli/Nextcloud/Experimental/RS402/target/debug/build/ethercat-sys-c6763acfbe4bdda5/build-script-build` (exit status: 101)
  --- stderr

  thread 'main' panicked at /home/uli/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/ethercat-sys-0.3.1/build.rs:8:46:
  Please set the ETHERCAT_PATH env var to the location of a checkout of the Ethercat master after running configure: NotPresent
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Solution

The ethercat-sys crate, which is a dependency of the ethercat crate, requires the location of a local checkout of the EtherCAT master repository to build its native bindings.

To resolve this issue, simply clone the EtherCAT master repository and set the ETHERCAT_PATH environment variable to point to the location of this checkout. Since the ethercat-sys crate needs config.h, just cloning the repository is not enough; you also need to build the EtherCAT master.

clone-ethercat.sh
git clone https://gitlab.com/etherlab.org/ethercat.git ~/.etherlab-ethercat
cd ~/.etherlab-ethercat
./bootstrap
./configure
make -j8

Now you can set the ETHERCAT_PATH environment variable. You can do this temporarily in your terminal session, but I recommend doing it permanently by adding it to your shell configuration file (e.g., ~/.bashrc or ~/.zshrc).

set-ethercat-path.sh
export ETHERCAT_PATH="$HOME/.etherlab-ethercat"

With the ETHERCAT_PATH environment variable set, you should now be able to build your Rust project that depends on the ethercat crate without encountering the previous error. Remember to reload your shell configuration using source ~/.bashrc or similar, or restart your terminal session to apply the changes, if you’ve placed the export command in your shell configuration file.

build-rust-project.sh
cargo build

Check out similar posts by category: Rust, EtherCAT