Search code examples
condasingularity-container

Activate conda environment on execution of Singularity container in Nextflow


I am using Singularity containers to run commands from Nextflow workflow management system. I have a conda environment inside Singularity which I can activate when I shell into the container

singularity pull shub://brucemoran/Singularity:pcgr.centos7
singularity shell brucemoran-Singularity-pcgr.centos7.img
#<inside container>
source activate pcgr

When Nextflow executes I have defined to source activate pcgr which I think should activate the conda env. But I get an unbound variable HOST warning. I think that relates to the non-activation and subsequent use of variables that should be defined if the env was activated(?).

I would like the container to activate the env (pcgr) whenever it is executed. I tried with

%run
 source activate pcgr 

and

%post
 source activate pcgr

but this doesn't work for me

singularity exec pcgr.img which pcgr.py
which: no pcgr.py in ...

I cannot see how this is done, but presume it is easy and I am massively overlooking something!

Help appreciated.


Solution

  • The shell in singularity runs in a special environment, so the standard conda modifications to the .bashrc do not work. Instead, you need to modify the $SINGULARITY_ENVIRONMENT variable. Something along these lines in your Singularity definition file should work:

    # set to whatever your conda path is, I usually install to /opt
    echo ". /opt/conda/etc/profile.d/conda.sh" >> $SINGULARITY_ENVIRONMENT
    echo "conda activate pcgr" >> $SINGULARITY_ENVIRONMENT
    

    This way the conda environment will automatically be activated. If you prefer to activate it manually in your steps, you can leave out the second line and do so in your %run steps.

    EDIT: changed to using . instead of source for compatability with with /bin/sh, mentioned in comments below