============
Installation
============

**python-rtmidi** uses the de-facto standard Python distutils and setuptools_
based packaging system and can be installed from the Python Package Index via
pip_. Since it is a Python C(++)-extension, a C++ compiler and build
environment as well as some system-dependent libraries are needed to install,
unless wheel packages with precompiled binaries are available for your system.
See the Requirements_ section below for details.


From PyPI
---------

If you have all the requirements_, you should be able to install the package
with pip_::

    $ pip install python-rtmidi

This will download the source distribution from python-rtmidi's `PyPI page`_,
compile the extension and install it in your active Python installation. Unless
you want to change the Cython_ source file ``_rtmidi.pyx``, there is no need to
have Cython installed.

python-rtmidi also works well with virtualenv_ and virtualenvwrapper_. If you
have both installed, creating an isolated environment for testing and/or using
python-rtmidi is as easy as::

    $ mkvirtualenv rtmidi
    (rtmidi)$ pip install python-rtmidi


Pre-compiled Binaries
---------------------

Binary wheels with a pre-compiled version for Windows with Windows MultiMedia
API support are available through PyPI for several Python versions. If you
install python-rtmidi via pip (see above), these wheels should be picked up by
it automatically, if you have compatible Python and Windows versions.


From the Source Distribution
----------------------------

Of course, you can also download the source distribution package as a Zip
archive or tarball, extract it and install using the common ``distutils``
commands, e.g.::

    $ pip download python-rtmidi
    $ unzip python-rtmidi-1.2.1.zip
    $ cd python-rtmidi-1.2.1
    $ python setup.py install


From the Source Code Repository
-------------------------------

Lastly, you can check out the python-rtmidi source code from the Git repository
and then install it from your working copy. Since the repository does not
include the C++ module source code pre-compiled from the Cython source, you'll
also need to install Cython >= 0.28, either via pip or from its Git repository.
Using virtualenv/virtualenvwrapper is strongly recommended in this scenario:

Make a virtual environment::

    $ mkvirtualenv rtmidi
    (rtmidi)$ cdvirtualenv

Install ``Cython`` from PyPI::

    (rtmidi)$ pip install Cython

or the Git repository::

    (rtmidi)$ git clone https://github.com/cython/cython.git
    (rtmidi)$ cd cython
    (rtmidi)$ python setup.py install
    (rtmidi)$ cd ..

Install **python-rtmidi**::

    (rtmidi)$ git clone https://github.com/SpotlightKid/python-rtmidi.git
    (rtmidi)$ cd python-rtmidi
    (rtmidi)$ python setup.py install


.. _requirements:

Requirements
============

Naturally, you'll need a C++ compiler and a build environment. See the
platform-specific hints below.

If you want to change the Cython source file ``_rtmidi.pyx`` or want to
recompile ``_rtmidi.cpp`` with a newer Cython version, you'll need to install
Cython >= 0.28. The ``_rtmidi.cpp`` file in the current source distribution
(version 1.2.1) is tagged with::

    /* Generated by Cython 0.29.2 */

RtMidi (and therefore python-rtmidi) supports several low-level MIDI frameworks
on different operating systems. Only one of the available options needs to be
present on the target system, but support for more than one can be compiled in.
The setup script will try to detect available libraries and should use the
appropriate compilations flags automatically.

    * Linux: ALSA, JACK
    * macOS (OS X): CoreMIDI, JACK
    * Windows: MultiMedia (MM)


Linux
-----

First you need a C++ compiler and the pthread library. Install the
``build-essential`` package on debian-based systems to get these.

Then you'll need Python development headers and libraries. On debian-based
systems, install the ``python-dev`` package. If you use the official installers
from python.org you should already have these.

To get ALSA support, you must install development files for the ``libasound2``
library (debian package: ``libasound2-dev``). For JACK support, install the
``libjack`` development files (``libjack-dev`` or ``libjack-jackd2-dev``).


macOS (OS X)
------------

Install the latest Xcode version or ``g++`` from MacPorts or homebrew
(untested). CoreMIDI support comes with installing Xcode. For JACK support,
install `JackOSX`_ with the installer or build JACK from source.

.. note::
    If you have an old version of OS X and Xcode which still support building
    binaries for PPC, you'll have to tell distribute to build the package only
    for i386 and x86_64 architectures::

        env ARCHFLAGS="-arch i386 -arch x86_64" python setup.py install


Windows
-------

Please see the detailed instructions for Windows in :doc:`install-windows`.


User Contributed Documentation
------------------------------

The python-rtmidi wiki on GitHub contains some `user contributed
documentation`_ for additional installation scenarios. Please check these, if
you have trouble installing python-rtmidi in an uncommon or not-yet-covered
environment.


.. _pypi page: http://python.org/pypi/python-rtmidi#downloads
.. _cython: http://cython.org/
.. _pip: http://python.org/pypi/pip
.. _setuptools: http://python.org/pypi/setuptools
.. _virtualenv: http://pypi.python.org/pypi/virtualenv
.. _virtualenvwrapper: http://www.doughellmann.com/projects/virtualenvwrapper/
.. _jackosx: hhttp://jackaudio.org/downloads/
.. _pyliblo: http://das.nasophon.de/pyliblo/
.. _user contributed documentation:
    https://github.com/SpotlightKid/python-rtmidi/wiki/User-contributed-documentation
