We investigate the formation of three-dimensional sand patterns under a steady laminar shear flow using a process-based stability approach. The hydrodynamics of the problem is solved in the infinite depth case and in the long-wavelength approximation. The sand transport is described using two different models. The first one is based on a semiempirical law in terms of both the local bed shear stress and the local bed slope. The second one accounts for an additional mechanism depending on grain inertia. A three-dimensional linear stability analysis reveals that within both models, the most unstable mode is a longitudinal mode, thus corresponding to a ripple pattern with crests perpendicular to the flow direction. In the first model, the destabilizing effect of fluid inertia is counterbalanced by the stabilizing mechanism due to gravity, whereas in the second one, the effect of grain inertia on sand transport is found to be the pertinent stabilizing process for sufficiently large particle Reynolds number. In addition, we find that a range of oblique modes is unstable and can couple to longitudinal modes in the nonlinear regime. A weakly nonlinear analysis indeed shows that the coupling between two oblique modes and a longitudinal one gives birth to complex three-dimensional sand ripples, which migrate in the flow direction at constant speed, preserving their shape. As a direct consequence, the three-dimensionality of the phenomenon cannot be neglected.