Updated:

The simplest way: change game mode

/gamemode creative
/gamemode spectator

In Creative, double-tap jump to fly. Spectator mode is always flying and can pass through walls, which is handy for scouting. Return with /gamemode survival.

Java: alternatives that keep Survival

MethodCommandWhat it does
Levitation/effect give @s minecraft:levitation 5 1 trueFloats upward for 5 seconds; you cannot descend on your own
Slow falling/effect give @s minecraft:slow_falling 60 0 trueFall slowly and land safely
Elytra/give @s minecraft:elytraGlide from high places
Rockets/give @s minecraft:firework_rocket 64Boost while gliding with an elytra

Wear the elytra in the chest slot and press jump while falling to open it. Use a rocket while gliding to speed up.

Java: launch with a trident or wind charges

These are not flight, but they get you high into the air while staying in Survival.

# Riptide III trident (works in water or rain)
/give @s minecraft:trident[enchantments={riptide:3}] 1
/weather rain
# Wind charges (Java 1.21+)
/give @s minecraft:wind_charge 64

Both can still cause fall damage on landing, so add Slow Falling while you practice.

Bedrock: /ability mayfly

/ability @s mayfly true

Bedrock has /ability when the world setting "Education Edition" is turned on. With mayfly true, players can double-tap jump to fly while staying in Survival. Set it to false to undo. Change the setting on a copy of your world first; Realms and external servers may not allow it.

Servers

Java servers running Paper or Spigot often use plugins such as EssentialsX, which add a /fly command for players with permission. On vanilla servers, switching game mode is the only command-based option.

Common problems

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.