Jesus did this as his second miraculous sign when he returned from Judea to Galilee.
After Jesus was born in Bethlehem in Judea, in the time of King Herod, wise men from the East came to Jerusalem
So when he came to Galilee, the Galileans welcomed him because they had seen all the things he had done in Jerusalem at the feast (for they themselves had gone to the feast).
When he heard that Jesus had come back from Judea to Galilee, he went to him and begged him to come down and heal his son, who was about to die.