Python pygmt error: libgmt.so: cannot open shared object file

Hi,

I installed the pygmt and I did:

python

import pygmt

then I had the message:

Traceback (most recent call last):
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/clib/loading.py", line 42, in load_libgmt
    libgmt = ctypes.CDLL(libpath)
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/ctypes/__init__.py", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libgmt.so: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/__init__.py", line 30, in <module>
    _begin()
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/session_management.py", line 16, in begin
    with Session() as lib:
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/clib/session.py", line 177, in __enter__
    self.create("pygmt-session")
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/clib/session.py", line 325, in create
    c_create_session = self.get_libgmt_func(
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/clib/session.py", line 276, in get_libgmt_func
    self._libgmt = load_libgmt()
  File "/home/conrado/anaconda3/envs/myenv/lib/python3.8/site-packages/pygmt/clib/loading.py", line 51, in load_libgmt
    raise GMTCLibNotFoundError(msg)
pygmt.exceptions.GMTCLibNotFoundError: Error loading the GMT shared library 'libgmt.so':
libgmt.so: cannot open shared object file: No such file or directory

Please, what can I do to solve this,

Conrado

Seems like the shared library cannot be found. Have you tried

export GMT_LIBRARY_PATH=$HOME/anaconda3/envs/pygmt/lib

as mentioned at the bottom of the installation instructions?

I am having a similar issue here: I created a new conda environment and pip install pygmt, and when I import pygmt it showed an error like like this:

GMTCLibNotFoundError: Error loading the GMT shared library libgmt.so.
 libgmt.so: cannot open shared object file: No such file or directory.

Strangely, if I choose to install pygmt by conda install gmt pygmt, Python will load pygmt without any issues. I suppose there is a missing dependency for pip to install this pygmt?

Hi @whyjz,

If I understand it correctly, doing just pip install pygmt into a fresh new conda environment won’t work because pip (Python-based) doesn’t also pull in GMT (a C library that cannot be installed using pip). GMT is a separate installation (see Installing — PyGMT) and can be installed using conda install gmt (recommended) or by other means Download — The Generic Mapping Tools (may need more work to connect to pygmt).

If you prefer to use pip install pygmt, one way is to install gmt first via conda, and then install pygmt too as outlined in Installing — PyGMT. I.e.:

conda create --name pygmt-env gmt
conda activate pygmt-env
pip install pygmt

Or you could just do conda create --name pygmt-env --channel conda-forge pygmt as outlined in the Quickstart install guide. Let me know if this is confusing and I can explain better, but the step by step is to:

Create a Python virtual environment → Install GMT → Install PyGMT

P.S. We’ve got an open Pull Request at Improve PyGMT documentation by core-man · Pull Request #996 · GenericMappingTools/pygmt · GitHub looking to improve the Installation docs. Would be good to clarify the language a bit for new users so head over and suggest any changes!

@weiji14 thanks for getting this back so quickly. Yes, this totally makes sense to me – you cannot simply pip install the GMT library. I forgot this when trying to set up a binder page for my GMT site. Thanks a lot again!
I hope this thread will also serve as a good future reference for people who don’t read the doc well like me… Will check the open pull request too!

1 Like

No worries, I do think we need to make the install docs clearer to more Python users since it might not be immediately obvious why GMT can’t be pip installed. Always good to have a fresh set of eyes on things like this!

Let us know too if you need any other help on PyGMT or GMT, I know you’ve done a lot of work at https://gmt-tutorials.org and it’ll be great to get more translations done :grinning_face_with_smiling_eyes:

2 Likes