108

I recently upgraded my OS to Ubuntu 20.04 LTS.

Now when I try to import a library like Numpy in Python, I get the following error:

ImportError: libffi.so.6: cannot open shared object file: No such file or directory

I tried installing the libffi package, but apt can't locate it :

sudo apt-get install libffi
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libffi
3

9 Answers 9

104

It seems like I fixed it. I could be wrong, but here is what I think happened:

  1. Ubuntu 20.04 upgraded libffi6 to libffi7
  2. Python is still looking for libffi6

What I did to fix it :

Locate libffi.so.7 in your system

$ find /usr/lib -name "libffi.so*"

Create a simlink named libffi.so.6 that points to libffi.so.7:

sudo ln -s /usr/path/to/libffi.so.7 /usr/lib/path/to/libffi.so.6

UPDATE:

As noted by many users, this fix could have unintended consequences. The better way to do it is to reinstall python as @amichaud explained. This should be used as a last resort IF you're not using pyenv/virtualenv/etc in which case removing python will cause a lot of dependencies to be removed as well.

4
  • 3
    I'd be very surprised if this actually works - symlinking one version of a lib to a different version seems likely to cause instability. Rebuilding python seems to be the proper solution Jul 16, 2020 at 23:30
  • It actually worked for me. I'm aware that it's not the cleanest way to do it, but this way you can avoid reinstalling all the python packages / dependencies. Regarding the instability, I don't see why it's a "recipe for disaster". At worst, you'll just need to revert back to rebuilding python. That said, I'm willing to change the accepted answer if it can cause more concerning problems that I'm not aware of.
    – Kh4zit
    Jul 17, 2020 at 8:26
  • 1
    When you say works though you appear to mean doesn't crash on startup. You cannot know if python is behaving slightly differently than it should because you're calling the wrong lib, subtle differences in execution could occur which would be impossible to detect. Easier but annoying would be a crash at some random point in the future because finally some python code results in a call to the lib which has changed enough that it fails in an obvious matter. Jul 18, 2020 at 0:32
  • If you want to avoid "losing" your installed packages just do pip freeze > backup.txt, remove the Python installs, reinstall them and use pip install -r backup.txt and you'll be back to your original state Oct 23, 2020 at 13:29
94

If you are using pyenv, you should just uninstall the used python version and then reinstall it.

Example:

pyenv uninstall 3.7.4
pyenv install 3.7.4

It's cleaner and safer than renaming system library from my point of view.

8
  • 4
    This is the right way to do it - faking a lib version is a last-resort option and could have unintended consequences.
    – bsplosion
    Jun 22, 2020 at 12:35
  • 14
    even pyenv install 3.7.4 -f which allows to not recreate virtualenvs Jun 26, 2020 at 17:49
  • Reinstalling Python is the ticket, whether with pyenv or otherwise. Python will find the updated library when it's building.
    – Franey
    Jul 9, 2020 at 13:13
  • 4
    This really should be the top / accepted answer! symlinking a different lib seems like a recipe for disaster Jul 16, 2020 at 23:29
  • 1
    This also work for poetry users Dec 8, 2021 at 10:57
83

I am using Xubuntu 20.04 and recompiling the python version 3.7 did not work for me.

The way I solved this was to download the 19.10 version of the package from here: https://mirrors.kernel.org/ubuntu/pool/main/libf/libffi/libffi6_3.2.1-8_amd64.deb

and then installing it

sudo apt install ./libffi6_3.2.1-8_amd64.deb

This will unpack the libffi.so.6 and libffi.so.6.0.4 files to /usr/lib/x86_64-linux-gnu/. The libffi.so.6 file is just a link to libffi.so.6.0.4 in the same directory.

As far as I could see this does not overwrite any files so should be safe.

Ubuntu 22.04 additional step As per comment from pijing below, you need to run this command after installing the above:

apt install libffi-dev

Then recompile Python.

