Yes it's possible. You will need an ISP programmer. If you don't have a dedicated programmer you can use an extra Arduino board as an "Arduino as ISP" programmer. I believe it's possible to use the ESP8266 for this purpose using ESP8266AVRISP (available via File > Examples > ESP8266AVRISP > Arduino_Wifi_AVRISP when you have an ESP8266 board selected from the Tools > Board menu) but you'd need to do some research on that because I have no experience with that.
Here's the easiest way to do it:
- Install MegaCore, following the directions here: GitHub - MCUdude/MegaCore: Arduino hardware package for ATmega64, ATmega128, ATmega165, ATmega169, ATmega325, ATmega329, ATmega640, ATmega645, ATmega649, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega3250, ATmega3290, ATmega6450, ATmega6490, AT90CAN32, AT90CAN64 and AT90CAN128
- Tools > Board > ATmega2560
- File > Examples > AVR C code examples > Blink
- Sketch > Show Sketch Folder
- Navigate up four folder levels until you reach the folder that contains the file boards.txt.
- Open boards.txt in a text editor.
- Change line 185 from:
2560.menu.clock.16MHz_external.upload.port=UART0
to:
2560.menu.clock.16MHz_external.upload.port=UART1
- (That is for uploading to Serial1. If you prefer a different port then adjust the above instructions accordingly.)
- Save the file
- Close all Arduino IDE windows
- Restart the Arduino IDE
- Connect the ISP programmer to your Mega.
- Plug the ISP programmer in to your computer.
- Tools > Programmer > select the appropriate selection for your programmer
- Tools > Burn Bootloader
Note that you will no longer be able to upload to your Mega via the USB serial port (Serial0) after doing this. You can always repeat the above instructions to change the port or just select Tools > Board > Arduino/Genuino Mega or Mega 2560 and then do a Burn Bootloader.