Ephemeral conda environments

I bet you’re also tired of cloning repos online that use conda as package manager. Nowadays, the community is shifting towards uv.
Sometimes you just want to try a piece of software and installing conda feels like overkill.
The goal of this page is to teach you to create conda environments on demand, that can be deleted without residue. No shell mutation. No base auto-activation. No fixed installation of conda needed. You download conda, create the environment you need, after use, when you turn off your machine, it will disappear.
The Idea
I break down the idea in steps:
- First we will download conda and put it into
/tmp(remember we don’t want it to persist) - We will install it as usual without always pressing
ato accept the ToS… - We will load
condafunctions in our current shell - We will create our environment
- We will activate our environment
The Code
Download the repo which you are interested in, make sure there is an environment.yaml. cd inside it and run this one-liner in your shell.
wget -qO /tmp/miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
bash /tmp/miniconda.sh -b -p /tmp/miniconda && \
source /tmp/miniconda/etc/profile.d/conda.sh && \
conda env create -f environment.yaml && \
conda activate "$(sed -n 's/^name:[[:space:]]*//p' environment.yaml)"The Explanation
wget ...downloads the Miniconda installer and saves it to/tmp.bash ... -b -p /tmp/miniconda.shinstalls Miniconda in batch mode to a fixed prefix (read the Note*)
-bdisables all interactive prompts and license confirmation.-pforces installation into the specified directory and prevents shell modification.
source .../conda.shloadscondafunctions into the current shell without activatingbase.conda env create -f environment.yamlmaterializes the environment declared in YAML.conda activate ...activates the environment explicitly by looking procedurally inside yourenvironment.yamlthroughsed(which looks for the correct env name).
At no point is the system shell altered. conda remains inert unless sourced. Now you’re ready to use your evironment as you want and whenever you turn off your machine Miniconda will disappear.
if during installation the script prompts you for accepting the ToS you can put yes a | before the installation command
Deep Dive: sed
In case you’re wondering: the command used for dynamic environment activation is:
sed -n 's/^name:[[:space:]]*//p' environment.yamlExplanation
sedliterally stands for streaming editor. It processes input line by line.-ndisables implicit printing.s/^name:[[:space:]]*//pperforms a substitution and prints only on match.^name:matches lines starting withname:.[[:space:]]*consumes trailing whitespace.- The matched prefix is removed.
pprints the result if substitution occurred.
environment.yamlis the input stream.
If, for example, the content of our environment.yaml contains:
name: StyleID
channels:
- conda-forge
dependencies:
- python=3.11The output would be: StyleID