Install Ruby
Following is a link to the tutorial, and you can also find other tutorials for that suits your specific OS.
https://gorails.com/setup/osx/10.14-mojave
Installing the specific rails gem you want
$ gem install rails -v 4.2.6 --no-ri --no-doc