A cabinet sold as 18 inches has an opening narrower than 18 inches, and the opening is what the hardware has to pass through.
Cabinet nominal sizes describe the carcass. The clear opening is that figure minus the face-frame stiles, the door thickness where it folds back, and on a sink cabinet, the center stile if there is one. On a face-frame cabinet that can be an inch and a half narrower than the number on the label. Manufacturers publish a minimum opening for exactly this reason: simplehuman's 35-liter dual under-counter can needs 20 inches high by 10 wide by 20 deep; the Rev-A-Shelf 4WCSC bottom-mount needs 15 inches wide by 20 and five-eighths high.
Depth is the one that catches people out under a sink, because the trap and the waste pipe eat into the back of the cabinet at exactly the height the buckets need. Measure to the nearest obstruction, not to the back wall. Step-by-step, with the numbers.