Chain length is one of the few setup numbers where being wrong in either direction causes damage, and where the correct answer is not printed on anything.
The standard method
Wrap the chain around the largest chainring and the largest sprocket, bypassing the rear derailleur entirely. Bring the two ends together until they just meet, then add two links — one inch — and that is the length.
This sets the chain so that even in the largest-largest combination, which most drivetrains are not designed to be ridden in, the derailleur cage is not pulled to its limit.

Why two links and not one
The derailleur cage rotates to take up slack. In the smallest-smallest combination it is at maximum wrap; in largest-largest it is at minimum.
Two links is the margin that keeps the cage from reaching a hard stop at the large end while still leaving enough spring tension to control the chain at the small end. One link is not enough margin on most systems; four makes the chain slap and shift lazily.
Where the rule does not apply
Full-suspension bikes. Suspension movement changes the distance between bottom bracket and rear axle, and on many designs the chain stay lengthens through the travel. Sizing at sag or at full extension — whichever the manufacturer specifies — matters, and using the rule with the suspension at rest can leave a chain that is too short at bottom-out. That failure destroys derailleurs.
Clutch derailleurs and long-cage 1x systems. Several manufacturers publish their own procedure that counts links from a reference position rather than using the wrap method. Where the manufacturer publishes one, use it.
Very large cassettes. With a 50 or 52 tooth largest sprocket, the wrap method still works but the margin becomes tight, and the derailleur's stated maximum capacity has to be respected separately.
What each error looks like
Too long — the chain slaps on rough ground, shifting into the small sprockets feels vague, and in the smallest-smallest combination the chain can hang slack enough to touch the cage or drop off the chainring.
Too short — the real danger. Shifting into the largest-largest combination pulls the derailleur cage past its limit, and something gives: the hanger bends, the derailleur is pulled into the spokes, or the chain snaps. On a 1x drivetrain there is no front shifting to protect you from reaching that combination by accident.
Measure the old chain, carefully
Replacing a chain that was the right length is the easiest approach: count the links and match. It only works if the old chain was correct, and if nothing else has changed.
Anything that changes the drivetrain — a different cassette, a different chainring, a new frame, a different derailleur — invalidates the old length and requires sizing from scratch.
Direction and joining
Modern chains are directional. Most have a marked outer face, and installing them backwards causes poor shifting that is very hard to diagnose because everything looks correct.
Join with a quick link matched to the speed count, or with a manufacturer's connecting pin. Do not reuse the pin that was pushed out, and do not reuse a quick link that is rated single-use — several are.
Check it once installed
Shift into largest-largest by hand, slowly, watching the derailleur cage. It should have visible movement left before its stop.
Then shift to smallest-smallest and look at the chain between the lower jockey wheel and the chainring. It should be tensioned, not hanging. If both checks pass, the length is right.
