Skip to content

Instantly share code, notes, and snippets.

@arbal
Forked from NucciTheBoss/move-conda-envs.sh
Created April 8, 2025 17:46
Show Gist options
  • Save arbal/d37b66f8afc9c0c021be017c924271a2 to your computer and use it in GitHub Desktop.
Save arbal/d37b66f8afc9c0c021be017c924271a2 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."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment