Installing TWRP on a locked OnePlus One without wiping data I am trying to upgrade my OPO's operating system from CyanogenMod to the latest nightly version of LineagOS.

As a first step, I tried installing TWRP, by executing from my shell the command:

fastboot flash recovery recovery.img


But I got the following message:

FAILED (remote:         Device not unlocked cannot flash or erase)


I read in a few places that I should first unlock my device by executing the command fastboot oem unlock, but that it is going to erase all my data.

Is it true? Is there a way to avoid the erasure of the data?
NO. Period.

If you want to unlock the bootloader erasure is inevitable. You can make a backup, but I'm not sure it'll be restored. However what is the point when you flash a custom ROM you'll need to wipe data in that case anyway. However you can move media to SD card pull it out and unlock bootloader (Make sure the SD card isn't encrypted).