8
  • 1
    I tried all the answers above, and they failed. This one worked on Ubuntu 21.04. Sep 18, 2021 at 16:10
  • 1
    This worked for me (Ubuntu 20.04). Nov 8, 2021 at 18:04
  • This worked for me. After install deb,you also need to run command "apt install libffi-devel " and then recompile the python. (Ubuntu 22.04.1)
    – pijing
    Nov 23, 2022 at 3:50
  • I had an issue with installing clang-format-11 which at the end happened that it depended on libffi.so.6. So downloading manually from this link and installing the package helped me. Thanks!
    – winsett
    Feb 17 at 8:18
  • the http link doesn't work anymore, it must be amended to https Jul 8 at 19:16
27

Ubuntu 20 has libffi7 installed instead. It's possible to install the previous version using coming from Ubuntu 19.10 (Eoan Ermine) download from here Or you can follow these commands

$ curl -LO http://archive.ubuntu.com/ubuntu/pool/main/libf/libffi/libffi6_3.2.1-8_amd64.deb

$ sudo dpkg -i libffi6_3.2.1-8_amd64.deb
3
  • Thanks! Confirmed to be working @ Ubuntu 20.04.2. Feb 9, 2022 at 0:53
  • This was the solution, for some reason apt or apt-get cannot find this package
    – Carlos
    Apr 13 at 18:45
  • Works @ Ubuntu 22
    – jef
    Aug 24 at 5:12
4

Same problem for me

  1. Upgraded to Ubuntu 20
  2. pip didn't work anymore (same error)

What I did was:

  1. Delete the virtual env I was using
  2. Recreate it

Sure, I wasn't able to do a pip freeze to get save my dependencies (as pip didn't work), but fortunately I didn't care about them.

3

The libffi6 package may be downloaded and installed as follows:

Identify a source for apt from the list Download Page for libffi6 (I picked http://mirrors.kernel.org/ubuntu/ for instance)

Make a back up of /etc/apt/sources.list (just in case)

Edit /etc/apt/sources.list and add the line (I added it to the very end of the file) ands save the file

deb https://mirrors.kernel.org/ubuntu bionic main

Update to use the new repository

sudo apt update

Finally, install the package:

sudo apt install libffi6

Note that both libffi6 and libffi7 appear to coexist. (My Ubuntu version is 20.04)

$ sudo apt list | grep libffi[67]/

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

libffi6/bionic,now 3.2.1-8 amd64 [installed]
libffi7/focal,now 3.3-4 amd64 [installed,automatic]
1

Symbolic linking to higher version of existing libffi,(e.g. pointing 6 to 8 or 9) does NOT harm since libffi's interface is almost frozen up to 9 years from now.

Higher SO version such as 6,7 or 8, simply it's there to indicate minimum requirement in case of new feature availability such as ffi_tramp_is_present. libffi itself is totally backward compatible so far as of 2022.

Anyone can confirm this by browsing inc folder in https://github.com/libffi/libffi

So, if you are seeing this issue in Ubuntu 22, please feel free to create a symbolic link to highest version of so available in your distro.

0

I had the same problem (when I upgrded to Ubuntu 20.04) when I tried to run Jupyter Notebook.

Step 1) --> Just re-install python3.7.6 (the version I was using) :

$ cd path_to_python3.7_folder
  • Do again de installation process:
    $ ./configure --enable-optimizations
    $ make
    $ sudo make install

  1. Step 2: uninstall jupyter. I used pip3.7 uninstall ... See: How to uninstall Jupyter note book installed by pip3

  2. Step 3: Re-install jupyter again:

    $ pip3.7 install jupyterlab
    $ pip3.7 install notebook

Try to run jupyter again. It should work.

-1

The problem with libffi can also be tackled with making a symlink: sudo ln -s /usr/path/to/libffi.so.8 /usr/lib/path/to/libffi.so.7 Then you get another error in xorg log concerning wayland. What helped me was to reinstall wayland and lib32-wayland. After that I could boot normally (although I use xorg in Cinnamon, but also have Gnome installed)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.