Leonardo resets and runs old program again before new one is done uploading

EDIT:
My Arduino Leonardo bord works fine on my friends os x but when i try to upload a program from my computer with the latest ubuntu and arduino IDE it starts the upload, tx and rx blinks a few time but before the IDE finishes the board stops blinking and the program i had on since before (from the mac) starts again and a few seconds later the arduino IDE says "Done uploading" without any errors. Its like the board resets itself before the upload finishes...

i started arduino IDE with sudo command now the problems beneath is happening again also sometimes the IDE crashes after the uploading finishes (and works)

When i try to upload to my leornado i get one of the 3 errors in the bottom of this post but after maybe 2-3 tries (it differs) its says uploading done but my program is not running if i upload again however it will run. if i upload a third time then i start getting the errors again. Also sometimes the port used for usb changes from ttyACM0 to ttyACM1 (ACM 0 dissapears and ACM 1 appears in the toolbar) and after a few more uploads to ACM2,ACM3 etc.

Im running a fresh install of the latest ubuntu.

Works good to upload from a mac but not from my computor running ubuntu

avrdude: butterfly_recv(): programmer is not responding
Device code: 0x3f

avrdude: butterfly_recv(): programmer is not responding

avrdude: error: programmer did not respond to command: set addr

EDIT: Got a new error

Found programmer: Id = "13
13
"; type = 1
    Software Version = 1.3; Hardware Version = 
.

avrdude: error: buffered memory access not supported. Maybe it isn't
a butterfly/AVR109 but a AVR910 device?

EDIT: And now also this

avrdude: error: programmer did not respond to command: write block

vrdude: verification error, first mismatch at byte 0x1200
         0x0c != 0x90
avrdude: verification error; content mismatch

bump

I am using ubuntu 14.04 and have had same problems.
The IDE is not correctly unlocking the device when it does it's thing. Do a
cd /var/lock/
directory and look for the locked file.
Use ls command and remove it using
su rm /var/lock/LCK...ACM0
or what ever lock file is locked there. Try to upload again.
You can check to see which device is recognized by using
lsusb
command in Terminal with no space. My system uses ttys0 some of the time also.

Hope this helped you. Solution from Larry Littlefield on GitHub site.
Don