Gentoo Linux is a highly customizable Linux distribution that gives users fine-grained control over their system. While the installation process is more involved than other Linux distributions, it allows you to tweak your system to be as lean and optimized as possible. This complete guide will take you through installing Gentoo Linux from scratch.

Choosing the Installation Media

The first step is obtaining the installation files. Gentoo offers a few options:

Minimal Installation CD

This is the recommended method for most hardware. The minimal install CD boots into a basic Linux environment and allows you to partition disks, connect to the internet, and download the stage tarballs.

LiveDVD

The Gentoo LiveDVD contains a complete Gentoo system and various tools to aid in the installation process. It offers convenience at the cost of download size. The LiveDVD is helpful if you need to test hardware compatibility before installing.

Stage Tarballs

For exotic hardware situations, you can download stage tarballs directly instead of using the minimal CD or LiveDVD. The tarballs contain a pre-built file system and packages to bootstrap the install. There are three stages:

  • Stage 1: Designed for developers building Gentoo from scratch. Contains just a bootable system.
  • Stage 2: Starts from stage 1 and compiles the base toolchain. For advanced users only.
  • Stage 3: Includes a full Gentoo base system that is usable out of the box. Recommended for most users.

Partitioning Disks

Before installing, you‘ll need to partition your disks. Gentoo has a naming scheme for partitions that the documentation uses. Here is an example partitioning setup for a UEFI system with a separate home partition:

  • /dev/sda1 – 512MiB – EFI system partition
  • /dev/sda2 – 50GiB – Linux file system partition (root)
  • /dev/sda3 – 20GiB – Linux file system partition (home)

Use your preferred partitioning tool to create the partitions, then format them:

mkfs.fat -F32 /dev/sda1 # For EFI partition 
mkfs.ext4 /dev/sda2     # For root partition
mkfs.ext4 /dev/sda3     # For home partition

Booting the Installation Environment

After partitioning, boot into the Gentoo install environment using either the minimal install CD or LiveDVD.

The minimal CD gives a boot menu that lets you optionally specify kernel parameters. This helps in situations where the framebuffer doesn‘t work properly. If you don‘t select anything, it will boot after 15 seconds using default options.

Once booted, the first steps are configuring networking and storage:

Networking

To enable networking, first check your IP address:

ip address show

Then start sshd to allow remote logins:

/etc/init.d/sshd start

Set the root password:

passwd

You can now ssh into the installation environment to continue the install process.

Mount Partitions

Now mount the partitions created earlier:

mount /dev/sda2 /mnt/gentoo # root partition
mount /dev/sda3 /mnt/gentoo/home # home partition 

We will mount the EFI partition later when configuring the bootloader.

Downloading Stage Tarballs

If you booted into the minimal install environment, you‘ll need to download the stage tarballs over the internet.

Change directory to where we mounted the disk partitions earlier:

cd /mnt/gentoo

Then use the links browser to navigate to a Gentoo mirror and download stage tarballs:

links www.gentoo.org/downloads

Alternatively, use wget directly if you know the URLs already.

After downloading the tarballs to our disk, unpack the stage 3 file:

tar xvf stage3-*.tar.xz --xattrs-include=‘*.*‘ --numeric-owner

The stage 3 tarball contains a base Gentoo system we will chroot into soon.

Entering the Chroot

Now we‘ll enter the chroot environment using the files we just unpacked:

mkdir /mnt/gentoo/etc/portage/repos.conf
cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
cp -L /etc/resolv.conf /mnt/gentoo/etc/
mount -t proc /proc /mnt/gentoo/proc 
mount --rbind /sys /mnt/gentoo/sys
mount --make-rslave /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo /dev
mount --make-rslave /mnt/gentoo/dev
chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) $PS1"

This will enter the chroot environment using our unpacked system on the disk partition. We also mounted some virtual filesystems the chroot will need access to.

From inside this chroot environment, we will install the Gentoo system.

Configuring Portage

Portage is Gentoo‘s package management system. It is based around the emerge command which handles building and installing software.

First, update Portage‘s package repositories:

emerge --sync

Next we‘ll configure some Portage options:

eselect profile list
eselect profile set <number>

cat > /etc/portage/make.conf << "EOF"
COMMON_FLAGS="-march=native -O2 -pipe"
MAKEOPTS="-j5" 
GENTOO_MIRRORS="http://distfiles.gentoo.org"
EOF

This updates our profile, compiler flags, parallel build options, and Gentoo mirror in make.conf.

We also need to update the environment for our new configuration:

env-update && source /etc/profile

Now Portage is ready to use!

Installing a Kernel

Gentoo gives you the flexibility to fully customize and compile your own kernel. However, it also provides binary kernels which are easier to install initially:

emerge sys-kernel/gentoo-kernel-bin

We‘ll configure the rest of the boot process later. The key point for now is we have a kernel installed ready for bootstrapping the system.

Configuring the Live System

Now we‘ll configure some system options to get our initial live environment running outside of the chroot.

Set the time zone:

ls /usr/share/zoneinfo
echo "Europe/London" > /etc/timezone

Install and configure system clock synchronization:

emerge --config sys-libs/timezone-data

Next, locale and language settings:

nano -w /etc/locale.gen
locale-gen
eselect locale list
eselect locale set <number>
env-update && source /etc/profile && export PS1="(chroot) $PS1"

And networking via DHCP:

echo ‘config_eth0="dhcp"‘ > /etc/conf.d/net
emerge net-misc/dhcpcd 
systemctl enable dhcpcd

Configuring the Boot Loader

Now we need to install and configure a boot loader to boot into our new system.

First, add an /etc/fstab entry for the EFI partition:

/dev/sda1  /boot        vfat    defaults        0 2

Then install GRUB with EFI support:

emerge sys-boot/grub:2
grub-install --efi-directory=/boot

Finally, generate the GRUB configuration file:

grub-mkconfig -o /boot/grub/grub.cfg

Our bootloader is now ready!

Finalizing the Installation

We‘re almost ready to reboot into our new system! Perform some final install steps:

emerge gentoo-sources
cd /usr/src/linux
make menuconfig # configure kernel
make install # install kernel 
emerge @world --emptytree

Here we install a kernel source, configure it, compile and install the kernel, then update all packages in our system.

Finally, reboot and remove the install media. Our new Gentoo Linux install should boot up!

Post-Installation

After booting into the live system, there are some additional steps to perform:

  • Create a new user account
  • Install a desktop environment
  • Configure service daemons like systemd
  • Recompile the kernel for your specific system

And since this is Gentoo, optimizing performance never stops. You can continually tweak USE flags and compiler options to squeeze maximum performance for your exact hardware setup.

Over time you can mold your Gentoo install into a lean, mean, performance-optimized machine fine-tuned to your particular needs. While the initial install takes effort, you are rewarded with an incredibly customizable system.

Conclusion

Installing Gentoo Linux from scratch teaches you the internals of a Linux system. The process gives insight into how the distribution is structured and pieced together. While it requires time and effort, you gain knowledge and control unrivaled by other distributions.

If you want to delve into the inner workings of Linux and don‘t mind getting your hands dirty, give Gentoo a try. Work through the initial install pains, and you may just fall in love with this supremely tweakable distribution!

Similar Posts