The joist that supports a ceiling fan must be strong enough to withstand the forces applied by the motor and cable. A professional electrician will use a special hanger or box to mount the fan between joists or install a fan brace to support the box from above. If the fan is replacing a light fixture, the existing electrical box must be rated for fans (look for an inscription). If not, you must replace it with a junction box and fan brace or mount a support bar between joists.
Turn off electricity to the circuit breaker before removing or opening any wall switches and unfastening the screws or retaining nuts that hold the light fixture in place. Carefully lower the fixture and disconnect the wires by unscrewing or loosening the plastic connectors at each end of the cable’s ends. Remove the old electrical box if it’s nailed to a joist or hanging from a bar and pry it free with a flat bar. If the old box is a metal one that was installed by an electrician, you might need to remove a metal plate to loosen and then pry it off the joists.
If attic access is available, a fan brace can be mounted to support the electrical box between two joists without having to open the sheetrock and cut a new hole. If the joist cannot be accessed, you must open the sheetrock and install a 2×4 to support the junction box, then reinstall it with an electric fan brace or the appropriate screw-down bracket.