Intro

conda is a very popular package manager for Python, particularly in the data science community.

The Conda package manager has two main distributions: Anaconda (a full distribution with all the libraries of the PyData ecosystem pre-installed) and a bootstrap version called Miniconda which includes the conda package manager and the libraries it depends on only.

In this post, I will demonstrate a useful short script for installing Miniconda directly from the command line on Linux and MacOS.

tl;dr – click here for the script 🚀

Miniconda vs Anaconda 🐍

Which one should you choose? The Conda documentation provides the following helpful distinction:

Choose Anaconda if you:

  • Are new to conda or Python.
  • Like the convenience of having Python and over 1,500 scientific packages automatically installed at once.
  • Have the time and disk space – a few minutes and 3 GB.
  • Do not want to individually install each of the packages you want to use.
  • Wish to use a curated and vetted set of packages.

Choose Miniconda if you:

  • Do not mind installing each of the packages you want to use individually.
  • Do not have time or disk space to install over 1,500 packages at once.
  • Want fast access to Python and the conda commands and you wish to sort out the other programs later.

I would always recommend using Miniconda. Anaconda is very bloated and contains many libraries which you are unlikely to use, especially not in a single project. Given installing a library is only a ‘pip install’ away, it is easy to use Miniconda and only install libraries as and when you need them. I also believe it is better practice to make sure your environment is as ‘lean’ as possible, containing only the packages your project directly depends on.

There are alternative package managers for Python instead of Conda. Personally, I prefer to use pyenv for managing my python environments. It is lightweight, portable, simple to use and I also find pyenv has fewer ‘gotchas’ than conda. However, sometimes you do not have the luxury of choosing your preferred environment manager. For example, if your project requires a version of a Python library only available from conda-forge , or if your team prefer using a specific package manager for consistency.

It is always a good idea to be comfortable with multiple different package managers so you are flexible to your team’s or project requirements.

Why Use the Command-Line? 💻

Installing Miniconda from the command line opens the door for automation.

Automating the set-up of your development environment is a key part of becoming an effective programmer. Automation provides the following direct and indirect benefits:

  • Seamlessly move across different machines – get up and running quickly when working on a new machine, such as spinning up a Cloud VM or a new company laptop, instead of manually trying to install software
  • Consistency – running the same script to install software on different computers will ensure you are running a consistent environment and help to avoid errors when installing the software
  • Reproducibility – share your scripts with co-workers to ensure you are both working from the same environment (Dockerfiles are also a good option)
  • ‘Clean Slate’ – when you inevitably accidentally break your environment (I am very guilty of this), you can easily remove everything and reinstall to restore the original state

⭐ Top tip: You can store your installation scripts in a public git repository which you can clone to your new machine. I personally keep my installation scripts in my dotfiles repo .

How to Install Miniconda on the Command Line 💡

Linux

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

MacOS

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm -rf ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

Notes

  • The only difference between the two scripts is the Miniconda version requested during the wget command. You have to install the correct version for your operating system, therefore, for Linux you need Miniconda-3-latest-Linux-x86_64 and for MacOS you need Miniconda3-latest-MacOSX-x86_64
  • Note, the wget program is not normally downloaded by default so you may need to install it before running the script
    # install on Linux
    sudo apt install wget
    
    # install on MacOS using homebrew
    brew install wget
    
  • By default, the script will download the latest version of Miniconda. If you want to download a specific version you need to change ‘latest’ with the version number you require. An archive of previous Miniconda versions can be found on the miniconda documentation page
  • After downloading the Miniconda package using wget, a bash command is used to run the Miniconda installation script (miniconda.sh). The installation script uses three flags – see the documentation for more details:
    • -b – install in batch mode
    • -u – update an existing installation if it exists
    • -p – prefix for the directory in which to install Miniconda
  • Next, the ~/miniconda3/bin/conda init bash and ~/miniconda3/bin/conda init zsh (if you are using zsh) commands modify the ~/.bashrc (and ~/.zshrc) files to add conda to your path so it is recognised as a command line program keyword
  • After running the script, restart your shell and conda should be ready to go

Note: these scripts were based on this helpful guide by Waylon Walker

Bonus ⚡

If you are using conda for your package management, I highly recommend checking out mamba .

One of my main irritations with using Anaconda in the past is that is takes a long time to solve the environment and download packages.

This has largely been solved with mamba which is an implementation of Anaconda in C++. Downloading packages and environments with mamba is significantly quicker than conda and requires very little additional setup after installing Miniconda.

To install mamba run the following command after installing miniconda

conda install mamba -n base -c conda-forge

You will now be able to use the mamba package manager in the same way as conda, however, now you just need to use the mamba keyword. For example

mamba create -n testenv python=3.9 

# much quicker than conda install!!
mamba install jupyterlab voila ipywidgets pandas matplotlib

Conclusion

In this post, we have demonstrated how to download Miniconda from the command line on to a Linux or MacOS machine.

You can incorporate these code snippets into a larger script which automates the installation and setup of your development environment. Automation will dramatically improve your efficiency when moving across different machines and improve reproducibility of your environments.

References

Further Reading