Fix some minor typesetting issues.
[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 version of gmxapi from the Python Packaging Index::
365
366     pip install gmxapi
367
368 If :command:`pip` does not find your GROMACS installation, use one of the following
369 environment variables to provide a hint.
370
371 gmxapi_DIR
372 ~~~~~~~~~~
373
374 If you have a single GROMACS installation at :file:`/path/to/gromacs`, it is usually
375 sufficient to provide this location to :command:`pip` through the :envvar:`gmxapi_DIR`
376 environment variable.
377
378 Example::
379
380     gmxapi_DIR=/path/to/gromacs pip install gmxapi
381
382 GMXTOOLCHAINDIR
383 ~~~~~~~~~~~~~~~
384
385 If you have multiple builds of GROMACS distinguished by suffixes
386 (e.g. *_d*, *_mpi*, etcetera), or if you need to provide extra hints to :command:`pip`
387 about the software tools that were used to build GROMACS, you can specify a
388 directory in which the installer can find a CMake "tool chain".
389
390 In the following example, ``${SUFFIX}`` is the suffix that distinguishes the
391 particular build of GROMACS you want to target (refer to GROMACS installation
392 instructions for more information.) ``${SUFFIX}`` may simply be empty, or ``''``.
393
394 ::
395
396     GMXTOOLCHAINDIR=/path/to/gromacs/share/cmake/gromacs${SUFFIX} pip install gmxapi
397
398 Install from source
399 -------------------
400
401 You can also install the :py:mod:`gmxapi` Python package from within a local copy of
402 the GROMACS source repository. Assuming you have already obtained the GROMACS
403 source code and you are in the root directory of the source tree, you will find
404 the :py:mod:`gmxapi` Python package sources in the :file:`python_packaging/src` directory.
405
406 ::
407
408     cd python_packaging/src
409     pip install -r requirements.txt
410     pip install .
411
412 .. _gmxapi offline install:
413
414 Offline install
415 ---------------
416
417 If the required dependencies are already installed, you can do a quick installation
418 without internet access, either from the source directory or from a source archive.
419
420 For example, the last line of the previous example could be replaced with::
421
422     pip install --no-cache-dir --no-deps --no-index --no-build-isolation .
423
424 Refer to pip_ documentation for descriptions of these options.
425
426 If you have built or downloaded a source distribution archive, you can provide
427 the archive file to :command:`pip` instead of the ``.`` argument::
428
429     pip install gmxapi-0.1.0.tar.gz
430
431 In this example, the archive file name is as was downloaded from
432 `PyPI <https://pypi.org/project/gmxapi/#history>`_ or as built locally,
433 according to the following instructions.
434
435 Building a source archive
436 -------------------------
437
438 A source archive for the gmxapi python package can be built from the GROMACS
439 source repository using Python ``setuptools`` and ``scikit-build``.
440
441 Example::
442
443     pip install --upgrade setuptools scikit-build
444     cd python_packaging/src
445     python setup.py sdist
446
447 This command will create a ``dist`` directory containing a source distribution
448 archive file. The file name has the form *gmxapi-<version>.<suffix>*, where
449 *<version>* is the version from the ``setup.py`` file, and *<suffix>* is
450 determined by the local environment or by additional arguments to ``setup.py``.
451
452 .. seealso::
453
454     Python documentation for
455     `creating a source distribution
456     <https://docs.python.org/3/distutils/sourcedist.html#creating-a-source-distribution>`_
457
458 Package maintainers may update the online respository by uploading a freshly
459 built ``sdist`` with ``python -m twine upload dist/*``
460
461 .. _gmxapi_package_documentation:
462
463 Accessing gmxapi documentation
464 ==============================
465
466 Documentation for the Python classes and functions in the gmx module can
467 be accessed in the usual ways, using ``pydoc`` from the command line or
468 ``help()`` in an interactive Python session.
469
470 The complete documentation (which you are currently reading)
471 can be browsed `online <http://manual.gromacs.org/current/gmxapi/>`__
472 or built from a copy of the GROMACS source repository.
473
474 Documentation is built from a combination of Python module documentation and
475 static content, and requires a local copy of the GROMACS source repository.
476
477 Build with GROMACS
478 ------------------
479
480 To build the full gmxapi documentation with GROMACS, configure GROMACS with
481 ``-DGMX_PYTHON_PACKAGE=ON`` and build the GROMACS documentation normally.
482 This will first build the *gmxapi* Python package and install it to a temporary
483 location in the build tree. Sphinx can then import the package to automatically
484 extract Python docstrings.
485
486 Sometimes the build environment can choose a different Python interpreter than
487 the one you intended.
488 You can set the ``Python3_ROOT`` or ``CMAKE_PREFIX_PATH`` CMake variable to
489 explicitly choose the Python installation or *venv* directory.
490
491 If you use pyenv or pyenv-virtualenv to dynamically manage your Python version,
492 you can help identify a particular version with ``pyenv version-name`` and the
493 directory with ``pyenv prefix {version}``. For example::
494
495     -DPython3_ROOT=$(pyenv prefix $(pyenv version-name))
496
497 Docker web server
498 -----------------
499
500 Alternatively, build the ``docs`` Docker image from ``python_packaging/docker/docs.dockerfile``
501 or pull a prebuilt image from DockerHub. Refer to the dockerfile or to
502 https://hub.docker.com/r/gmxapi/docs for more information.
503
504 .. todo::
505
506     Document sample_restraint package. Reference issue
507     `3027 <https://gitlab.com/gromacs/gromacs/-/issues/3027>`_
508
509 .. _gmxapi install troubleshooting:
510
511 Troubleshooting
512 ===============
513
514 Couldn't find the ``gmxapi`` support library?
515 If you don't want to "source" your :ref:`GMXRC <getting access to |Gromacs|>` file, you
516 can tell the package where to find a gmxapi compatible GROMACS installation with
517 ``gmxapi_DIR``. E.g. ``gmxapi_DIR=/path/to/gromacs pip install .``
518
519 Before updating the ``gmxapi`` package it is generally a good idea to remove the
520 previous installation and to start with a fresh build directory. You should be
521 able to just ``pip uninstall gmxapi``.
522
523 Do you see something like the following?
524
525 .. code-block:: none
526
527    CMake Error at gmx/core/CMakeLists.txt:45 (find_package):
528       Could not find a package configuration file provided by "gmxapi" with any
529       of the following names:
530
531         gmxapiConfig.cmake
532         gmxapi-config.cmake
533
534       Add the installation prefix of "gmxapi" to CMAKE_PREFIX_PATH or set
535       "gmxapi_DIR" to a directory containing one of the above files.  If "gmxapi"
536       provides a separate development package or SDK, be sure it has been
537       installed.
538
539 This could be because
540
541 * GROMACS is not already installed
542 * GROMACS was built without the CMake variable ``GMXAPI=ON``
543 * or if ``gmxapi_DIR`` (or ``GROMACS_DIR``) is not a path containing directories
544   like ``bin`` and ``share``.
545
546 If you are not a system administrator you are encouraged to install in a Python
547 virtual environment, created with virtualenv or Conda_.
548 Otherwise, you will need to specify the ``--user`` flag to ``pip``.
549
550 Two of the easiest problems to run into are incompatible compilers and
551 incompatible Python. Try to make sure that you use the same C and C++
552 compilers for GROMACS, for the Python package, and for the sample
553 plugin. These compilers should also correspond to the :command:`mpicc` compiler
554 wrapper used to compile mpi4py_. In order to build the Python
555 package, you will need the Python headers or development installation,
556 which might not already be installed on the machine you are using. (If
557 not, then you will get an error about missing :file:`Python.h` at some
558 point.) If you have multiple Python installations (or modules available
559 on an HPC system), you could try one of the other Python installations,
560 or you or a system administrator could install an appropriate Python dev
561 package. Alternatively, you might try installing your own Anaconda or
562 MiniConda in your home directory.
563
564 If an attempted installation fails with CMake errors about missing
565 “gmxapi”, make sure that Gromacs is installed and can be found during
566 installation. For instance,
567
568 ::
569
570     gmxapi_DIR=/Users/eric/gromacs python setup.py install --verbose
571
572 Pip and related Python package management tools can be a little too
573 flexible and ambiguous sometimes. If things get really messed up, try
574 explicitly uninstalling the :py:mod:`gmxapi` module and its dependencies, then do
575 it again and repeat until :command:`pip` can no longer find any version of any
576 of the packages.
577
578 ::
579
580     pip uninstall gmxapi
581     pip uninstall cmake
582     # ...
583
584 Successfully running the test suite is not essential to having a working
585 :py:mod:`gmxapi` package. We are working to make the testing more robust, but
586 right now the test suite is a bit delicate and may not work right, even
587 though you have a successfully built the :py:mod:`gmxapi` package. If you want to
588 troubleshoot, though, the main problems seem to be that automatic
589 installation of required python packages may not work (requiring manual
590 installations, such as with :command:`pip install somepackage`) and ambiguities
591 between python versions. 
592
593 If you are working in a development branch of the repository, note that
594 the upstream branch may be reset to ``master`` after a new release is
595 tagged. In general, but particularly on the ``devel`` branch, when you
596 do a :command:`git pull`, you should use the ``--rebase`` flag.
597
598 If you fetch this repository and then see a git status like this::
599
600     $ git status
601     On branch devel
602     Your branch and 'origin/devel' have diverged,
603     and have 31 and 29 different commits each, respectively.
604
605 then :py:mod:`gmxapi` has probably entered a new development cycle. You can
606 do :command:`git pull --rebase` to update to the latest development branch.
607
608 If you do a :command:`git pull` while in ``devel`` and get a bunch of unexpected
609 merge conflicts, do :command:`git merge --abort; git pull --rebase` and you should
610 be back on track.
611
612 If you are developing code for gmxapi, this should be an indication to
613 rebase your feature branches for the new development cycle.
614
615 .. _cmake: https://pypi.org/project/cmake/
616
617 .. _Conda: https://docs.conda.io/en/latest/
618
619 .. _mpi4py: https://pypi.org/project/mpi4py/
620
621 .. _networkx: https://pypi.org/project/networkx/
622
623 .. _numpy: https://www.numpy.org/
624
625 .. _pip: https://pip.pypa.io/en/stable/
626
627 .. _scikit-build: https://pypi.org/project/scikit-build/
628
629 .. _setuptools: https://pypi.org/project/setuptools/