The paper deals with the robust energy-based stabilization of a wheeled inverted pendulum, which is an underactuated, unstable mechanical system subject to nonholonomic constraints. The equilibrium to be stabilized is characterized by the length of the driven path, the orientation, and the pitch angle. We use the method of Controlled Lagrangians which is applied in a systematic way, and is very intuitive, for it is physically motivated. After a detailed presentation of the model under nonholonomic constraints, we provide an elegant solution of the matching equations for kinetic and potential energy shaping for the considered systems. Simulations show the applicability and robustness of the method.