News

New paper!

Tuesday, December 3, 2024

Conda environment control (with miniconda3)

 Conda environment control (the example is particularly for Linux)

  1. Download miniconda3 installer of your preference e.g.
    mkdir -p ~/miniconda3
    curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o ~/miniconda3/miniconda.sh

  2. Install miniconda3 (as described here https://docs.conda.io/projects/conda/en/stable/user-guide/install/linux.html)
    bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
    rm -rf ~/miniconda3/miniconda.sh

  3. Initialize conda (i.e. define conda commands and set up PATH)
    It might be done by `conda init` or something (I’m not sure) but what needs to be done is add this to ~/.bashrc and/or ~/.bash_profile
    # >>> conda initialize >>>
    # !! Contents within this block are managed by 'conda init' !!
    __conda_setup=“$(‘<path_to_the_miniconda3_you_want_to_use>/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
      eval "$__conda_setup"
    else
      if [ -f "<path_to_the_miniconda3_you_want_to_use>/miniconda3/etc/profile.d/conda.sh" ]; then
        . "<path_to_the_miniconda3_you_want_to_use>/miniconda3/etc/profile.d/conda.sh"
      else
        export PATH="<path_to_the_miniconda3_you_want_to_use>/miniconda3/bin:$PATH"
      fi
    fi
    unset __conda_setup
    # <<< conda initialize <<< 


Note that if PATH contains something else (e.g. <your_home_dir>/.local/bin) before <path_to_the_miniconda3_you_want_to_use>, the thing that comes earlier in the PATH will be prioritized. For example. If <your_home_dir>/.local/bin contains things installed by `pip` etc, they can interfere conda environment. For me, adding the lines below to ~/.bash_profile solved the issue, by making sure that the things in .bashrc are executed upon logging in:
if [ -f ~/.bashrc ]; then

   . ~/.bashrc

fi


        4. Now you should be able to access conda environment that’s stored under <path_to_the_miniconda3_you_want_to_use>/miniconda3/envs/ !!


        5. To create a new conda environment (e.g. containing a specific package like python=3.8):
            conda create -n myenv python=3.8 https://docs.conda.io/projects/conda/en/latest/commands/create.html

        
        6. Activate the conda environment and install packages for example,
                conda activate myenv
                conda install numpy matplotlib pandas