Resolving “No such file or directory” Error When Installing Ansible with pip3 on CentOS 7


2 views

When attempting to install Ansible via pip3 on CentOS 7 with both Python 2.7 and 3.6 installed, users frequently encounter the "-bash: /bin/ansible: No such file or directory" error. This occurs despite successful package installation messages from pip.

The key difference between yum install ansible and pip3 install ansible lies in how binaries get installed:

# yum installation paths
/usr/bin/ansible
/usr/lib/python2.7/site-packages/ansible

# pip3 installation paths
/usr/local/bin/ansible
/usr/local/lib/python3.6/site-packages/ansible

First confirm where pip3 installed the binaries:

# Check pip3 installation directory
pip3 show ansible | grep Location

# Verify ansible binary exists
ls -la /usr/local/bin/ansible

# Check $PATH environment variable
echo $PATH

The most straightforward fix is to add pip3's binary directory to your PATH:

# Temporary solution (current session only)
export PATH=$PATH:/usr/local/bin

# Permanent solution (add to ~/.bashrc or /etc/profile)
echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc

For production systems, consider these more robust approaches:

# Method 1: Using Python3 virtual environment
python3 -m venv ansible-venv
source ansible-venv/bin/activate
pip install ansible

# Method 2: Using official RPM for Python3
yum install centos-release-ansible-29
yum install ansible --enablerepo=centos-ansible-29

After successful installation, confirm Ansible uses Python 3:

ansible --version | grep "python version"
# Should show Python 3.6.x in the output

If issues persist, check these potential problems:

# 1. Conflicting installations
which -a ansible

# 2. Python interpreter conflicts
head -n1 $(which ansible)

# 3. Permission issues
ls -ld /usr/local/bin /usr/local/lib/python3.6/site-packages

When attempting to install Ansible via pip3 on CentOS 7 with both Python 2.7 and Python 3.6 installed, the package appears to install successfully but the ansible executable remains unavailable in standard paths. Here's what's happening behind the scenes:

$ which ansible
/usr/bin/which: no ansible in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin)

The key difference between yum and pip installations:

  • Yum install: Places binaries in /usr/bin/ and uses system Python
  • Pip3 install: Installs to /usr/local/lib/python3.6/site-packages/ without creating system symlinks

Check where pip actually installed the modules:

$ python3 -c "import ansible; print(ansible.__file__)"
/usr/local/lib/python3.6/site-packages/ansible/__init__.py

Here's the full working approach:

# First clean up any existing installations
sudo yum remove ansible -y
sudo pip3 uninstall ansible -y

# Install prerequisites
sudo yum install python3-pip python3-devel -y
sudo pip3 install --upgrade pip

# Install Ansible with proper paths
sudo pip3 install ansible --prefix=/usr/local

# Alternative method using venv
python3 -m venv ansible-venv
source ansible-venv/bin/activate
pip install ansible

After successful installation, ensure your PATH includes the correct locations:

echo 'export PATH=$PATH:/usr/local/bin' >> ~/.bashrc
source ~/.bashrc
  • Verify Python interpreter version in ansible.cfg: interpreter_python=/usr/bin/python3
  • Check for conflicting installations: rpm -qa | grep ansible
  • For development setups, consider using virtual environments

For more reliable production deployments:

# Using Python 3 RPM from EPEL
sudo yum install epel-release -y
sudo yum install ansible --enablerepo=epel-testing -y

# Verify Python 3 usage
ansible --version | grep "python version"