Skip to content

Instantly share code, notes, and snippets.

@NucciTheBoss
Created March 21, 2022 17:44
Show Gist options
  • Save NucciTheBoss/fdf842cca303c9159fb52a9caeb2df65 to your computer and use it in GitHub Desktop.
Save NucciTheBoss/fdf842cca303c9159fb52a9caeb2df65 to your computer and use it in GitHub Desktop.
Move conda environments to a new location on your Linux system.
#!/usr/bin/env bash
echo "Begin environment relocation."
NEW_ENV_DIR=$1
BASE_ENV=$(conda info --base)
#########################################
#
# Install jq locally
#
#########################################
mkdir -p ~/.local/tmp/bin
wget https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64 -O ~/.local/tmp/bin/jq
chmod +x ~/.local/tmp/bin/jq
export PATH=~/.local/tmp/bin:$PATH
#########################################
#
# Relocate conda environments
#
#########################################
mkdir -p $NEW_ENV_DIR
conda config --add envs_dirs $NEW_ENV_DIR
for i in $(conda env list --json | jq '.envs | .[]'); do
i=$(sed -e 's/^"//' -e 's/"$//' <<< $i)
if [ $(basename $BASE_ENV) != $(basename $i) ]; then
conda env export -p $i > tmp.yaml
conda remove -p $i --all -y
conda create -p $NEW_ENV_DIR/$(basename $i) -y
sed -i "1d" tmp.yaml
sed -i "$(wc -l < tmp.yaml)d" tmp.yaml
conda env update -p $NEW_ENV_DIR/$(basename $i) -f tmp.yaml
fi
done
#########################################
#
# Clean up environment
#
#########################################
chmod -R ug+rwx $NEW_ENV_DIR
rm -rf ~/.local/tmp
rm -f tmp.yaml
echo "Environment relocation finished."
@NucciTheBoss
Copy link
Author

Example command:

bash move-conda-envs.sh /path/to/new/envs/stack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment