Skip to main content
Chemistry LibreTexts

5.2.7: Installing custom packages

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The LibreTexts platform aims to be accommodating by already providing many commonly-used libraries and packages for use in code blocks. Unfortunately, authors may still find themselves in a position where they need a package that is not installed by default. If such a package is available in the Ubuntu 18.04 APT repositories or conda's conda-forge channel, the LibreTexts team can have it added for all code blocks upon request.

    Installing a package that isn't available at either of the above locations is technically possible, though it is an unsupported feature.

    General Instructions

    For best results, any commands to install packages should be in the first code block of a page. To run a command in a Python, Octave, SageMath, or C++ code block, simply prepend the line with an exclamation mark. Examples of this in Python are shown below. Documentation on why this works in Python can be found here. Running commands in R code blocks can be accomplished through the use of system(). Octave also has a similar system() function. In Julia, this can be done through run().

    One thing to keep in mind is that installing custom packages can increase the time it takes to run code by a significant amount. This may result in the code cell appearing to be frozen when it is in the process of downloading and installing packages. In such a case, it may be a good idea to include a print statement before any package installation commands letting anyone running the code cell that packages are currently being installed. Something along the lines of "Loading packages. This process may take up to x minutes. Please wait." will likely suffice.

    Examples of Installing Custom Packages

    Installing a conda package from the conda-forge channel

    If an author wants to use a package that is available in the conda-forge channel, but not already installed on LibreTexts, they can do so as follows. To use the package pycrypto as an example:

    !conda install -y pycrypto

    The argument y is used here to automatically say yes to the package installation.

    Installing other conda packages

    If a conda package is desired that is not available in the conda-forge channel, the channel will have to specifically be specified. For the purposes of this example, assume that pycrypto is not available in conda-forge, but actually only from the "anaconda" channel. The command would then be as follows:

    !conda install -y -c anaconda pycrypto

    Notice the inclusion of the flag -c to specify the channel from which the package comes.

    Using mamba instead of conda

    An alternative to using the command conda for conda packages would be to use mamba. Mamba is a more recent and generally faster manager for conda packages. Syntax is identical between either command except for the replacement of conda for mamba. For example:

    !mamba install -y pycrypto

    One thing to keep in mind when using mamba is that it may output an alarming amount of text indicating the package is being installed. To prevent this from happening, authors may include the -q argument.

    Installing a pip package

    Following the previous examples, we simply include the pip install command in the first code block on the page. If an author wanted to use the times2 package, they could insert this line into their code:

    !pip install times2

    Real-time example

    All of this working in action:

    print("Installing dependencies. This process may take up to 5 minutes.")
    !conda install -y pycrypto
    !pip install times2
    import times2
    from Crypto import Random
    print("pycrypto output:")
    rndfile =
    print("times2 output:")


    5.2.7: Installing custom packages is shared under a CC BY 1.3 license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?