How to create a personal Conda environment on Gadi when you really need one?

Hello,
I have been using a personal Conda environment on Gadi to organize and run IETF-Robose Tractive, Reposurgeon and related software to migrate Subversion repositories and Trac to GitHub. Yesterday, when I tried to reproduce the Conda environment, this resulted in error messages. On investigating, I found the Overview of current shared infrastructure and the Conda - climate-cms wikis.unsw.edu.au page, including its instructions on how to use a hh5 Conda environment to create a personal environment.

Question: Is this the correct, up-to-date page of instructions on how to use a hh5 Conda environment to create a personal environment?

The reason that I ask is that I am following the instructions there and see:

[pcl851@gadi-login-03 ~]$ module use /g/data/hh5/public/modules
[pcl851@gadi-login-03 ~]$ module load conda/analysis3
[pcl851@gadi-login-03 ~]$ conda deactivate
usage: conda [-h] [--no-plugins] [-V] COMMAND ...
conda: error: argument COMMAND: invalid choice: 'deactivate' (choose from 'clean', 'compare', 'config', 'create', 'info', 'init', 'install', 'list', 'notices', 'package', 'remove', 'uninstall', 'rename', 'run', 'search', 'update', 'upgrade', 'build', 'convert', 'debug', 'develop', 'doctor', 'index', 'inspect', 'metapackage', 'render', 'skeleton', 'tree', 'env', 'verify')
[pcl851@gadi-login-03 ~]$ conda env create -n tractive

SpecNotFound: '/home/851/pcl851/environment.yml' is empty

See also How to create conda enviroment on Gadi?

I tried an alternative command:

[pcl851@gadi-login-03 ~]$ conda create -n tractive
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /scratch/tm70/pcl851/conda/envs/tractive



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate tractive
#
# To deactivate an active environment, use
#
#     $ conda deactivate

[pcl851@gadi-login-03 ~]$ conda activate tractive
usage: conda [-h] [--no-plugins] [-V] COMMAND ...
conda: error: argument COMMAND: invalid choice: 'activate' (choose from 'clean', 'compare', 'config', 'create', 'info', 'init', 'install', 'list', 'notices', 'package', 'remove', 'uninstall', 'rename', 'run', 'search', 'update', 'upgrade', 'build', 'convert', 'debug', 'develop', 'doctor', 'index', 'inspect', 'metapackage', 'render', 'skeleton', 'verify', 'env', 'tree')
[pcl851@gadi-login-03 ~]$ conda env list
# conda environments:
#
analysis27-18.10         /g/data/hh5/public/apps/miniconda3/envs/analysis27-18.10
analysis3                /g/data/hh5/public/apps/miniconda3/envs/analysis3
analysis3-22.01          /g/data/hh5/public/apps/miniconda3/envs/analysis3-22.01
analysis3-22.04          /g/data/hh5/public/apps/miniconda3/envs/analysis3-22.04
analysis3-22.07          /g/data/hh5/public/apps/miniconda3/envs/analysis3-22.07
analysis3-22.10          /g/data/hh5/public/apps/miniconda3/envs/analysis3-22.10
analysis3-23.01          /g/data/hh5/public/apps/miniconda3/envs/analysis3-23.01
analysis3-23.04          /g/data/hh5/public/apps/miniconda3/envs/analysis3-23.04
base                  *  /g/data/hh5/public/apps/miniconda3/envs/analysis3-23.07
analysis3-23.10          /g/data/hh5/public/apps/miniconda3/envs/analysis3-23.10
analysis3-unstable       /g/data/hh5/public/apps/miniconda3/envs/analysis3-unstable
cmip6-publication        /g/data/hh5/public/apps/miniconda3/envs/cmip6-publication
cws-radar                /g/data/hh5/public/apps/miniconda3/envs/cws-radar
esmvaltool-21.08         /g/data/hh5/public/apps/miniconda3/envs/esmvaltool-21.08
gdal27                   /g/data/hh5/public/apps/miniconda3/envs/gdal27
ml_tools                 /g/data/hh5/public/apps/miniconda3/envs/ml_tools
pyodc                    /g/data/hh5/public/apps/miniconda3/envs/pyodc
python27                 /g/data/hh5/public/apps/miniconda3/envs/python27
python3                  /g/data/hh5/public/apps/miniconda3/envs/python3
pytorch-gpu6             /g/data/hh5/public/apps/miniconda3/envs/pytorch-gpu6
tractive                 /scratch/tm70/pcl851/conda/envs/tractive

