How to fix "dyld: Library not loaded"?

I ran the following command php -v, and instead of getting the version, I got the following error:

dyld: Library not loaded: /usr/local/opt/libffi/lib/libffi.6.dylib
  Referenced from: /usr/local/bin/php
  Reason: image not found

I tried many things I found online, but the following is what did the trick for me.

How to fix the error

  1. I ran brew update. For me, everything was up to date.

  2. I ran brew upgrade.

    I got the following error:

     Removing: /usr/local/Cellar/php/7.4.4... (517 files, 76MB)
     Error: Directory not empty @ dir_s_rmdir - /usr/local/Cellar/php/7.4.4
    
  3. I ran brew cleanup, and I got a similar error:

    Error: Directory not empty @ dir_s_rmdir - /usr/local/Cellar/nginx/1.17.9
    
  4. I ran sudo rm -R /usr/local/Cellar/nginx/1.17.9.

  5. I ran brew cleanup again, and once again, I got a similar error.

     Error: Directory not empty @ dir_s_rmdir - /usr/local/Cellar/php/7.4.4
    

    This is the same error as my step 2.

  6. I ran sudo rm -R /usr/local/Cellar/php/7.4.4.

  7. I then ran brew cleanup again. It seemed like everything worked.

     Pruned 0 symbolic links and 15 directories from /usr/local
    
  8. Finally, I tried php -v again, and this time it worked!

Summary

If you are getting a "dyld: Library not loaded > Reason: image not found" error, try running brew update and brew upgrade. If you get a Error: Directory not empty, try removing that directory using sudo rm -R.

Make one page websites quickly using my Carrd Templates