This paper introduces a variational strategy to learn spatially-varying metrics on large groups of images, in the Large Deformation Diffeomorphic Metric Mapping (LDDMM) framework. Spatially-varying metrics we learn not only favor local deformations but also correlated deformations in different image regions and in different directions. In addition, metric parameters can be efficiently estimated using a gradient descent method. We first describe the general strategy and then show how to use it on 3D medical images with reasonable computational ressources. Our method is assessed on the 3D brain images of the LPBA40 dataset. Results are compared with ANTS-SyN and LDDMM with spatially-homogeneous metrics.