Merge branch 'release-2020' into master
[alexxy/gromacs.git] / docs / gmxapi / userguide / install.rst
1 ==============================
2 Full installation instructions
3 ==============================
4
5 .. highlight:: bash
6
7 Installation instructions for the :py:mod:`gmxapi` Python package,
8 built on GROMACS.
9
10 Command line examples assume the `bash <https://www.gnu.org/software/bash/>`_ shell.
11
12 .. note:: Regarding multiple GROMACS installations:
13     Many GROMACS users switch between multiple GROMACS installations on the same
14     computer using an HPC module system and/or a :ref:`GMXRC <getting access to |Gromacs|>` configuration script.
15     For the equivalent sort of environment switching with the :py:mod:`gmxapi` Python package,
16     we recommend installing it in a different
17     `Python virtual environment <https://www.google.com/search?q=python+virtual+environment>`_
18     for each GROMACS installation.
19     Once built, a particular copy of the :py:mod:`gmxapi` Python package always refers to the
20     same GROMACS installation.
21
22 .. contents:: Contents
23     :local:
24     :depth: 2
25
26 .. note::
27
28     The following documentation contains frequent references to the pip_ tool
29     for installing Python packages. In some cases, an unprivileged user should
30     use the ``--user`` command line flag to tell pip_ to install packages
31     into the user site-packages directory rather than the default site-packages
32     directory for the Python installation. This flag is not appropriate when
33     running :command:`pip` in a virtual environment (as recommended) and is omitted in
34     this documentation. If you need the ``--user`` flag, you should modify the
35     example commands to look something like :command:`pip install --upgrade somepackage --user`
36
37 .. note::
38
39     These instructions use the executable names :command:`python` and :command:`pip`
40     instead of :command:`python3` or :command:`pip3`. Some Python installations require the ``3``
41     suffix, but it is usually not necessary if you have already activated a Python
42     virtual environment (recommended).
43
44 Overview
45 ========
46
47 Typically, setting up the *gmxapi* Python package follows these three steps.
48 If this overview is sufficient for your computing environment,
49 you may disregard the rest of this document.
50
51 Install GROMACS
52 ---------------
53
54 Locate your GROMACS installation, or build and install GROMACS 2020 or higher.
55
56 .. seealso:: `GROMACS installation <http://manual.gromacs.org/documentation/current/install-guide/index.html>`_
57
58 The following assumes GROMACS is installed to :file:`/path/to/gromacs`
59
60 Set up a Python virtual environment
61 -----------------------------------
62
63 ::
64
65     python3 -m venv $HOME/myvenv
66     . $HOME/myvenv/bin/activate
67     python -m ensurepip --default-pip
68     pip install --upgrade pip setuptools
69     pip install --upgrade cmake scikit-build
70
71 .. seealso:: :ref:`gmxapi venv`
72
73 Install the gmxapi Python package
74 ---------------------------------
75
76 ::
77
78     . /path/to/gromacs/bin/GMXRC
79     pip install gmxapi
80
81 .. seealso:: :ref:`installation`
82
83 Background
84 ==========
85
86 *gmxapi* comes in three parts:
87
88 * GROMACS gmxapi library for C++.
89 * This Python package, supporting Python 3.6 and higher
90 * MD restraint plugins and sample gmxapi client code
91
92 GROMACS requirements
93 --------------------
94
95 The Python package requires a GROMACS installation.
96 Locate an existing GROMACS installation, or
97 `build and install GROMACS <http://manual.gromacs.org/documentation/current/install-guide/index.html>`_
98 before proceeding.
99
100 .. note::
101
102     Note that gmxapi requires that GROMACS is configured with ``GMXAPI=ON`` and ``BUILD_SHARED_LIBS=ON``.
103     These are enabled by default in most cases. If these options were overridden
104     for your GROMACS installation, you will see CMake errors when trying to build
105     and install the gmxapi Python package or other client software.
106
107 Then, "source" the :file:`GMXRC` file from the GROMACS installation
108 :ref:`as you normally would <getting access to |Gromacs|>`
109 before using GROMACS, or note its installation location so that you can pass it
110 to the build configuration.
111
112 Build system requirements
113 -------------------------
114
115 gmxapi can be built for Python 3.6 and higher.
116
117 You will need a C++ 14 compatible compiler and a reasonably up-to-date version
118 of CMake.
119 Full gmxapi functionality may also require an MPI compiler (e.g. :command:`mpicc`).
120
121 Important: To build a module that can be imported by Python, you need a Python
122 installation that includes the Python headers. Unfortunately, it is not always
123 obvious whether these headers are present or where to find them. The simplest
124 answer is to just try to build the Python package using these instructions, and
125 if gmxapi is unable to find the Python tools it needs, try a different Python
126 installation or install the additional development packages.
127
128 On a Linux system, this may require installing packages such as ``python-dev``
129 and/or ``python3-dev``.
130 If you are building Python, either from scratch or with a tool like
131 :command:`pyenv install` (see
132 `wiki entry <https://github.com/pyenv/pyenv/wiki#how-to-build-cpython-with---enable-shared>`_
133 ),
134 be sure to enable installation of the Python C library with the
135 ``--enable-shared`` flag.
136 Alternatively, various Python distributions provide a
137 sufficient build environment while only requiring installation into a user
138 home directory. (Some examples below.)
139
140 If you are using an HPC system with software available through modules you may
141 be able to just :command:`module load` a different Python installation and find one
142 that works.
143
144 Python environment requirements
145 -------------------------------
146
147 gmxapi requires Python 3.6 or higher. Check your version with
148 :command:`python3 --version` or :command:`python --version`.
149
150 ..  note::
151
152     The following documentation assumes you do not need to use a trailing '3' to
153     access a Python 3 interpreter on your system.
154     The default Python interpreter on your system may use :command:`python3` and :command:`pip3`
155     instead of :command:`python` and :command:`pip`. You can check the version with
156     :command:`python3 --version` or :command:`python --version` and :command:`pip --version`.
157
158 To build and install, you need the Python packages for
159 cmake_, networkx_, scikit-build_, and setuptools_
160 (all available from `PyPI with pip <https://pip.pypa.io/en/stable/>`_).
161
162 For full functionality, you should also have mpi4py_ and numpy_.
163 These requirements and version numbers are listed in :file:`requirements.txt`.
164
165 The easiest way to make sure you have the requirements installed, first update
166 pip_, then use the :file:`requirements.txt` file provided with the repository.
167 File paths in this section are relative to the root directory of your local copy
168 of the GROMACS source.
169
170 Confirm that pip_ is available, install pip_ if it is missing, or get
171 instructions on how to install pip_::
172
173     python -m ensurepip --default-pip
174
175 Install or upgrade required components::
176
177     python -m pip install --upgrade pip
178     pip install --upgrade setuptools
179
180 "requirements" files in GROMACS source tree
181 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182
183 If you are building from source code in a local copy of the GROMACS source
184 repository, some helpful files allow you to preinstall the Python requirements
185 before installing the :py:mod:`gmxapi` package.
186
187     pip install -r python_packaging/src/requirements.txt
188
189 If building documentation or running tests,
190 :command:`pip install -r python_packaging/requirements-docs.txt` or
191 :command:`pip install -r python_packaging/requirements-test.txt`,
192 respectively, or see below.
193
194 Documentation build requirements
195 --------------------------------
196
197 See :ref:`gmxapi_package_documentation`
198
199 .. _testing_requirements:
200
201 Testing requirements
202 --------------------
203
204 Testing is performed with `pytest <https://docs.pytest.org/en/latest/>`_.
205 Tests also require numpy_.
206 You can probably install both with pip_::
207
208     pip install pytest numpy
209
210 To test the full functionality also requires an MPI parallel environment.
211 You will need the mpi4py_ Python package and an MPI launcher
212 (such as :command:`mpiexec`, :command:`mpirun`, a launcher provided by your HPC queuing system,
213 or whatever is provided by your favorite MPI package for your operating system).
214
215 .. _mpi_requirements:
216
217 MPI requirements
218 ----------------
219
220 For the ensemble simulations features, you will need an MPI installation.
221 On an HPC system, this means you will probably have to use :command:`module load`
222 to load a compatible set of MPI tools and compilers.
223 Check your HPC documentation or try :command:`module avail` to look for an
224 ``openmpi``, ``mpich``, or ``mvapich`` module and matching compiler module.
225 This may be as simple as::
226
227     module load gcc
228     module load mpicc
229
230 Note that the compilers loaded might not be the first compilers discovered
231 automatically by the build tools we will use below,
232 so you may have to specify compilers on the command line for consistency.
233 It may be necessary to require that GROMACS, gmxapi,
234 and the sample code are built with the same compiler(s).
235
236 Note that strange errors have been known to occur when mpi4py_ is built with
237 different a different tool set than has been used to build Python and gmxapi.
238 If the default compilers on your system are not sufficient for GROMACS or gmxapi,
239 you may need to build, e.g., OpenMPI or MPICH, and/or build mpi4py_ with a
240 specific MPI compiler wrapper. This can complicate building in environments such
241 as Conda_.
242
243 Set the MPICC environment variable to the MPI compiler wrapper and forcibly
244 reinstall mpi4py_::
245
246     export MPICC=`which mpicc`
247     pip install --no-cache-dir --upgrade --no-binary ":all:" --force-reinstall mpi4py
248
249 If you have a different MPI C compiler wrapper, substitute it for :command:`mpicc` above.
250
251 .. _installation:
252
253 Installing the Python package
254 =============================
255
256 We recommend using Python's `pip <https://pip.pypa.io/en/stable/>`_
257 package installer to automatically download, build, and install the latest
258 version of the gmxapi package into a Python
259 `virtual environment <https://docs.python.org/3/tutorial/venv.html>`_,
260 though it is also possible to install without a virtual environment.
261 If installing without a virtual environment as an un-privileged user,
262 you may need to set the CMake variable ``GMXAPI_USER_INSTALL``
263 (``-DGMXAPI_USER_INSTALL=ON`` on the :command:`cmake` command line)
264 and / or use the ``--user`` option with :command:`pip install`.
265
266 Recommended installation
267 ------------------------
268
269 The instructions in this section assume that *pip* is able to download files
270 from the internet. Alternatively, refer to :ref:`gmxapi offline install`.
271
272 Locate or install GROMACS
273 ^^^^^^^^^^^^^^^^^^^^^^^^^
274
275 You need a GROMACS installation that includes the gmxapi headers and library.
276 If GROMACS 2020 or higher is already installed,
277 *and* was configured with ``GMXAPI=ON`` at build time,
278 you can just source the :ref:`GMXRC <getting access to |Gromacs|>`
279 (so that the Python package knows where to find GROMACS)
280 and skip to the next section.
281
282 Otherwise, install a supported version of GROMACS.
283 When building GROMACS from source, be sure to configure cmake with the flag
284 ``-DGMXAPI=ON`` (default).
285
286 Set the environment variables for the GROMACS installation so that the gmxapi
287 headers and library can be found when building the Python package.
288 If you installed to a :file:`gromacs-gmxapi` directory in your home directory as
289 above and you use the :command:`bash` shell, do::
290
291     source $HOME/gromacs-gmxapi/bin/GMXRC
292
293 .. _gmxapi venv:
294
295 Set up a Python virtual environment
296 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
297
298 We recommend installing the Python package in a virtual environment.
299 If not installing in a virtual environment, you may not be able to install
300 necessary prerequisites (e.g. if you are not an administrator of the system you are on).
301
302 The following instructions use the :py:mod:`venv` module.
303 Alternative virtual environments, such as Conda_,
304 should work fine, but are beyond the scope of this document.
305 (We welcome contributed recipes!)
306
307 Depending on your computing environment, the Python 3 interpreter may be accessed
308 with the command :command:`python` or :command:`python3`. Use :command:`python --version` and
309 :command:`python3 --version` to figure out which you need to use. The following assumes
310 the Python 3 interpreter is accessed with :command:`python3`.
311
312 Create a Python 3 virtual environment::
313
314     python3 -m venv $HOME/myvenv
315
316 Activate the virtual environment. Your shell prompt will probably be updated with the name of the environment you
317 created to make it more obvious.
318
319 .. code-block:: none
320
321     $ source $HOME/myvenv/bin/activate
322     (myvenv)$
323
324 ..  note::
325
326     After activating the *venv*, :command:`python` and :command:`pip` are sufficient.
327     (The '3' suffix will no longer be necessary and will be omitted in the rest
328     of this document.)
329
330 Activating the virtual environment may change your shell prompt to indicate the
331 environment is active. The prompt is omitted from the remaining examples, but
332 the remaining examples assume the virtual environment is still active.
333 (Don't do it now, but you can deactivate the environment by running :command:`deactivate`.)
334
335 Install dependencies
336 ^^^^^^^^^^^^^^^^^^^^
337
338 It is always a good idea to update pip_ and setuptools_ before installing
339 new Python packages::
340
341     pip install --upgrade pip setuptools
342
343 The gmxapi installer requires a few additional packages. It is best to make sure
344 they are installed and up to date before proceeding.
345
346 ::
347
348     pip install --upgrade cmake scikit-build
349
350 For MPI, we use mpi4py_.
351 Make sure it is using the same MPI installation that we are building
352 GROMACS against and building with compatible compilers.
353
354 ::
355
356     python -m pip install --upgrade pip setuptools
357     MPICC=`which mpicc` pip install --upgrade mpi4py
358
359 .. seealso:: :ref:`mpi_requirements`
360
361 Install the latest version of gmxapi
362 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
363
364 Fetch and install the latest official version of gmxapi from the Python Packaging Index::
365
366     # Get the latest official release.
367     pip install gmxapi
368
369 The `PyPI repository <https://pypi.org/project/gmxapi/#history>`_
370 may include pre-release versions,
371 but :command:`pip` will ignore them unless you use the ``--pre`` flag::
372
373     # Get the latest version, including pre-release versions.
374     pip install --pre gmxapi
375
376 If :command:`pip` does not find your GROMACS installation, use one of the following
377 environment variables to provide a hint.
378
379 gmxapi_DIR
380 ~~~~~~~~~~
381
382 If you have a single GROMACS installation at :file:`/path/to/gromacs`, it is usually
383 sufficient to provide this location to :command:`pip` through the :envvar:`gmxapi_DIR`
384 environment variable.
385
386 Example::
387
388     gmxapi_DIR=/path/to/gromacs pip install gmxapi
389
390 GMXTOOLCHAINDIR
391 ~~~~~~~~~~~~~~~
392
393 If you have multiple builds of GROMACS distinguished by suffixes
394 (e.g. *_d*, *_mpi*, etcetera), or if you need to provide extra hints to :command:`pip`
395 about the software tools that were used to build GROMACS, you can specify a
396 directory in which the installer can find a CMake "tool chain".
397
398 In the following example, ``${SUFFIX}`` is the suffix that distinguishes the
399 particular build of GROMACS you want to target (refer to GROMACS installation
400 instructions for more information.) ``${SUFFIX}`` may simply be empty, or ``''``.
401
402 ::
403
404     GMXTOOLCHAINDIR=/path/to/gromacs/share/cmake/gromacs${SUFFIX} pip install gmxapi
405
406 Install from source
407 -------------------
408
409 You can also install the :py:mod:`gmxapi` Python package from within a local copy of
410 the GROMACS source repository. Assuming you have already obtained the GROMACS
411 source code and you are in the root directory of the source tree, you will find
412 the :py:mod:`gmxapi` Python package sources in the :file:`python_packaging/src` directory.
413
414 ::
415
416     cd python_packaging/src
417     pip install -r requirements.txt
418     pip install .
419
420 .. _gmxapi offline install:
421
422 Offline install
423 ---------------
424
425 If the required dependencies are already installed, you can do a quick installation
426 without internet access, either from the source directory or from a source archive.
427
428 For example, the last line of the previous example could be replaced with::
429
430     pip install --no-cache-dir --no-deps --no-index --no-build-isolation .
431
432 Refer to pip_ documentation for descriptions of these options.
433
434 If you have built or downloaded a source distribution archive, you can provide
435 the archive file to :command:`pip` instead of the ``.`` argument::
436
437     pip install gmxapi-0.1.0.tar.gz
438
439 In this example, the archive file name is as was downloaded from
440 `PyPI <https://pypi.org/project/gmxapi/#history>`_ or as built locally,
441 according to the following instructions.
442
443 Building a source archive
444 -------------------------
445
446 A source archive for the gmxapi python package can be built from the GROMACS
447 source repository using Python ``setuptools`` and ``scikit-build``.
448
449 Example::
450
451     pip install --upgrade setuptools scikit-build
452     cd python_packaging/src
453     python setup.py sdist
454
455 This command will create a ``dist`` directory containing a source distribution
456 archive file. The file name has the form *gmxapi-<version>.<suffix>*, where
457 *<version>* is the version from the ``setup.py`` file, and *<suffix>* is
458 determined by the local environment or by additional arguments to ``setup.py``.
459
460 .. seealso::
461
462     Python documentation for
463     `creating a source distribution
464     <https://docs.python.org/3/distutils/sourcedist.html#creating-a-source-distribution>`_
465
466 Package maintainers may update the online respository by uploading a freshly
467 built ``sdist`` with ``python -m twine upload dist/*``
468
469 .. _gmxapi_package_documentation:
470
471 Accessing gmxapi documentation
472 ==============================
473
474 Documentation for the Python classes and functions in the gmx module can
475 be accessed in the usual ways, using ``pydoc`` from the command line or
476 ``help()`` in an interactive Python session.
477
478 The complete documentation (which you are currently reading)
479 can be browsed `online <http://manual.gromacs.org/current/gmxapi/>`__
480 or built from a copy of the GROMACS source repository.
481
482 Documentation is built from a combination of Python module documentation and
483 static content, and requires a local copy of the GROMACS source repository.
484
485 Build with GROMACS
486 ------------------
487
488 To build the full gmxapi documentation with GROMACS, configure GROMACS with
489 ``-DGMX_PYTHON_PACKAGE=ON`` and build the GROMACS documentation normally.
490 This will first build the *gmxapi* Python package and install it to a temporary
491 location in the build tree. Sphinx can then import the package to automatically
492 extract Python docstrings.
493
494 Sometimes the build environment can choose a different Python interpreter than
495 the one you intended.
496 You can set the ``Python3_ROOT`` or ``CMAKE_PREFIX_PATH`` CMake variable to
497 explicitly choose the Python installation or *venv* directory.
498
499 If you use pyenv or pyenv-virtualenv to dynamically manage your Python version,
500 you can help identify a particular version with ``pyenv version-name`` and the
501 directory with ``pyenv prefix {version}``. For example::
502
503     -DPython3_ROOT=$(pyenv prefix $(pyenv version-name))
504
505 Docker web server
506 -----------------
507
508 Alternatively, build the ``docs`` Docker image from ``python_packaging/docker/docs.dockerfile``
509 or pull a prebuilt image from DockerHub. Refer to the dockerfile or to
510 https://hub.docker.com/r/gmxapi/docs for more information.
511
512 .. todo::
513
514     Document sample_restraint package. Reference issue
515     `3027 <https://gitlab.com/gromacs/gromacs/-/issues/3027>`_
516
517 .. _gmxapi install troubleshooting:
518
519 Troubleshooting
520 ===============
521
522 AttributeError: module 'enum' has no attribute 'IntFlag'
523 --------------------------------------------------------
524
525 If you had older versions of some of the dependencies installed,
526 you might have picked up a transitive dependency on the ``enum34`` package.
527 Try::
528
529     pip uninstall -y enum34
530
531 and see if that fixes the problem. If not, try a fresh virtual environment
532 (see above) to help narrow down the problem before you
533 `open an issue <https://gitlab.com/gromacs/gromacs/-/issues/>`_.
534
535 Couldn't find the ``gmxapi`` support library?
536 ---------------------------------------------
537
538 If you don't want to "source" your :ref:`GMXRC <getting access to |Gromacs|>` file, you
539 can tell the package where to find a gmxapi compatible GROMACS installation with
540 ``gmxapi_DIR``. E.g. ``gmxapi_DIR=/path/to/gromacs pip install .``
541
542 Before updating the ``gmxapi`` package it is generally a good idea to remove the
543 previous installation and to start with a fresh build directory. You should be
544 able to just ``pip uninstall gmxapi``.
545
546 Do you see something like the following?
547
548 .. code-block:: none
549
550    CMake Error at gmx/core/CMakeLists.txt:45 (find_package):
551       Could not find a package configuration file provided by "gmxapi" with any
552       of the following names:
553
554         gmxapiConfig.cmake
555         gmxapi-config.cmake
556
557       Add the installation prefix of "gmxapi" to CMAKE_PREFIX_PATH or set
558       "gmxapi_DIR" to a directory containing one of the above files.  If "gmxapi"
559       provides a separate development package or SDK, be sure it has been
560       installed.
561
562 This could be because
563
564 * GROMACS is not already installed
565 * GROMACS was built without the CMake variable ``GMXAPI=ON``
566 * or if ``gmxapi_DIR`` (or ``GROMACS_DIR``) is not a path containing directories
567   like ``bin`` and ``share``.
568
569 If you are not a system administrator you are encouraged to install in a Python
570 virtual environment, created with virtualenv or Conda_.
571 Otherwise, you will need to specify the ``--user`` flag to ``pip``.
572
573 Two of the easiest problems to run into are incompatible compilers and
574 incompatible Python. Try to make sure that you use the same C and C++
575 compilers for GROMACS, for the Python package, and for the sample
576 plugin. These compilers should also correspond to the :command:`mpicc` compiler
577 wrapper used to compile mpi4py_. In order to build the Python
578 package, you will need the Python headers or development installation,
579 which might not already be installed on the machine you are using. (If
580 not, then you will get an error about missing :file:`Python.h` at some
581 point.) If you have multiple Python installations (or modules available
582 on an HPC system), you could try one of the other Python installations,
583 or you or a system administrator could install an appropriate Python dev
584 package. Alternatively, you might try installing your own Anaconda or
585 MiniConda in your home directory.
586
587 If an attempted installation fails with CMake errors about missing
588 “gmxapi”, make sure that Gromacs is installed and can be found during
589 installation. For instance,
590
591 ::
592
593     gmxapi_DIR=/Users/eric/gromacs python setup.py install --verbose
594
595 Pip and related Python package management tools can be a little too
596 flexible and ambiguous sometimes. If things get really messed up, try
597 explicitly uninstalling the :py:mod:`gmxapi` module and its dependencies, then do
598 it again and repeat until :command:`pip` can no longer find any version of any
599 of the packages.
600
601 ::
602
603     pip uninstall gmxapi
604     pip uninstall cmake
605     # ...
606
607 Successfully running the test suite is not essential to having a working
608 :py:mod:`gmxapi` package. We are working to make the testing more robust, but
609 right now the test suite is a bit delicate and may not work right, even
610 though you have a successfully built the :py:mod:`gmxapi` package. If you want to
611 troubleshoot, though, the main problems seem to be that automatic
612 installation of required python packages may not work (requiring manual
613 installations, such as with :command:`pip install somepackage`) and ambiguities
614 between python versions. 
615
616 If you are working in a development branch of the repository, note that
617 the upstream branch may be reset to ``master`` after a new release is
618 tagged. In general, but particularly on the ``devel`` branch, when you
619 do a :command:`git pull`, you should use the ``--rebase`` flag.
620
621 If you fetch this repository and then see a git status like this::
622
623     $ git status
624     On branch devel
625     Your branch and 'origin/devel' have diverged,
626     and have 31 and 29 different commits each, respectively.
627
628 then :py:mod:`gmxapi` has probably entered a new development cycle. You can
629 do :command:`git pull --rebase` to update to the latest development branch.
630
631 If you do a :command:`git pull` while in ``devel`` and get a bunch of unexpected
632 merge conflicts, do :command:`git merge --abort; git pull --rebase` and you should
633 be back on track.
634
635 If you are developing code for gmxapi, this should be an indication to
636 rebase your feature branches for the new development cycle.
637
638 .. _cmake: https://pypi.org/project/cmake/
639
640 .. _Conda: https://docs.conda.io/en/latest/
641
642 .. _mpi4py: https://pypi.org/project/mpi4py/
643
644 .. _networkx: https://pypi.org/project/networkx/
645
646 .. _numpy: https://www.numpy.org/
647
648 .. _pip: https://pip.pypa.io/en/stable/
649
650 .. _scikit-build: https://pypi.org/project/scikit-build/
651
652 .. _setuptools: https://pypi.org/project/setuptools/