Keywords: Ubuntu 20.04 | Python | libffi | shared library | compatibility
Abstract: This paper provides an in-depth analysis of the libffi.so.6 missing error encountered when importing Python libraries after upgrading to Ubuntu 20.04 LTS. By examining system library version changes, it presents three primary solutions: creating symbolic links to the new library version, reinstalling Python, and manually installing the legacy libffi6 package. The article compares the advantages and disadvantages of each method from a technical perspective, offering safety recommendations to help developers understand shared library dependencies and effectively address compatibility issues.
Problem Background and Error Analysis
After upgrading the operating system to Ubuntu 20.04 LTS, many Python developers encounter the following error when attempting to import libraries such as NumPy:
ImportError: libffi.so.6: cannot open shared object file: No such file or directory
This error indicates that the Python runtime cannot locate the required shared library file libffi.so.6. When trying to install it via apt-get install libffi, the system reports that the package cannot be found, revealing the core issue: Ubuntu 20.04 has upgraded the libffi library from version 6 to version 7.
Technical Principle: Shared Library Version Management
In Linux systems, shared libraries (e.g., libffi.so) are managed through version numbers. Ubuntu 20.04 replaces libffi6 with libffi7, but some installed Python versions still depend on the older library. This version mismatch prevents the dynamic linker from resolving symbols, leading to import errors. Understanding this is fundamental to all subsequent solutions.
Solution 1: Creating a Symbolic Link
As a temporary fix, a symbolic link can be created to point libffi.so.6 to the existing libffi.so.7 in the system. First, locate the path of the new version library using:
$ find /usr/lib -name "libffi.so*"
Assuming the path is found as /usr/lib/x86_64-linux-gnu/libffi.so.7, create the symbolic link:
sudo ln -s /usr/lib/x86_64-linux-gnu/libffi.so.7 /usr/lib/x86_64-linux-gnu/libffi.so.6
This method tricks the dynamic linker to satisfy the dependency, but note that it may cause incompatibilities with other software, so it is recommended only as a last resort.
Solution 2: Reinstalling Python
For users employing version management tools like pyenv, a safer approach is to reinstall Python. For example, for Python 3.7.4:
pyenv uninstall 3.7.4
pyenv install 3.7.4
The reinstallation process automatically links to the current system's libffi version, ensuring compatibility. This method avoids direct modification of system libraries, reducing potential risks, especially in virtual environments.
Solution 3: Manual Installation of Legacy Package
Another solution involves manually downloading and installing the libffi6 package from Ubuntu 19.10. First, download the deb file:
curl -LO http://archive.ubuntu.com/ubuntu/pool/main/libf/libffi/libffi6_3.2.1-8_amd64.deb
Then install using dpkg:
sudo dpkg -i libffi6_3.2.1-8_amd64.deb
After installation, files will be extracted to the /usr/lib/x86_64-linux-gnu/ directory. For Ubuntu 22.04 users, it is advisable to additionally install libffi-dev and recompile Python for enhanced compatibility.
Comparison and Recommendations
Comparing the three solutions: creating symbolic links is quick but higher risk; reinstalling Python is safe but may affect dependencies; manual installation of the legacy package is a compromise but requires attention to version conflicts. Developers are advised to prioritize using pyenv or virtual environments for Python version management and back up critical configurations before system upgrades. By deeply understanding shared library mechanisms, such compatibility issues can be effectively prevented and resolved.