Quick Links
If you’re a programmer, particularly in data science or analysis, you’ve probably suffered headaches over the packages included in a Linux distribution. What if there were a way you could have a separate programming environment for your projects that was isolated from the system without having to set up a virtual machine? Mamba might be what you need.
What Is Mamba?
Mambais a package manager that’s intended for per-project use. It’s similar to a package manager that you find on a modern Linux system, but instead of managing software packages for the whole system, you create environments just for the packages you need for your project. Mamba is a reimplementation of the Conda package manager. Conda was originally written in Python, but Mamba was rewritten in C++ for extra speed.
Mamba, like Conda, is popular for data science, statistics, biostatistics, and data analysis. It seems to have an affinity with Python, given the snake-themed naming. The name seems to be chosen for the speed of the namesake snake, as Mamba is a faster version of Conda.
Mamba does a lot of the things you would expect a package manager to do, such as searching for packages, installing them, and keeping them updated. You create environments that you’re able to activate or deactivate at will when you want to. You can install multiple environments on the same machine.
Why Mamba?
If you’re using a Linux or other Unix-like system for development, you might wonder why you’d need something like Mamba. Don’t you already have a package manager?
The software that’s installed in a package manager is meant for the system as a whole. Many systems include a Python interpreter, but that’s intended to run scripts and other programs that depend on Python, and not so much to support your programming projects. If you need a newer version of Python for your own code than the system supplies and you upgrade it, other scripts and programs that depend on the version supplied with your system might break.
Mamba solves this problem by letting you create environments for a task that are isolated from the underlying system. If you’re working in data science or analytics, you’re able to define an environment just for your work while leaving the base system untouched. You can activate your environment when you want to run some calculations and turn it off when you’re finished.
I’ve used Mamba for several pieces on HTG, including a recent one onbuilding a laptop price predictor.
Installing Mamba
Installing Mamba is pretty simple in most Unix-like systems. I’ll demonstrate installing Mamba on a Debian Linux system.
you’re able to follow some simple instructions to download and install Mamba right from the Mamba documentation page. First, you’ll want to download theMiniforge distribution. This is a minimal version of Mamba that has all the basic files you need to get started.
Assuming you have curl installed, all you have to do is paste in a line todownload it on a Unix-like system:
After you’ve downloaded it, you can now run the installation script:
The script will ask you to press Enter if you want to continue, then review some licensing terms. Since Mamba is open source, this probably won’t be a problem for you. You’ll have to type “yes” or “no” to accept or decline the licensing terms. To accept and continue with installation, type “yes” at the prompt.
After that, you’ll have to confirm the directory where you want to install your Conda packages. By default, it will be in your home directory with a “miniforge” subdirectory.
When miniforge is installed, you’ll then get a prompt to edit your shell’s default startup script, such as .bashrc or .zshrc, depending on which shell you’re using, to start Mamba up automatically when you start a shell. You’ll probably want this if you plan on working with Mamba extensively. This’ll make it easier to activate and deactivate Mamba environments. Type “yes” again to confirm it.
Create an Environment
When you open a new terminal, you may have noticed something different about your shell prompt. You’ll see something that says “(base)”. This means that Mamba has been activated with the base environment. That’s exactly what it sounds like: a minimal base environment. You can check this by typing “python” at the prompt. In the introduction message, you can see that this version of Python was “packaged by conda-forge.”
You can install other packages into this base environment, but you don’t want to. The point of Mamba is to have all the packages you need in one environment, and others you need for a different project in another environment.
It’s easy to create environments. To create an environment, you use the mamba create command with the “-n” option followed by the name of the environment you wish to create, then the list of packages you want to install in this environment.
For example, to create an environment for statistical computing named “stats” with theNumPy,SciPy, andpandasPython packages, use this command:
Like any other package manager, it will determine the packages and their dependencies, and finally ask you to confirm their installation. Since the installation is done in your home directory, you don’t need to use sudo or supply a root password.
To activate the stats environment, use the “mamba activate” command:
You’ll see the name of the active environment change from “base” to “stats” in the prompt. You can now use the packages you installed in this environment.
Let’s test this out by running the Python interactive interpreter.
We can import NumPy and have it work.
It will report the answer as a floating-point number of 2.5.
Installing Extra Packages
A lot of the time, you realize you might want to add packages to your existing environment. For example, I like the IPython interpreter over the standard interpreter. I can add it in one of two ways.
I can add it using the -n option with “mamba install”
I can also run “mamba install” within an active environment to install the package in that environment. Since “stats” is already active, I’ll do that. It’ll ask me to confirm the changes and then install IPython.
I can run IPython at the shell:
To deactivate an environment when I’m done with it, I can use the “mamba deactivate” command. To prove that IPython was installed into the “stats” environment, when I try to run it, I get the “command not found” error after deactivation.
To find packages, you can use either the “mamba search” or “mamba repoquery” search commands.
For example, to find packages relating toJupyter, a popular notebook interface for scientific computing:
Updating Mamba Environments
As with other package managers, Mamba is handy at keeping packages updated as new versions become available. You can use the “mamba update” command.
For example, to update all the packages in the stats environment:
Alternatively, you can use “–update-all” instead of “–all” in the command, but the latter is shorter. Since this is a fresh install, there’s nothing to update when I run this command.
With Mamba, you can create environments suited to your programming projects, activate and deactivate them, and update them at will. It might seem strange having a package manager on top of your package manager, but using Mamba will make your programming project go much more smoothly.