Some hard dates are that Lloyds Register of ships goes back to 1754, though there was no general government requirement to register ships until the Merchant Shipping Act of 1786. Foreign-built ships had to be registered in 1661, and British ships wanting to trade with the colonies had to be registered from 1695.
However ships were 'registered' before that in the sense that they were known as xxx of yyy, where yyy was the home port. As far as I know that goes back to time immemorial and was necessary to distinguish between ships of the same name.
For instance Grace Dieu of Southampton was built in 1416-18 and Grace Dieu of Holdernesse in 1449 (in Hull, which is in Holderness).
I suspect the port of registry had to be put on the ship herself from 1786, but was done voluntarily before that.
About other countries I don't have information, though the general pattern is probably similar.
PS: Something that used to irritate us as kids was that Queen Mary and Queen Elizabeth were registered 'of Liverpool' while their true home port was Southampton.