Thursday, August 23, 2007

How to install Ubuntu from a USB flash drive

The title says it all and this is how I did it. Make sure your machine can boot from a USB device and that you have a pen drive that is at least 1 GB in size. This installation refers to a Feisty install but you can apply the same concept to Gutsy or Gutsy++. Be careful with the commands you type as one wrong digit can format your hard drive erasing all its data. I am not responsible for any damage you do to your machine. If you have never formated a computer or installed and Operating System, ask for help before going forward.

I woud say not to copy paste the commands as this can lead to errors on your side.

Insert the pen drive in the USB slot and wait until Ubuntu mounts it.

You must get the location where the USB drive mounts and this is the most critical step. Type the following and notice the mounting point where the USB drive is located:
df -hT

This command should give you all the drives that are available. In my case my USB drive mounting point shows up like this along with the other drives:

/dev/sdb1 vfat 957M 704M 254M 74% /media/disk

From now on I will refer to my mounting point as: /dev/sdb1 in your case you will need to change this to the one you obtained from the df -hT command.

Install the following two packages: syslinux mtools
sudo aptitude install syslinux mtools

Next we need to download the kernel that is going to boot the pen drive. Change the feisty part below to the release that you will be installing. Keep in mind that this is a i386 machine and you need to change accordingly.

Download these two files:

Next download the Alternate CD iso image from the Ubuntu download page. Keep in mind that this installation only worked for the Alternate CD. I tried doing a regular Desktop install but the process failed on me.
In this case my iso file name is: ubuntu-7.04-alternate-i386.iso

Unmount the flash drive by typing:
sudo umount /dev/sdb1

Now lets partition the pen drive with this command:
cfdisk /dev/sdb

/dev/sdb is the actual mounting point I mentioned above. Notice that you do not need the number 1 at end. Using the incorrect mounting point will erase your hard drive. Be Careful.
Keep in mind that we are going to erase all the data in the flash drive.

Do the following while in the cfdisk partitioning program:
  1. Erase any existing partition and then create a new "Primary Partition."
  2. Press "t" to select the partition type. Enter "0B" for a FAT32 partition.
  3. Make the partition bootable.
  4. Write the partition changes and exit the cfdisk program.
Now we are going to format the new partition created above. Once again change the /dev/sdb1 accordingly to your mounting point for the pen drive. The following command is going to format partition number 1 to a FAT32:
mkdosfs -F32 -v /dev/sdb1

Note that if the above command failed you need to make sure the USB drive is unmounted.

Install the boot loader in the flash drive first partition:
syslinux /dev/sdb1

Mount the flash drive and copy to it the files downloaded above:

Create a new file in the pen drive with the following 2 lines:
default vmlinuz
append initrd=initrd.gz ramdisk_size=700000 root=/dev/ram rw

Save the file with this name: syslinux.cfg

Your pen drive should now have 5 files in the same place:

We are done. Now you will need to go into your computer's BIOS during boot. Select that you wish to boot from the USB device first instead of going to the Hard Drive or CD-ROM.


Anonymous said...

you dont actually have to format the drive if its already fat32 bootable

Alex said...

Simply great!

I don't know if I should cry or laugh out load! I tried so many different ways for setting up a bootable USB device to install an alternate or server "low-level" ubuntu from. Your suggestion was by far the easiest one to do and the ONLY one that worked for me! I wouldn't have guessed that putting the plain .iso on the stick would be sufficient. Thanx so much for writing this how-to!

Greetings, Alex

P.S.: Most of the other ways to do it started by setting up a live-system on the flash drive. But since I got almost no memory on my system they all failed for the one or other reason.

Hari Gaire said...

can u tell me why i get this error while booting from USB
"couldn't find kernel image:linux"

Anonymous said...

It works, it's the same method I used. Anyway you should add that after the installation you must modify "/boot/grub/menu.lst" otherwise it won't boot.

Anonymous said...

On thing I'm curious about doing is creating a flash drive that I can have multiple images on. Basically the same thing, but with a brief menu that allows you to select which *.iso to load.

Anonymous said...