Gentoo is a powerful and extensible Linux distribution. It is one of the few operating systems that stuck to the original source-based package management in Linux. Further, its package manager, portage, is a powerful utility that allows you to fine-tune and tweak each aspect of your distribution.
Content
- Why Use a Source-Based Distribution?
- Why Use Gentoo?
- Obtaining Gentoo
- Installing Gentoo Linux
- Setting Up the Network Connection
- Partitioning the Hard Disk
- Downloading the Gentoo Stage 3 Tarball
- Your First Gentoo System Update
- Time Zone and Locales
- Configuring the fstab
- Installing Additional Tools
- Installing the Bootloader
- Frequently Asked Questions
Why Use a Source-Based Distribution?
Nowadays, most Linux distributions provide precompiled packages. This means that the distribution builds the packages in the developer’s machine.
While compiling packages is not necessarily an issue in today’s high-bandwidth world, there are a number of problems that should be considered in that distribution model:
- Compiled binaries mean that you do not have control over how the program is being compiled and what features are included in the program.
- Relying on binaries means that you give your full trust to the distribution’s developers that they will not include malicious code in their repositories.
Why Use Gentoo?
The reason is simple: Gentoo is a powerful Linux distribution. It gives you almost unrestricted access and freedom to change and modify the whole system.
Portage, the Heart of Gentoo
Portage is the main component that manages a Gentoo Linux system. It installs, manages and tweaks third-party packages. Further, portage also manages basic system configurations.
The main difference that Portage offers is that it allows you to install your packages from its source. While this is possible in other distributions, Portage takes it a step further and makes it the default means of obtaining packages.
Obtaining Gentoo
You can obtain a copy of Gentoo from its website. In there, you can choose the installer from a number of different platforms. Gentoo supports both amd64 and arm64.
With the downloaded ISO file, you can then use a USB writing program like balenaEtcher to create your bootable flash drive.
Installing Gentoo Linux
Before we continue installing Gentoo, it is important to note that this process is entirely manual, which means a lot of commands will be executed along the way. As such, it is recommended that you not only consult this guide but also check Gentoo’s official handbook for further guidance during the installation.
Stick your flash drive into your computer and boot it up. It will boot you into the Gentoo Installer screen.
Setting Up the Network Connection
To install Gentoo, you need to be connected to the Internet. The installer will connect automatically on a wired connection. From there, run the following command to check whether you are online:
ping -c 5 maketecheasier.com
If you need to connect to a wireless network, you need to set up wpa_supplicant, which will connect you to your wireless access point.
wpa_passphrase 'Your_SSID_Here' 'Your_Password_Here' >> /etc/wpa_supplicant/wpa_supplicant.conf
Once done, you have to reload the dhcpcd
daemon to start your network connection by running this command:
rc-service dhcpcd restart
Partitioning the Hard Disk
Next, you need to format the disk you want to install Gentoo on. Do that easily by using the fdisk
command.
Fdisk is a simple command-line disk partitioning tool. It allows you to partition your hard disk and set the amount of storage you want for your system. Initialize it by typing:
fdisk /your/disklabel
If you are not sure of your existing disk partition structure, check it with the -l
flag:
fdisk -l
Using fdisk to Partition the Hard Disk
Once inside fdisk, you need to first wipe the disk for any existing partitions. Do that by pressing G in the prompt.
With that done, you need to create new partitions in the disk:
- Press N to tell fdisk that you want to create a new partition. Fdisk will ask you for the partition number you want to use. Press Enter to use the default.
- Fdisk will ask you for the first and last sectors of the partition. Press Enter for the first sector and specify the size for the last sector. The general syntax for this is relatively simple:
+size(G|M|K)
- The + sign tells fdisk that you want the size to be relative to the first sector you have selected.
- The size is a number that you specify yourself.
- The last option tells fdisk whether that size is in GB, MB or KB.
Partitioning for a UEFI Gentoo System
With that done, continue partitioning your disk. This process will largely depend on whether you are using a BIOS or UEFI system.
If you are using a modern computer with relatively recent hardware, chances are that you are using a UEFI system.
To create a UEFI-compatible disk, you need to first create an “EFI System” partition. This should be the first partition in your disk and around 100-256MB in size.
With that done, set the type of that partition to “EFI System” by pressing T. This will tell fdisk that you want to change the type of the partition you just created. From there, it will ask which type you want to set the partition to. Type 1.
Partitioning the Rest of the Disk
The next partition you have to make is the “/boot” partition where the Linux kernel and the bootloader will be saved.
Ideally, this partition should be around 500M-1G. In my case, I am setting this partition to 1G.
From there, create the SWAP partition: part of your hard disk that your RAM can access to dynamically swap its memory whenever it is running out of space.
The general rule for the SWAP size is 1/2 to 2 times the amount of memory that you have in your system. My laptop currently has 8GB of memory installed, so I set my SWAP size to 16GB.
Further, to properly set this partition as SWAP, set its type to “19.”
Lastly, create the root partition where the rest of the system will be installed. To allocate the rest of the disk, just press Enter in the Last Sector option when creating a new partition.
With that done, press P to check the partition table that just created.
Press W to save your changes to disk.
Formatting the Disks in Gentoo
The next step is to format your hard disk. First, format the EFI System partition as FAT by using the following command:
mkfs.vfat /dev/sda1
This will create the FAT filesystem that the EFI system requires to boot. From there, create the filesystem for the “/boot” partition by using the simpler ext2 filesystem.
mkfs.ext2 /dev/sda2
Next, create and enable the SWAP partition to tell Gentoo that this partition can be used for swapping with live memory. To do that, run the following:
mkswap /dev/sda3 swapon /dev/sda3
Lastly, create an ext4 filesystem for the root partition by running the following command:
mkfs.ext4 /dev/sda4
Downloading the Gentoo Stage 3 Tarball
From there, mount the root partition you just created to the “/mnt/gentoo” directory because the installation will now be done in your machine’s hard disk.
mount /dev/sda4 /mnt/gentoo && cd /mnt/gentoo
Once done, download the Stage 3 tarball from the gentoo.org website by using wget:
wget https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds/20220227T170528Z/stage3-amd64-desktop-openrc-20220227T170528Z.tar.xz
This tarball contains all the programs you need to have a minimal Linux installation. Unpack it using tar:
tar xpvf ./stage3-amd64-desktop-openrc-20220227T170528Z.tar.xz --xattrs-include='*.*' --numeric-owner
Selecting a Download Mirror and Copying DNS Info
Indicate where you want to download your packages for this system by running the mirrorselect command:
mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf
This will open a TUI program where you can select the closest server location to you.
From there, copy the repository file for Gentoo to your “/etc/portage” folder, which is a configuration file that contains the links to how Gentoo will download its packages:
mkdir -p /mnt/gentoo/etc/portage/repos.conf && \ cp /mnt/gentoo/usr/share/portage/config/repos.conf \ /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
Lastly, copy the resolver information from your USB installer to the “/mnt/gentoo” directory to ensure that once you change the root directory to the hard disk, the network will still work. To do that, run this command:
cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
Mounting the Device Files and Chrooting
Begin mounting the special directories from the USB installer to the hard disk to make sure that once you have run chroot, the peripherals you are using still work.
mount --types 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 mount --bind /run /mnt/gentoo/run mount --make-slave /mnt/gentoo/run
The last thing to do in the USB installer is to run the chroot command to move the base root directory from the USB to your hard disk:
chroot /mnt/gentoo /bin/bash source /etc/profile export PS1="[chroot] ${PS1}"
Mounting the /boot partition and Updating Gentoo
Mount the “/boot” partition to your Gentoo machine to ensure that when you install the kernel later that it will be saved in the correct place:
mount /dev/sda2 /boot mkdir /boot/efi mount /dev/sda1 /boot/efi
Once that is done, update your Gentoo repositories to make sure you get the latest packages when you do the first system update:
emerge-webrsync
From here, emerge-webrsync
will connect to the mirrors that you have selected and download the latest EBUILDS available for Gentoo.
Your First Gentoo System Update
You can now update your system to match the latest EBUILDS by running this emerge command:
emerge --ask --verbose --update --deep --newuse @world
This will ask emerge to check your Stage 3 packages to the ones that are currently on the Gentoo repository. Emerge will update the packages that it finds to have a more recent version.
As such, this process will take a while, as Emerge will fetch and compile each and every package it needs to update.
Time Zone and Locales
Next, you need to set up region-specific information, which includes both your time zone and system locale. To set the time zone to your area, find the closest location to you listed in the “/usr/share/zoneinfo” folder.
From there, write it to your “/etc/timezone” file. For example, the closest location to me is “Asia/Manila”:
echo "Asia/Manila" > /etc/timezone emerge --config sys-libs/timezone-data
Now set your locale for Gentoo will determine the default system language for the computer. To do this, edit the “/etc/locale.gen” file:
nano -w /etc/locale.gen
From there, add the locale you want Gentoo to use. You can view the full listing of available locales by checking the “/usr/share/i18n/SUPPORTED” file. In my case, I want the system to use a U.S. English locale:
en_US ISO-8859-1 en_US.UTF-8 UTF-8
Then run locale-gen to recreate your system locale.
Installing the Linux Kernel
There are multiple ways to install the Linux Kernel in Gentoo. Either manually configure all of its features or use a pre-built one from the Gentoo developers. For our purposes, we are opting for the latter.
To install the pre-built Linux kernel, install two things – the installkernel package and the kernel itself – by running the following command:
emerge --ask sys-kernel/installkernel-gentoo sys-kernel/gentoo-kernel-bin
Configuring the fstab
With the kernel installed, populate the fstab file: the file that tells Gentoo on startup which partitions to mount and where. Run this command to edit it:
nano -w /etc/fstab
Indicate all of the partitions you created in fdisk. For example, in a UEFI system, your fstab file could look something like this:
[...] /dev/sda1 /boot/efi vfat defaults 0 2 /dev/sda2 /boot ext2 defaults,noatime 0 2 /dev/sda3 none swap sw 0 0 /dev/sda4 / ext4 noatime 0 1
Setting Up the Root Password
From there, you want to set the root password for the system, as by default, the Stage 3 tarball sets a random password for the root account.
If you do not add a password here, it would be impossible to access it once you reboot. To change the root password, run the following:
passwd
Installing Additional Tools
Once done, install additional third-party tools you may need for your Gentoo system. For example, if you want both Ethernet and wireless connectivity, install the following:
emerge --ask net-misc/dhcpcd net-wireless/iw net-wireless/wpa_supplicant rc-update add dhcpcd default
Further, you can install a system logger if you want to monitor the behavior of your system:
emerge --ask app-admin/sysklogd rc-update add sysklogd default
You can also install the filesystem utilities for the most common filesystems you might interact with:
emerge --ask sys-fs/dosfstools sys-fs/ntfs3g
Installing the Bootloader
From there, the final step in installing Gentoo is the bootloader, a simple program that sits between the BIOS and the Linux kernel. Its function is to instruct the BIOS to load the kernel, which then bootstraps the operating system.
To install the GRUB Bootloader, run the following command:
emerge --ask sys-boot/grub
Once installed, run the following commands to properly initialize and configure GRUB:
grub-install --target=x86_64-efi --efi-directory=/boot/efi grub-mkconfig -o /boot/grub/grub.cfg
After that, all that is left to do is to unmount the USB installer and reboot the system by running the following:
exit cd umount -l /mnt/gentoo/dev{/shm,/pts,} umount -R /mnt/gentoo
Once the system reboots, you will be greeted by your newly installed Gentoo Linux system. From there, start installing basic software and packages to build your own custom Linux distribution.
However, if you are still not sure which desktop environment to install, check out our article on what we think are the best desktop environments on Linux.
Frequently Asked Questions
1. Is there a way to speed up the compile times in Gentoo?
Yes! You can massively speed up the compile times in Gentoo by adding a few values to your “/etc/portage/make.conf” file. For example, set this value to allow your computer to utilize most of its cores during program compilation:
MAKEOPTS="-j4" # You should change this number based on the # amount of CPU cores in your system.
2. My system did not boot up! It showed GRUB, but after booting up, it is just blank.
This could be due to a number of things. One of the most common reasons is that your fstab file was not properly set up. Fix this by plugging in your USB installer and following the steps that we took to chroot to the hard drive.
3. Xorg doesn’t display fonts; is there something wrong?
This is most likely because Xft was not properly installed. When installing programs, you need to add the “Xft” USE flag in your make.conf so that the programs you compile will use X-compatible fonts. To do that, add the following line to your /etc/portage/make.conf:
USE="xft"
Image credit: Unsplash
Our latest tutorials delivered straight to your inbox