The nematode Caenorhabditis elegans is emerging as a useful model for studying the molecular mechanisms underlying interactions between hosts and their gut microbiomes. While experiments with well-characterized bacteria or defined bacterial communities can facilitate the analysis of molecular mechanisms, studying nematodes in their natural microbial context is essential for exploring the diversity of such mechanisms. At the same time, the isolation of worms from the wild is not always feasible, and, even when possible, sampling from the wild restricts the use of the genetic toolkit otherwise available for C. elegans research. The following protocol describes a method for microbiome studies utilizing compost microcosms for the in-lab growth in microbially diverse and natural-like environments. Locally sourced soil can be enriched with produce to diversify the microbial communities in which worms are raised and from which they are harvested, washed, and surface-sterilized for subsequent analyses. Representative experiments demonstrate the ability to modulate the microbial community in a common soil by enriching it with different produce and further demonstrate that worms raised in these distinct environments assemble similar gut microbiomes distinct from their respective environments, supporting the notion of a species-specific core gut microbiome. Overall, compost microcosms provide natural-like in-lab environments for microbiome research as an alternative to synthetic microbial communities or to the isolation of wild nematodes.