Cannot install ansible-pylibssh on macOS

Question:

I’m trying to install ansible-pylibssh on an Apple Silicon based Mac.
ansible is already installed.

When running pip3 install ansible-pylibssh I get the following error:

pip3 install --user ansible-pylibssh                                                                                                 1 ✘  took 3s   at 00:36:15 
Collecting ansible-pylibssh
  Using cached ansible-pylibssh-1.0.0.tar.gz (97 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: ansible-pylibssh
  Building wheel for ansible-pylibssh (pyproject.toml) ... error
  error: subprocess-exited-with-error

  × Building wheel for ansible-pylibssh (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [18 lines of output]
      [1/1] Cythonizing /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/pylibsshext/_libssh_version.pyx
      running build_ext
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src
      creating /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/pylibsshext
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/include/python3.9 -c /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/pylibsshext/_libssh_version.c -o /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/tmpa3ax4nf7/private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/pylibsshext/_libssh_version.o
      /private/var/folders/6l/48k83hdn30bc24655lbtzgx80000gn/T/pip-install-lme1njwa/ansible-pylibssh_0b7185d13e5244b3982c6d892f55319b/src/pylibsshext/_libssh_version.c:757:10: fatal error: 'libssh/libssh.h' file not found
      #include "libssh/libssh.h"
               ^~~~~~~~~~~~~~~~~
      1 error generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for ansible-pylibssh
Failed to build ansible-pylibssh
ERROR: Could not build wheels for ansible-pylibssh, which is required to install pyproject.toml-based projects

However libssh is already installed:

brew install libssh                                                                                                                
Warning: libssh 0.10.4 is already installed and up-to-date.
To reinstall 0.10.4, run:
  brew reinstall libssh

I have tried reinstalling ansible, libssh, none of them worked.

Asked By: Kristof Rado

||

Answers:

The problem was with the package not my computer.

It seems that Github Actions are not available for ARM Macs so no dists were built for this platform.

Manually building and installing from source worked:

$ git clone https://github.com/ansible/pylibssh.git
$ cd pylibssh
$ pip install tox
$ tox -e build-dists

Installing:

cd dist
pip3 install ansible_pylibssh-1.0.1.dev47+g75289d4-cp39-cp39-macosx_12_0_arm64.whl
Defaulting to user installation because normal site-packages is not writeable
Processing ./ansible_pylibssh-1.0.1.dev47+g75289d4-cp39-cp39-macosx_12_0_arm64.whl
Installing collected packages: ansible-pylibssh
Successfully installed ansible-pylibssh-1.0.1.dev47+g75289d4
Answered By: Kristof Rado

I’m having the same problem as you, this issue was finally solved today.
When i manually building and installing from source, still run into this problem:

#include "libssh/libssh.h"
         ^~~~~~~~~~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1

This is because the libssh library was not discovered by clang, so we manually added a softlink to solve it.

  1. Find out the lib dir:
% clang -E -x c - -v < /dev/null 
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
......
ignoring nonexistent directory "/usr/include"
#include "..." search starts here:
#include <...> search starts here:
 /Library/Developer/CommandLineTools/usr/lib/clang/14.0.0/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
......
  1. Create a softlink for libssh:
% cd /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
% sudo ln -s /opt/homebrew/Cellar/libssh/0.10.4/include/libssh/ libssh
  1. Rebuilding and installing from source, Worked!
Answered By: Richard Li

Install libssh:

brew install libssh

Use a python virtual environment :

python3 -m venv .venv
source .venv/bin/activate

EDIT:
Install with correct compiler flags:

CFLAGS="-I $(brew --prefix)/include -I ext -L $(brew --prefix)/lib -lssh" pip install ansible-pylibssh

Thanks, @webknjaz!

OLD Additional steps not needed (preserved for history):

Install tox build tool:

pip install 'tox >= 3.19.0'

Build from source on ARM-based Macs, per the ansible-pylibssh docs:

#git clone https://github.com/ansible/pylibssh.git ~/src/github/ansible/pylibssh
# or, if you use SSH:
git clone ssh://[email protected]/ansible/pylibssh.git ~/src/github/ansible/pylibssh
cd ~/src/github/ansible/pylibssh

Use libraries installed by brew with clang:

export CFLAGS="-I $(brew --prefix)/include -I ext -L $(brew --prefix)/lib -lssh"

Build ansible-pylibssh:

tox -e build-dists

Install built ansible-pylibssh wheel:

pip install ~/src/github/ansible/pylibssh/dist/ansible_pylibssh*.whl

Additional resources that lead to this answer:

  • Kristof Rado‘s answer
  • ansible/pylibssh GitHub issue 207 provides background on why this package is not available for macOS on ARM from PyPi (symlink/softlink to point to the libraries did not help. Correct compiler flags were the needed answer.)

If you prefer not to work with a virtual environment, this may work: replace pip commands python3 -m pip --user and python commands with python3.

Answered By: dmacduff
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.