4

I'm new to singularity and I would like to activate a conda environment (myenv) within a singularity recipe in order to install a package into it (mypkg). I've read some related posts but the answers did not solve my problem although the questions are very similar (Activate conda environment on execution of Singularity container in Nextflow, How to activate an Anaconda environment in a Singularity recipe, Building Singularity recipe from Nipype docker image CommandNotFound). Could you help me to find what I've missed please?

In my case, myenv is automatically activated when I execute the container but I don't succeed to activate it during the container building for installing mypkg. I don't have any error message but the package is installed in the base env instead of myenv.

Bootstrap: docker
From: ubuntu:18.04

%files
    myenv.yml

%environment
    export LC_ALL=C
    export LC_NUMERIC=en_GB.UTF-8
    export PATH="/opt/miniconda/bin:$PATH"
    export PATH="/opt/miniconda/envs/$(head -1 myenv.yml | cut -d' ' -f2)/bin:$PATH"

%runscript
    exec "$@"

%post
    # miniconda2: get miniconda2 version 4.5.1
    wget https://repo.continuum.io/miniconda/Miniconda2-4.7.12.1-Linux-x86_64.sh -O miniconda.sh

    #install conda
    bash miniconda.sh -b -p /opt/miniconda
    export PATH="/opt/miniconda/bin:$PATH"

    #install conda env.yml file
    echo ". /opt/miniconda/etc/profile.d/conda.sh" >> $SINGULARITY_ENVIRONMENT
    echo "conda activate $(head -1 myenv.yml | cut -d' ' -f2)" >> $SINGULARITY_ENVIRONMENT
    /opt/miniconda/bin/conda env create -f myenv.yml

    # Activate conda env, clone and install mypkg
    . /opt/miniconda/bin/activate myenv &&
    git clone https://github.com/mypkg.git &&
    cd mypkg&&
    python setup.py install

RomB
  • 295
  • 3
  • 17

1 Answers1

0

Instead of . /opt/miniconda/bin/activate myenv try using:

. /opt/conda/etc/profile.d/conda.sh
conda activate myenv

Using the functions directly is better supported.

tsnowlan
  • 3,472
  • 10
  • 15