It is a farḍ kifâyah (collective obligation); so if it is performed by a sufficient number, the obligation is fulfilled. It is an individual obligation for anyone who shows up at the battlefield or whose town is surrounded by the enemy. It is only obligatory on capable, sane, free adult males.
وَهُوَ فَرْضُ كِفَايَةٍ، إِذَا قَامَ بِهِ مَنْ يَكْفِيْ، سَقَطَ عَنِ اْلبَاقِيْنَ، وَيَتَعَيَّنُ عَلىٰ مَنْ حَضَرَ الصَّفَّ، أَوْ حَصَرَ اْلعَدُوُّ بَلَدَهُ. وَلاَ يَجِبُ إِلاَّ عَلىٰ ذَكَرٍ حُرٍّ، بَالِغٍ، عَاقِلٍ، مُسْتَطِيْعٍ