Skip to content

Instantly share code, notes, and snippets.

@aquohn
Last active August 19, 2021 17:20
Show Gist options
  • Save aquohn/85ba208b3c1ad276be0ed1b0f79a7245 to your computer and use it in GitHub Desktop.
Save aquohn/85ba208b3c1ad276be0ed1b0f79a7245 to your computer and use it in GitHub Desktop.
So you've given up on `pip`: a beginner's guide to building with `conda`

Introduction

NOTE: This is a work in progress. The author is no expert on build systems, and this serves more as a record of my experiences with conda than anything else. It is, however, sufficient for you to get to building and installing stuff with conda.

Who doesn't love pip install? A whole universe of packages on pypi, just a painless command away? Well, when you've got multiple projects with conflicting requirements, each of which have their idiosyncratic dependencies, it starts to really set in. pip's dependency resolution and optional approach to virtual environments makes using it quick and dirty, but when dirty is an unacceptable price to pay, in comes conda.

These are just my observations from mucking around with conda a bit. I'm not terribly invested in the project so forgive me if I get the history or motivation wrong, but from what I can see, it has immense potential to be a universal package manager, with the separation between virtual environments, with different sets of active packages, being built fundamentally into it, and with language agnosticism (although it is overwhelmingly used for Python). The conda-forge project consolidates community-contributed and maintained recipes for various packages, which are thoroughly tested to ensure that they can be built on a variety of platforms. Contributing is a piece of cake.

Many Python packages are available on pypi but not packaged for conda, and although you can use pip on top of conda, doing so negates many of its advantages, since conda won't know what pip is doing. It is imperative, therefore, to migrate as many packages as possible to conda.

The point of this gist, then, is to help complete beginners learn how to build and package pypi packages, and share them with the broader conda community, to advance the project. To reiterate, conda encourages more disciplined environment management and focuses on reproducible builds, so I would recommend everyone to start learning and using it as much as possible. While it is in theory a very general build system, I will focus specifically on pypi packages for now, which are currently the most mature (by far) and most in need of migration to conda.

Essential Packages

Unfortunately, conda is written in Python, and is the incarnation of the "Python slow" meme. So the first thing to do is conda install mamba, which is a drop-in replacement for conda with a faster solving library and partial rewrites in C++, and forget about conda as much as possible. Next,

mamba install grayskull boa

grayskull is a tool for generating recipes, files named meta.yaml which describe the dependencies and build steps for conda packages, while boa vastly expedites the build process by using mamba for it. It also provides a build system for a different kind of recipe for conda packages, but I haven't looked into them in detail. These are still-developing tools that are mature enough to provide a painless experience for building from pypi, but the "pure conda" commands that perform equivalent steps are listed as comments above them.

Building

Let's say we want to install <pkg> from pypi. Then, we would first generate the recipe

# conda skeleton pypi <pkg>
grayskull pypi <pkg>

This will create a folder in your current directory, <curr dir>/<pkg>/, and a file meta.yaml in that folder, which is your recipe. Next, build your package with

# conda-build <pkg>
conda mambabuild <pkg>

This will build the package, and package it as a tarball, in $HOME/.conda/envs/<env name>/conda-bld. You can just navigate to the tarball there, and run mamba install <tarball> to install your package for yourself and not think about it again...

Sharing

...or you could share the love by uploading it to conda-forge for everyone else to enjoy. The contribution instructions are at the link.

conda-forge is a channel for packages, one of many, but which encourages user contributions. If, for whatever reason, you want to host it on your own channel, you can run

mamba install anaconda

to install the client to communicate with the Anaconda servers which host these channels. After setting up your account, just run

conda config --set anaconda_upload yes

to upload all subsequent packages, and

anaconda upload <tarball>

to upload the package you just built.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment