Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulties to install miniconda properly #389

Closed
gandroz opened this issue Sep 14, 2020 · 2 comments
Closed

Difficulties to install miniconda properly #389

gandroz opened this issue Sep 14, 2020 · 2 comments

Comments

@gandroz
Copy link

gandroz commented Sep 14, 2020

I've got this buildspec

version: 0.2

phases:
  install:
    runtime-versions:
      python: 3.8
    commands:
      - export PATH=/opt/conda/bin:$PATH  # should not be mandatory, but needed to call `conda` command
      - wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
      - sh ~/miniconda.sh -b -p /opt/conda
      - rm ~/miniconda.sh
      - ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh
      - echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc
      - echo "conda activate" >> ~/.bashrc



  pre_build:
    commands:
      # Initialize Conda for the current shell
      - conda init bash
      # Restart shell for changes to take effect. Does not seem to work :(
      - exec $SHELL
      - conda create -n myenv python=3.6
      # command `conda activate myenv` does not work, call `activate` the standard way
      - . activate myenv && which python  # returns /opt/conda/bin/python instead of /opt/conda/envs/myenv/bin/python

Conda is well installed but I have trouble to make it work properly. As stated in the doc, conda need to be initialized. However, the shell needs to be restarted afterwards, and the exec $SHELL does not seems to do it as I am not able to activate the virtual environment with conda activate myenv. Although, once activated (or I think it was), the python binary called is the one on the root, not the virtual environment.

@JelsB
Copy link

JelsB commented Dec 17, 2020

I've had a very similar problem. Activating a conda environment in a CI server or Docker image isn't straightforward. I've encountered this issues several times. The documentation of conda does provide an example with using Travis CI, but it didn't work properly when I tried it on AWS CodeBuild

# part of conda docs for Travis CI
- wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
- bash miniconda.sh -b -p $HOME/miniconda
- source "$HOME/miniconda/etc/profile.d/conda.sh"
- hash -r
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
# Useful for debugging any issues with conda
- conda info -a

# Replace dep1 dep2 ... with your dependencies
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION dep1 dep2 ...
- conda activate test-environment
- python setup.py install

For AWS CodeCuild I had to change source "$HOME/miniconda/etc/profile.d/conda.sh" into . "$HOME/miniconda/etc/profile.d/conda.sh" since the source command is not a POSIX standard.
Next, it failed when trying to conda activate test-environment, an error that most people encounter:

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'. To initialize your shell, run

  $ conda init <SHELL_NAME>

Currently supported shells are:
- bash
- fish
- tcsh
- xonsh
- zsh
- powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

And like you tried, the conda init command doesn't really work...
But via this blog, I found a working solution!
You can just use an environment without activating it by running conda run -n my_env python file.py

Translating this to AWS CodeCommit my buildspecs (conda part) look like this:

{
  "version": "0.2",
  "phases": {
    "pre_build": {
      "commands": [
        "wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh -q",
        "bash miniconda.sh -b -p $HOME/miniconda",
        ". \"$HOME/miniconda/etc/profile.d/conda.sh\"",
        "hash -r",
        "conda config --set always_yes yes --set changeps1 no",
        "conda update -q conda",
        "conda info -a",
        "conda env create -q -p ./py_env -f environment.yml"
      ]
    },
    "build": {
      "commands": [
        "conda run -p ./py_env python app.py"
      ]
    }
  }
}

@subinataws
Copy link
Contributor

Thank @JelsB. @gandroz - Marking this resolved, assuming steps listed by @JelsB works for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants