The majority of insect species have a clearly defined larval stage during development. Larval nutrition is crucial for individuals’ growth and development, and larval foraging success often depends on both resource availability and competition for those resources. To date, however, little is known about how these factors interact to shape larval development and behaviour. Here we manipulated the density of larvae of the polyphagous fruit fly pest Bactrocera tryoni (‘Queensland fruit fly’), and the diet concentration of patches in a foraging arena to address this gap. Using advanced statistical methods of machine learning and linear regression models, we showed that high larval density results in overall high larval aggregation across all diets except in extreme diet dilutions. Larval aggregation was positively associated with larval body mass across all diet concentrations except in extreme diet dilutions where this relationship was reversed. Over time, larvae in low-density arenas also tended to aggregate while those in high-density arenas tended to disperse, an effect that was observed for all diet concentrations. Furthermore, larvae in high-density arenas displayed significant avoidance of low concentration diets – a behaviour that was not observed amongst larvae in low-density arenas. Thus, aggregation can help, rather than hinder, larval growth in high-density environments, and larvae may be better able to explore available nutrition when at high-density than when at low-density.