So it looks like conda create works but conda activate and conda deactivate do not.

When you module load conda/analysis3 it does not activate the environment in conda, so you can’t deactivate it. However all the packages etc in conda/analysis3 are available to use somehow (because conda “base” environment points to them?).

If I login and run module use ..., then module load ..., thenconda deactivate, the deactivate doesn’t do anything but also doesn’t give an error. I think this is because I have run conda init at some point which means a conda shell script is run when I login.

You will be able to activate your env tractive after creating it. (This will show up in the command prompt like this:
(tractive) [as2285@gadi-login-04 ~]$

(Edit: Nevermind, thanks @anton, didn’t notice you had replied too, yes it looks like I have run conda init which is why it works for me)

Hi Paul, I don’t know much about these envs, but I’m just trying to duplicate the issue and it might be something specific to your environment or shell - I tried the same steps and conda deactivate and activate work with no errors.

What output do you get from which conda and which __conda_activate ?

1 Like

To expand… I tried removnig my .bashrc, logged out and logged in again and was able to duplicate your issue.

I was then able to fix it by running:

module use /g/data/hh5/public/modules
module load conda/analysis3
conda init

This adds to .bashrc, so can then log out and in again, which now activates the default (base) environment, and makes conda activate and deactivate commands available, then conda activate envname will work

Not sure if this is the best approach as it sets up a fixed version of the analysis3 conda environment as the default conda. Perhaps others can weigh in if there is a better way to have conda available on login.

The second command you tried (conda create) is the correct one for your use case.

conda env create is a different thing that reads the packages to install from an environment.yaml file, it’s failed in the first example because you didn’t supply that file.

As Owen says, conda init will set up your bashrc file so that conda activate will work in future sessions. Alternatively you can source /g/data/hh5/public/apps/miniconda3/etc/profile.d/conda.sh which will do the same thing for your current session.

The best way currently for creating conda environments is to do them as singularity containers, since this avoids having an enormous numbers of inodes on the filesystem. I don’t think this is well documented anywhere, examples are Dale’s updated hh5 conda environments and my template at GitHub - ScottWales/singularity-conda-template

1 Like

Thanks Scott. I tried the same thing with the environment that I installed:

[pcl851@gadi-login-06 ~]$ source /scratch/tm70/pcl851/conda/envs/tractive/etc/profile.d/conda.sh
[pcl851@gadi-login-06 ~]$ conda activate

CondaError: Run 'conda init' before 'conda activate'

[pcl851@gadi-login-06 ~]$ conda info

     active environment : None
            shell level : 0
       user config file : /home/851/pcl851/.condarc
 populated config files : 
          conda version : 23.11.0
    conda-build version : not installed
         python version : 3.11.7.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=cascadelake
                          __conda=23.11.0=0
                          __glibc=2.28=0
                          __linux=4.18.0=0
                          __unix=0=0
       base environment : /scratch/tm70/pcl851/conda/envs/tractive  (writable)
      conda av data dir : /scratch/tm70/pcl851/conda/envs/tractive/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /scratch/tm70/pcl851/conda/envs/tractive/pkgs
                          /home/851/pcl851/.conda/pkgs
       envs directories : /scratch/tm70/pcl851/conda/envs/tractive/envs
                          /home/851/pcl851/.conda/envs
               platform : linux-64
             user-agent : conda/23.11.0 requests/2.31.0 CPython/3.11.7 Linux/4.18.0-477.27.1.el8.nci.x86_64 rocky/8.8 glibc/2.28 solver/libmamba conda-libmamba-solver/23.12.0 libmambapy/1.5.6
                UID:GID : 1329:8641
             netrc file : None
           offline mode : False

Something in the installed environment is broken such that conda activate does not work.

I then tried:

[pcl851@gadi-login-06 ~]$ conda init
no change     /scratch/tm70/pcl851/conda/envs/tractive/condabin/conda
no change     /scratch/tm70/pcl851/conda/envs/tractive/bin/conda
no change     /scratch/tm70/pcl851/conda/envs/tractive/bin/conda-env
no change     /scratch/tm70/pcl851/conda/envs/tractive/bin/activate
no change     /scratch/tm70/pcl851/conda/envs/tractive/bin/deactivate
no change     /scratch/tm70/pcl851/conda/envs/tractive/etc/profile.d/conda.sh
no change     /scratch/tm70/pcl851/conda/envs/tractive/etc/fish/conf.d/conda.fish
no change     /scratch/tm70/pcl851/conda/envs/tractive/shell/condabin/Conda.psm1
no change     /scratch/tm70/pcl851/conda/envs/tractive/shell/condabin/conda-hook.ps1
no change     /scratch/tm70/pcl851/conda/envs/tractive/lib/python3.11/site-packages/xontrib/conda.xsh
no change     /scratch/tm70/pcl851/conda/envs/tractive/etc/profile.d/conda.csh
modified      /home/851/pcl851/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

This added the following to my ~/.bashrc:

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/scratch/tm70/pcl851/conda/envs/tractive/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/scratch/tm70/pcl851/conda/envs/tractive/etc/profile.d/conda.sh" ]; then
        . "/scratch/tm70/pcl851/conda/envs/tractive/etc/profile.d/conda.sh"
    else
        export PATH="/scratch/tm70/pcl851/conda/envs/tractive/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Logging out and back in again results in:

###############################################################################
#                  Welcome to the NCI National Facility!                      #
[...]
CondaError: Run 'conda init' before 'conda activate'

Also:

[pcl851@gadi-login-08 ~]$ conda doctor
Environment Health Report for: /scratch/tm70/pcl851/conda/envs/tractive

Altered Files:

python-3.11.7-h955ad1f_0: 1

Environment listed in environments.txt file: ✅
Missing Files:

✅ There are no packages with missing files.

Make sure that conda is installed in your tractive environment (as that’s the path being sourced in bashrc, probably better to use your root conda environment).

conda list -n tractive conda
$ conda list -n tractive conda
# packages in environment at /g/data/hh5/public/apps/miniconda3:
#
# Name                    Version                   Build  Channel
conda                     4.13.0           py39h06a4308_0

Check that variables are being set properly

env | grep CONDA
$ env | grep CONDA
CONDA_SHLVL=0
CONDA_EXE=/g/data/hh5/public/apps/miniconda3/bin/conda
CONDA_PYTHON_EXE=/g/data/hh5/public/apps/miniconda3/bin/python
_CE_CONDA=

Check that which conda is returning a function

which conda
$ which conda
conda ()
{
    \local cmd="${1-__missing__}";
    case "$cmd" in
        activate | deactivate)
            __conda_activate "$@"
        ;;
        install | update | upgrade | remove | uninstall)
            __conda_exe "$@" || \return;
            __conda_reactivate
        ;;
        *)
            __conda_exe "$@"
        ;;
    esac
}

Thanks again. I have also consulted conda init and conda activate — conda 23.11.1.dev54 documentation to see what conda init and conda activate are supposed to do in detail.

Slightly different but if you want to avoid conda (which is apparently the recommendation on Gadi because of file duplication I think?) you can achieve a nice workflow with venv and pip. It’s more lightweight apparently, and works very well. Below are instructions for getting it working with ARE:

  1. (all in terminal from ARE) -module load python3/3.11.0 (or whatever version)
  2. cd to the place you want your venv
  3. python3 -m venv
  4. source /bin/activate
  5. pip install jupyterlab
  6. go to ARE instance launcher
  7. Make sure Modules has python3/3.11.0 (same version as above)
  8. Python or Conda virtual environment base to the root directory of your new venv (not the bin directory)
  9. ARE should then be able to connect with a new python lab machine
2 Likes

I have devised a solution. See Make Trac-to-Github-migration scripts work with Conda on Gadi · Issue #20 · ACCESS-NRI/dev-docs · GitHub for my diagnosis of the problem, and Make Trac-to-Github-migration software installation scripts work with Conda on Gadi by penguian · Pull Request #21 · ACCESS-NRI/dev-docs · GitHub for the corresponding Pull Request.