Installation#

Installation via conda#

Conda is a software package management system. Once the conda system is set-up (see miniforge), the installation of phonopy is super easy for any of Linux, MacOSX, and Windows. Phonopy is installed using conda by

% conda install -c conda-forge phonopy

Minimum steps to install and use phonopy via conda#

In the following procedure, conda’s environment (see miniforge) is used not to interfere existing environment (mainly python environment).

% conda create -n phonopy -c conda-forge
% conda activate phonopy
% conda install -c conda-forge phonopy

To exit from this conda’s environment:

% conda deactivate

To use this phonopy, entering this environment is necessary like below.

% conda activate phonopy
(phonopy) % phonopy
        _
  _ __ | |__   ___  _ __   ___   _ __  _   _
 | '_ \| '_ \ / _ \| '_ \ / _ \ | '_ \| | | |
 | |_) | | | | (_) | | | | (_) || |_) | |_| |
 | .__/|_| |_|\___/|_| |_|\___(_) .__/ \__, |
 |_|                            |_|    |___/
                                       4.1.0

Rust backend (phonors) using rayon (10 threads).
Running in phonopy.load mode.
Python version 3.13.3
Spglib version 2.7.0


Switched on phonopy-yaml mode.
"phonopy_disp.yaml" or "phonopy.yaml" could not be found.
  ___ _ __ _ __ ___  _ __
 / _ \ '__| '__/ _ \| '__|
|  __/ |  | | | (_) | |
 \___|_|  |_|  \___/|_|

Using HDF5 on NFS mounted file system#

Recent hdf5 versions just as installed may not work on NFS mounted file systems. In this case, setting the following environment variable may solve the problem:

export HDF5_USE_FILE_LOCKING=FALSE

Installation from source code#

System requirement#

The procedure to setup phonopy is explained in this section. It is supposed that phonopy is installed on the recent linux distribution like Ubuntu or Fedora with Python version 3.11 or later. Mac OS X users may use conda (conda-forge channel) packages. Windows users should use conda (conda-forge channel) packages as well.

Prepare the following Python libraries:

  • Python (>=3.11) and its header files

  • numpy (>=1.17)

  • matplotlib (>=2.2.2)

  • python-yaml (pyyaml>=5.3)

  • python-h5py (h5py>=3.0)

  • scipy

  • spglib (>=2.5)

  • symfc (>=1.8)

  • phonors (Rust backend; installed automatically by pip install phonopy)

It is recommended to install seekpath to plot phonon band structure:

  • seekpath

For the CP2K interface, the following package will be needed to install:

  • cp2k-input-tools

Installing required packages by conda#

The python libraries can be installed using conda. Conda packages are distributed in binary. Minimum setup of conda envrironment is done by miniforge, which is downloaded at conda-forge/miniforge. It is strongly recommended to create conda’s virtual environment by conda create -n <venvname> as written above. The installation of necessary libraries is done as follows:

% conda install -c conda-forge numpy scipy h5py pyyaml matplotlib-base seekpath symfc spglib phonors

Here phonors is the Rust backend, installed as a pre-built binary from conda; usually this is all that is needed. Only when a development version of phonors is required is it necessary to build phonors from source; see Rust backend.

Building using pip install#

phonopy can be installed from the source code with pip install, for example to use the development version on GitHub. phonopy is a pure Python package, and it is built with setuptools. No C/C++ compiler is needed.

  1. Get the source code from github

    % git clone https://github.com/phonopy/phonopy.git
    % cd phonopy
    
  2. Run pip install

    % pip install . -vvv
    

    For development, the editable install (pip install -e . -vvv) can be used instead.

The computationally heavy parts of phonopy run in phonors, the Rust backend. phonors is a required dependency, and pip install installs it automatically. pip install does not build the C extension of phonopy. The C extension is needed only to use the legacy backend (lang="C" in the Python API or --legacy-backend on the command line). How to build it is described in Building the C extension. See Rust backend for the backends.

Building the C extension#

The C extension of phonopy consists of the modules phonopy._phonopy and phonopy._recgrid. It is the legacy backend, and phonopy uses it only when lang="C" or --legacy-backend is given. Without the C extension, these options fall back to the Rust backend, and phonopy prints a message once.

The C extension is built by hand with CMake. phonopy’s tests on GitHub Actions build it with the same commands.

  1. Install a C/C++ compiler, CMake (3.20 or later) and nanobind (earlier than 2.10.0). With conda, they are installed by

    % conda install -c conda-forge c-compiler cxx-compiler cmake "nanobind<2.10"
    
  2. Get the source code from github, and install phonopy as an editable install:

    % git clone https://github.com/phonopy/phonopy.git
    % cd phonopy
    % pip install -e . -vvv
    

    The next step places the C extension in the phonopy directory of the source code. An editable install loads phonopy from this directory, so the C extension is found there. A normal install (pip install .) copies the files of phonopy to site-packages when it is run. A C extension placed in the source code after that is not found.

  3. Build the C extension, and place it in the phonopy directory:

    % cmake -S . -B build/cmake -DPython_EXECUTABLE=$(which python)
    % cmake --build build/cmake
    % cmake --install build/cmake --prefix .
    

    The first command configures the build in build/cmake, and the second command compiles the C extension there. The third command copies the two modules to the phonopy directory, for example as phonopy/_phonopy.abi3.so and phonopy/_recgrid.abi3.so on Linux and macOS.

    OpenMP is used when CMake finds it. To build the C extension without OpenMP, add -DPHONOPY_USE_OMP=OFF to the first command.

  4. Check that the C extension is loaded:

    % python -c "import phonopy._phonopy, phonopy._recgrid"
    

    This command prints nothing when the C extension is found. It prints ModuleNotFoundError when the C extension is not found.

When the C source code in c/ is changed, for example by git pull, run step 3 again.

Trouble shooting#

Remove previous phonopy installations#

Sometimes previous installations of phonopy prevent from loading newly installed phonopy. In this case, it is recommended to uninstall all the older phonopy packages by

  1. Running pip uninstall phonopy as many times as no phonopy packages will be found. Error message may be shown, but don’t mind it. Similarly do conda uninstall phonopy.

  2. There may still exist litter of phonopy packages. So it is also recommend to remove them if it is found, e.g.:

    % rm -fr ~/.local/lib/python*/site-packages/phonopy*
    

Set correct environment variables PATH and PYTHONPATH

When using conda environment, this information is not applicable#

In phonopy, PATH and PYTHONPATH play important roles. Of course the information about them can be easily found in internet (e.g. https://en.wikipedia.org/wiki/PATH_(variable)), so you really have to find information by yourself and read them. Even if you can’t understand them, first you must ask to your colleagues or people before sending this unnecessary question (as a researcher using computer simulation) to the mailing list.

The problem appears when phonopy execution and library paths are set multiple times in those environment variable. It is easy to check current environment variables by:

% echo $PATH
% echo $PYTHONPATH

When multiple different phonopy paths are found, remove all except for what you really need. Then logout from the current shell (terminal) and open new shell (terminal) to confirm that the modification is activated.

Missing Intel libraries when building from source using icc#

LDSHARED="icc -shared" may be of help. See this github issues, phonopy/phonopy#123.

Missing or unknown CXX compiler#

When the C extension is built (Building the C extension), CMake errors may occur related to the CXX compiler.

This can typically be resolved by ensuring your C++ compiler is updated, and that the CXX environment variable (https://cmake.org/cmake/help/latest/envvar/CXX.html) is set.

See this GitHub issue: phonopy/phonopy#439.