Block package installations to conda base environment

Question:

I’m currently using miniconda and I want to prevent myself and other users of my machine from installing anything into the base environment. This is because I want users to be creating virtual environments and installing stuff there. I also don’t want my base environment to get bloated.

Is there anyway to do this? I use both conda and pip so I imagine I need to somehow block both of those.

Asked By: Johnny Metz

||

Answers:

One option would be to change the write permissions on the directories pip and conda install packages to for the base environments. These locations vary based on your distribution, but you can check by using something like python -c "import setuptools; print(setuptools.__file__)". The parent directory to setuputils will be where the packages get installed by default. Run chmod -w <packages dir> to remove write permissions. You can always add them back with chmod +w <packages dir> later, but while they’re disabled this should keep you from installing packages there by accident. Unless you haphazardly install packages with sudo, that is…

Answered By: augray

You can add this in your .bashrc or .zshrc

function pip(){
  if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
    echo "Not allowed in base"
  else
    command pip "$@"
  fi
}

function extended_conda(){
  if [ "${CONDA_PROMPT_MODIFIER-}" = "(base) " ] && [ "$1" = "install" ]; then
    echo "Not allowed in base"
  else
    conda "$@"
  fi
}
alias conda=extended_conda

It will deny the install commands if you are in the base environment.

Answered By: Gavinkaa

I am not sure how to lock the environment but suitable solution could be to restore previous environment revision once you detected it was (accidentally) changed.
Anytime you can list the environment revisions

conda list --revisions

You will get a list of revisions with their revision number. To revert to particular revision, run:

conda install --revision N

where N is the revision number. In addition, I believe this simple procedure can be automated so you can effectively develop a lock to specific revision. See conda documentation for details.

Answered By: Karel Marik
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.