png R library: resolving libpng.so error - lmmx/devnotes GitHub Wiki

libpng16.so.16: cannot open shared object file: No such file or directory

The png package is required by various other libraries, but on Ubuntu many have reported it fails as follows:

> install.packages("png")
Installing package into ‘/home/louis/opt/R/libs’
(as ‘lib’ is unspecified)
trying URL 'https://mirrors.ebi.ac.uk/CRAN/src/contrib/png_0.1-7.tar.gz'
Content type 'unknown' length 24990 bytes (24 KB)
==================================================
downloaded 24 KB

* installing *source* package ‘png’ ...
** package ‘png’ successfully unpacked and MD5 sums checked
** libs
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      `libpng-config --cflags` -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c read.c -o read.o
gcc -std=gnu99 -I/usr/share/R/include -DNDEBUG      `libpng-config --cflags` -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g  -c write.c -o write.o
gcc -std=gnu99 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o png.so read.o write.o -L/home/louis/anaconda3/lib -lpng16 -lm -lz -lm -L/usr/lib/R/lib -lR
installing to /home/louis/opt/R/libs/png/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/home/louis/opt/R/libs/png/libs/png.so':
  libpng16.so.16: cannot open shared object file: No such file or directory
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/home/louis/opt/R/libs/png’

The downloaded source packages are in
        ‘/tmp/RtmpGg4rjz/downloaded_packages’
Warning message:
In install.packages("png") :
  installation of package ‘png’ had non-zero exit status

I already have this .so file on my computer in various places (including from former versions of R)

function findafile (){ find / -iname "$@" 2>/dev/null; }
findafile png.so
  • @/R/x86_64-pc-linux-gnu-library/3.0/png/libs/png.so
  • ~/R/x86_64-pc-linux-gnu-library/3.2/png/libs/png.so
  • /usr/lib/x86_64-linux-gnu/imlib2/loaders/png.so
  • /usr/lib/x86_64-linux-gnu/ImageMagick-6.7.7/modules-Q16/coders/png.so
findafile libpng16.so.16
  • ~/anaconda3/pkgs/libpng-1.6.17-0/lib/libpng16.so.16
  • ~/anaconda3/lib/libpng16.so.16
findafile libpng16.so.16.17.0
  • /home/louis/anaconda3/pkgs/libpng-1.6.17-0/lib/libpng16.so.16.17.0
  • /home/louis/anaconda3/lib/libpng16.so.16.17.0

This answer suggests ln -s symlinking but copying is fine (and e.g. protects against you deleting your old R version trees and losing any retro-linked libraries)

User stuckinsd recommends:

mkdir /home/kuragari/R/x86_64-pc-linux-gnu-library/3.2/png/
mkdir /home/kuragari/R/x86_64-pc-linux-gnu-library/3.2/png/libs/
ln -s /usr/kuragari/x86_64-linux-gnu/imlib2/loaders/png.so /home/tlu/R/x86_64-pc-linux-gnu-library/3.2/png/libs/
ln -s /home/kuragari/anaconda/lib/libpng16.so.16 /home/tlu/R/x86_64-pc-linux-gnu-library/3.2/png/libs/
ln -s /home/kuragari/anaconda/lib/libpng16.so.16.17.0 /home/tlu/R/x86_64-pc-linux-gnu-library/3.2/png/libs/

Info on shared libraries here.

I have 3.3, and use a personal package library at ~/opt/R/libs/, so it was:

mkdir -p ~/opt/R/libs/png/libs/
# old version's built library:
ln -s /usr/lib/x86_64-linux-gnu/imlib2/loaders/png.so ~/opt/R/libs/png/libs/
ln -s /home/louis/anaconda3/lib/libpng16.so.16 ~/opt/R/libs/png/libs/
ln -s /home/louis/anaconda3/lib/libpng16.so.16.17.0 ~/opt/R/libs/png/libs/

# There'll still be the same error if you run `install.packages('png')`
# you need to run `sudo ldconfig` inside the folder with the `.so` files
# (what it does is explained here: http://stackoverflow.com/a/28460293/2668831

cd ~/opt/R/libs/png/libs/
sudo ldconfig
  • Note: trial and error showed only the /usr/lib/x86_64-linux-gnu/imlib2/loaders/png.so version would work
  • I had to start R with sudo to get the copy from the above location to work