Updated:

The easy way: a spawn egg

  1. Place a spawner: /give @p spawner (Bedrock: mob_spawner).
  2. Right-click it with the spawn egg of the mob you want.

This works in creative mode on both Java and Bedrock.

Place one with a command (Java)

/setblock ~ ~ ~2 spawner{SpawnData:{entity:{id:"minecraft:zombie"}}}

This puts a zombie spawner two blocks in front of you. Change id for another mob.

Adjust the spawning (Java)

/setblock ~ ~ ~2 spawner{SpawnData:{entity:{id:"minecraft:skeleton"}},MinSpawnDelay:100,MaxSpawnDelay:200,SpawnCount:4,MaxNearbyEntities:6,RequiredPlayerRange:16,SpawnRange:4}
FieldMeaningDefault
MinSpawnDelay / MaxSpawnDelayTicks until the next spawn (20 = 1 second)200 / 800
SpawnCountMobs it tries to spawn each time4
MaxNearbyEntitiesStops spawning when this many are nearby6
RequiredPlayerRangeOnly active with a player this close16
SpawnRangeSize of the spawning area4

Short delays and large counts make a server lag. Change the defaults a little at a time, even for farms.

Give a filled spawner item (Java 1.20.5+)

/give @p spawner[block_entity_data={id:"minecraft:mob_spawner",SpawnData:{entity:{id:"minecraft:blaze"}}}] 1

The spawner already contains the mob when placed. block_entity_data needs the id of the block entity.

When nothing spawns

Bedrock Edition

The Bedrock ID is mob_spawner. Delay and count cannot be changed with commands, so set the mob with a spawn egg.

Common questions

Check your command

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