Lab 05 — Mamba and your first project env
Goal
Install mamba (the fast drop-in replacement for conda) on Unity, create a per-project Python environment with the scientific libraries you’ll use for the rest of this course, and export a portable environment.yml that you (or your labmates) can use to recreate the env on any machine.
By the end of this lab, you have mamba activate eslab working in any fresh shell, with NumPy, pandas, matplotlib, scikit-learn, and Jupyter installed and ready.
Reading
- Handbook: Python Environments on Unity — sections 1–5 in detail. (Section 6 — the pip trap — is Lab 6.)
Budget ~30 minutes for the reading.
Learning objectives
- Install Miniforge3 (which ships with mamba) into your Unity home directory.
- Create a per-project mamba environment and activate/deactivate it.
- Install scientific packages with
mamba install; inspect withmamba list. - Export
environment.ymlin both naive and--from-historyforms and explain the difference. - Register your environment as a Jupyter kernel for use in Lab 7.
Setup / prerequisites
- Labs 01–04 complete — you should be working from VS Code’s integrated terminal on Unity.
- ~5 GB of free space in your home directory for the Miniforge install.
Tasks
1. Download and run the Miniforge installer (10 min)
From your Unity home directory:
cd ~
wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Linux-x86_64.sh
ls -lh Miniforge3-Linux-x86_64.sh # confirm download worked, should be ~80 MB
bash Miniforge3-Linux-x86_64.shThe installer asks three questions:
- License — accept? Type
yes. - Install location: accept the default (
~/miniforge3) by pressing Enter. - Initialize Miniforge3? Type
yes. (This edits your~/.bashrcto add the conda/mamba shell functions.)
After it finishes:
# Re-source so the conda init block takes effect:
source ~/.bashrc
# Verify:
which mamba
mamba --version
which python # should now point at ~/miniforge3/bin/python✅ Self-check: mamba --version prints something like mamba 1.x.x.
2. Set strict channel priority (5 min)
conda config --set channel_priority strictThis avoids subtle dependency-resolution problems caused by packages with the same name living on multiple channels. See Handbook §3.4.
You can also clean up the channel list to use only conda-forge:
conda config --remove channels defaults 2>/dev/null || true
conda config --add channels conda-forge
cat ~/.condarc3. Create your eslab project environment (10 min)
mamba create -n eslab python=3.11 numpy pandas matplotlib scikit-learn jupyter ipykernel psutilThis will: - Resolve a compatible set of versions (mamba does this in seconds; conda would take minutes) - Download a few hundred MB of packages - Install everything under ~/miniforge3/envs/eslab/
When it asks for confirmation, type y.
Activate the env:
mamba activate eslabYour shell prompt should now show (eslab) at the start.
✅ Self-check: which python now points inside ~/miniforge3/envs/eslab/bin/python.
4. Sanity-check the environment (5 min)
python -c "import numpy; print('NumPy', numpy.__version__)"
python -c "import pandas; print('pandas', pandas.__version__)"
python -c "import sklearn; print('sklearn', sklearn.__version__)"
python -c "import psutil; print('psutil', psutil.__version__)"All four lines should print a version. If any fail with ModuleNotFoundError, you forgot to activate the env or the install didn’t finish.
5. Inspect what’s installed (5 min)
mamba listNote the Channel column on the right. Everything should say conda-forge (because of the strict channel priority you set in Task 2).
Try filtering:
mamba list | grep -i numpy
mamba list | head -206. Export environment.yml — two ways (10 min)
The naive export (everything, every version):
mamba env export -n eslab > environment_full.yml
wc -l environment_full.yml # likely 100+ lines
head -30 environment_full.ymlThe --from-history export (only what you explicitly asked for):
mamba env export -n eslab --from-history > environment.yml
cat environment.yml # should be just ~10 linesCompare the two. The --from-history form is the one you commit to a public GitHub repo for humans — it’s loosely pinned and recreates a working env even months later. The full export is more like a snapshot.
✅ Self-check: environment.yml lists python, numpy, pandas, etc. but not transitive deps like cycler or kiwisolver.
7. Register eslab as a Jupyter kernel (5 min)
So that when you launch Jupyter (Lab 7) you can pick “Python (eslab)” from the kernel menu:
python -m ipykernel install --user --name eslab --display-name "Python (eslab)"
jupyter kernelspec listeslab should appear in the kernel list.
8. (Optional) Auto-activate on login (3 min)
If you’ll spend 90% of your shell time in eslab, add to the end of ~/.bashrc:
mamba activate eslabNow every new SSH session lands in the env automatically. Skip this if you’d rather activate explicitly.
Deliverables
Save to lab05/ in your personal repo:
lab05/environment.yml— the--from-historyexport from Task 6.lab05/mamba_list.txt— output ofmamba list -n eslab(full list, with channel column visible).lab05/kernels.txt— output ofjupyter kernelspec list.lab05/reflection.md— 4–6 sentences:- What’s the difference between
environment.yml(--from-history) andenvironment_full.yml? - When would you commit the loose one to GitHub, and when the locked one?
- What did
conda config --set channel_priority strictchange, and why?
- What’s the difference between
Self-check
Common issues
❌ mamba: command not found after re-sourcing
The init block didn’t make it into .bashrc. Re-run:
~/miniforge3/bin/mamba init bash
source ~/.bashrc❌ “Solving environment: failed”
You asked for an impossible combination of versions. Read the error message — mamba usually tells you which constraints conflict. Common fix: loosen pins or remove a problematic package.
❌ Install fails with “disk quota exceeded”
Mamba caches a lot of packages in ~/miniforge3/pkgs/. Clear it:
mamba clean -aFor genuinely long-term storage of multiple envs, see Handbook §5 on creating envs under /fs/project/<group>/envs/ where the quota is much larger.
❌ import sklearn works in the shell but fails in a Slurm job
Slurm runs your script in a fresh non-interactive shell that doesn’t auto-activate the env. Add this to your batch scripts (you’ll see it in Lab 8):
source ~/miniforge3/etc/profile.d/conda.sh
mamba activate eslab❌ Two environments with similar names — eslab vs eslab_copy — and I can’t tell which is which
mamba env list # lists all environments and their full pathsTime estimate
- Reading: ~30 min
- Tasks: ~50 min (mostly waiting for the installer and for mamba to download packages)
- Deliverables: ~10 min
Total: ~1.5 hours
Extensions (optional)
Generate a lock file with conda-lock
For fully byte-exact reproducibility (useful for paper submissions):
mamba install -n base conda-lock
conda-lock lock -f environment.yml --platform linux-64
ls conda-lock.ymlThe resulting conda-lock.yml pins every package version, build hash, and source URL. See Handbook §8.
Install AI-assistant-friendly Python tooling
For better Python development:
mamba install -n eslab ruff black ipythonruff is a fast linter; whichever AI assistant you installed in Lab 2 will use VS Code’s Problems panel to see what ruff flags and can offer to fix the issues directly.
What’s next?
With a working env, you’re ready for the cautionary tale of Lab 06 — The pip-conda trap and how to recover.