<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sections SYSTEM "/dtd/book.dtd">

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->

<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/hb-install-stage.xml,v 1.126 2012/10/31 18:55:27 swift Exp $ -->

<sections>

<abstract>
Gentoo installs work through a stage3 archive. In this chapter we
describe how you extract the stage3 archive and configure Portage.
</abstract>

<version>14</version>
<date>2012-10-31</date>

<section>
<title>Installing a Stage Tarball</title>
<subsection>
<title>Setting the Date/Time Right</title>
<body>

<p>
Before you continue you need to check your date/time and update it. A
misconfigured clock may lead to strange results in the future!
</p>

<p>
To verify the current date/time, run <c>date</c>:
</p>

<pre caption="Verifying the date/time">
# <i>date</i>
Fri Mar 29 16:21:18 UTC 2005
</pre>

<p>
If the date/time displayed is wrong, update it using the <c>date
MMDDhhmmYYYY</c> syntax (<b>M</b>onth, <b>D</b>ay, <b>h</b>our, <b>m</b>inute
and <b>Y</b>ear). At this stage, you should use UTC time. You will be able to
define your timezone later on. For instance, to set the date to March 29th,
16:21 in the year 2005:
</p>

<pre caption="Setting the UTC date/time">
# <i>date 032916212005</i>
</pre>

</body>
</subsection>
<subsection>
<title>Making your Choice</title>
<body>

<p>
The next step you need to perform is to install the <e>stage3</e> tarball onto
your system. You have the option of downloading the required tarball from the
Internet or, if you booted one of the Gentoo Universal CDs, copy it over from
the disc itself. In most cases, the command <c>uname -m</c> can be used to help
you decide which stage file to download.
</p>

<p>
Minimal CDs and LiveDVDs do not contain any stage3 archive.
</p>

<ul test="not(contains('AMD64 x86', func:keyval('arch')))">
<li><uri link="#doc_chap2">Default: Using a Stage from the Internet</uri></li>
<li>
  <uri link="#doc_chap3">Alternative: Using a Stage from the Universal CD</uri>
</li>
</ul>

</body>
</subsection>
</section>
<section>
<title>Default: Using a Stage from the Internet</title>
<subsection>
<title>Downloading the Stage Tarball</title>
<body>

<p>
Go to the Gentoo mountpoint at which you mounted your filesystems
(most likely <path>/mnt/gentoo</path>):
</p>

<pre caption="Going to the Gentoo mountpoint">
# <i>cd /mnt/gentoo</i>
</pre>

<p>
Depending on your installation medium, you have a couple of tools available to
download a stage. If you have <c>links</c> available, then you can immediately
surf to <uri link="/main/en/mirrors.xml">the Gentoo mirrorlist</uri> and 
choose a mirror close to you: type <c>links http://www.gentoo.org/main/en/mirrors.xml</c>
and press enter.
</p>

<p>
If you don't have <c>links</c> available you should have <c>lynx</c> at your 
disposal. If you need to go through a proxy, export the <c>http_proxy</c> and
<c>ftp_proxy</c> variables:
</p>

<pre caption="Setting proxy information for lynx">
# <i>export http_proxy="http://proxy.server.com:port"</i>
# <i>export ftp_proxy="http://proxy.server.com:port"</i>
</pre>

<p>
We will now assume that you have <c>links</c> at your disposal.
</p>

<p>
Select a mirror closeby. Usually HTTP mirrors suffice, but other protocols are
available as well. Move to the <path><keyval id="release-dir"/></path>
directory. There you should see all available stage files for your architecture
(they might be stored within subdirectories named after the individual
subarchitectures). Select one and press <c>D</c> to download. When you're
finished, press <c>Q</c> to quit the browser.
</p>

<p test="'x86'=func:keyval('arch')">
Most PC users should use the <b><keyval id="stage3"/></b> stage3 archive. All
modern PCs are considered i686. If you use an old machine, you can check the
<uri link="http://en.wikipedia.org/wiki/I686">list of i686-compatible
processors</uri> on Wikipedia. Old processors such as the Pentium, K5, K6, or
Via C3 and similar require the more generic <b>x86</b> stage3. Processors older
than <b>i486</b> are not supported.
</p>

<pre caption="Surfing to the mirror listing with links">
# <i>links http://www.gentoo.org/main/en/mirrors.xml</i>

<comment>(If you need proxy support with links:)</comment>
# <i>links -http-proxy proxy.server.com:8080 http://www.gentoo.org/main/en/mirrors.xml</i>
</pre>

<p>
Make sure you download a <b>stage3</b> tarball - installations using a stage1
or stage2 tarball are not supported anymore.
</p>

<p>
If you want to check the integrity of the downloaded stage tarball, use
<c>openssl</c> and compare the output with the checksums provided on the
mirror. The digests files provide several checksums, each taken with a different
algorithm. The recommended ones are SHA512 and Whirlpool.
</p>

<pre caption="Calculating the integrity checksum of a stage tarball">
<comment>## Calculating the SHA512 checksum</comment>
# <i>openssl dgst -r -sha512 <keyval id="stage3" /></i>
<comment>or</comment>
# <i>sha512sum <keyval id="stage3" /></i>

<comment>## Calculating the Whirlpool checksum</comment>
# <i>openssl dgst -r -whirlpool <keyval id="stage3" /></i>
</pre>

<p>
Then compare the output of these commands with the value registered in the
.DIGESTS files that can be found on the mirrors as well. The values need to
match, otherwise the downloaded file might be corrupt (or the digests file is).
</p>

</body>
</subsection>
<subsection>
<title>Unpacking the Stage Tarball</title>
<body>

<p>
Now unpack your downloaded stage onto your system. We use <c>tar</c> to proceed
as it is the easiest method:
</p>

<pre caption="Unpacking the stage">
# <i>tar xvjpf stage3-*.tar.bz2</i>
</pre>

<p>
Make sure that you use the same options (<c>xvjpf</c>). The <c>x</c> stands for
<e>Extract</e>, the <c>v</c> for <e>Verbose</e> to see what happens during the
extraction process (optional), the <c>j</c> for <e>Decompress with bzip2</e>,
the <c>p</c> for <e>Preserve permissions</e> and the <c>f</c> to denote that we
want to extract a file, not standard input.
</p>

<!-- MIPS uses its own hb-install-stage.xml file, any other arch?
<note>
Some architectures (e.g. MIPS) Installation CDs and boot images rely upon the
<c>tar</c> built into BusyBox which doesn't currently support the <c>v</c>
option. Use the <c>xjpf</c> options instead.
</note>
-->

<p>
Now that the stage is installed, continue with <uri
link="#installing_portage">Installing Portage</uri>.
</p>

</body>
</subsection>
</section>
<section test="not(contains('AMD64 x86', func:keyval('arch')))">
<title>Alternative: Using a Stage from the Universal CD</title>
<subsection>
<title>Extracting the Stage Tarball</title>
<body>

<p>
The stages on the CD reside in the <path>/mnt/cdrom/stages</path> directory. To
see a listing of available stages, use <c>ls</c>:
</p>

<pre caption="List all available stages">
# <i>ls /mnt/cdrom/stages</i>
</pre>

<p>
If the system replies with an error, you may need to mount the CD-ROM first:
</p>

<pre caption="Mounting the CD-ROM">
# <i>ls /mnt/cdrom/stages</i>
ls: /mnt/cdrom/stages: No such file or directory
# <i>mount /dev/cdroms/cdrom0 /mnt/cdrom</i>
# <i>ls /mnt/cdrom/stages</i>
</pre>

<p>
Now go into your Gentoo mountpoint (usually <path>/mnt/gentoo</path>):
</p>

<pre caption="Changing directory to /mnt/gentoo">
# <i>cd /mnt/gentoo</i>
</pre>

<p>
We will now extract the stage tarball of your choice. We will do this with
<c>tar</c>. Make sure you use the same options (<c>xvjpf</c>). The <c>v</c>
argument is optional and not supported in some <c>tar</c> versions.  In the next
example, we extract the stage tarball
<path>stage3-&lt;subarch&gt;-&lt;release&gt;.tar.bz2</path>. Be sure to
substitute the tarball filename with your stage.
</p>

<pre caption="Extracting the stage tarball">
# <i>tar xvjpf /mnt/cdrom/stages/stage3-&lt;subarch&gt;-&lt;release&gt;.tar.bz2</i>
</pre>

<p>
Now that the stage is installed, continue with <uri
link="#installing_portage">Installing Portage</uri>.
</p>

</body>
</subsection>
</section>
<section id="installing_portage">
<title>Installing Portage</title>
<subsection>
<title>Unpacking a Portage Snapshot</title>
<body>

<p>
You now have to install a Portage snapshot, a collection of files that inform
Portage what software titles you can install, which profiles are available, etc.
</p>

</body>
</subsection>
<subsection id="installing_from_Internet">
<title>Download and Install a Portage Snapshot</title>
<body>

<p>
Go to the mountpoint where you mounted your filesystem (most likely
<path>/mnt/gentoo</path>):
</p>

<pre caption="Going to the Gentoo mountpoint">
# <i>cd /mnt/gentoo</i>
</pre>

<p>
Fire up <c>links</c> (or <c>lynx</c>) and go to our <uri
link="/main/en/mirrors.xml">Gentoo mirror list</uri>. Pick a mirror close to
you and open the <path>snapshots/</path> directory. There, download the latest
Portage snapshot (<path>portage-latest.tar.bz2</path>) by selecting it and
pressing <c>D</c>.
</p>

<pre caption="Browsing the Gentoo mirrorlist">
# <i>links http://www.gentoo.org/main/en/mirrors.xml</i>
</pre>

<p>
Now exit your browser by pressing <c>Q</c>. You will now have a Portage snapshot
stored in <path>/mnt/gentoo</path>.
</p>

<p>
If you want to check the integrity of the downloaded snapshot, use
<c>md5sum</c> and compare the output with the MD5 checksum provided on the
mirror.
</p>

<pre caption="Checking integrity of a Portage snapshot">
# <i>md5sum -c portage-latest.tar.bz2.md5sum</i>
portage-latest.tar.bz2: OK
</pre>

<p>
In the next step, we extract the Portage snapshot onto your filesystem. Make
sure that you use the exact command; the last option is a capital <c>C</c>, not
<c>c</c>.
</p>

<pre caption="Extracting the Portage snapshot">
# <i>tar xvjf /mnt/gentoo/portage-latest.tar.bz2 -C /mnt/gentoo/usr</i>
</pre>

</body>
</subsection>
</section>

<section id="compile_options">
<title>Configuring the Compile Options</title>
<subsection>
<title>Introduction</title>
<body>

<p>
To optimize Gentoo, you can set a couple of variables which impact Portage 
behaviour. All those variables can be set as environment variables (using
<c>export</c>) but that isn't permanent. To keep your settings, Portage provides
you with <path>/etc/portage/make.conf</path>, a configuration file for Portage.
It is this file we will edit now. 
</p>

<note>
A commented listing of all possible variables can be found in
<path>/mnt/gentoo/usr/share/portage/config/make.conf.example</path>. For a
successful Gentoo installation you'll only need to set the variables which are
mentioned beneath.
</note>

<p>
Fire up your favorite editor (in this guide we use <c>nano</c>) so we can alter
the optimization variables we will discuss hereafter. 
</p>

<pre caption="Opening /etc/portage/make.conf">
# <i>nano -w /mnt/gentoo/etc/portage/make.conf</i>
</pre>

<p>
As you probably noticed, the <path>make.conf.example</path> file is
structured in a generic way: commented lines start with "#", other lines define
variables using the <c>VARIABLE="content"</c> syntax. The <path>make.conf</path>
file uses the same syntax. Several of those variables are discussed next. 
</p>

</body>
</subsection>
<subsection>
<title>CFLAGS and CXXFLAGS</title>
<body>

<p>
The <c>CFLAGS</c> and <c>CXXFLAGS</c> variables define the optimization flags 
for the <c>gcc</c> C and C++ compiler respectively. Although we define those
generally here, you will only have maximum performance if you optimize these
flags for each program separately. The reason for this is because every program
is different.
</p>

<p>
In <path>make.conf</path> you should define the optimization flags you think
will make your system the most responsive <e>generally</e>. Don't place
experimental settings in this variable; too much optimization can make 
programs behave bad (crash, or even worse, malfunction).
</p>

<p>
We will not explain all possible optimization options. If you want to know
them all, read the <uri link="http://gcc.gnu.org/onlinedocs/">GNU
Online Manual(s)</uri> or the <c>gcc</c> info page (<c>info gcc</c> -- only
works on a working Linux system). The <path>make.conf.example</path> file 
itself also contains lots of examples and information; don't forget to read it 
too. 
</p>

<p test="not(contains('AMD64 x86', func:keyval('arch')))">
A first setting is the <c>-march=</c> or <c>-mcpu=</c> flag, which specifies
the name of the target architecture. Possible options are described in the
<path>make.conf.example</path> file (as comments). A commonly used value is
<e>native</e> as that tells the compiler to select the target architecture of
the current system (the one you are installing on).
</p>

<p test="contains('AMD64 x86',func:keyval('arch'))">
A first setting is the <c>-march=</c> or <c>-mtune=</c> flag, which specifies
the name of the target architecture. Possible options are described in the
<path>make.conf.example</path> file (as comments). A commonly used value is
<e>native</e> as that tells the compiler to select the target architecture of
the current system (the one you are installing on).
</p>

<p>
A second one is the <c>-O</c> flag (that is a capital O, not a zero),
which specifies the <c>gcc</c> optimization
class flag. Possible classes are <c>s</c> (for size-optimized),
<c>0</c> (zero - for no optimizations), <c>1</c>, <c>2</c> or even <c>3</c> for more
speed-optimization flags (every class has the same flags as the one before, plus
some extras). <c>-O2</c> is the recommended default. <c>-O3</c> is known to
cause problems when used system-wide, so we recommend that you stick to
<c>-O2</c>.
</p>

<p>
Another popular optimization flag is <c>-pipe</c> (use pipes rather than
temporary files for communication between the various stages of compilation).
It has no impact on the generated code, but uses more memory. On systems with
low memory, gcc might get killed. In that case, do not use this flag.
</p>

<p>
Using <c>-fomit-frame-pointer</c> (which doesn't keep the frame pointer in a
register for functions that don't need one) might have serious repercussions on
the debugging of applications.
</p>

<p>
When you define the <c>CFLAGS</c> and <c>CXXFLAGS</c>, you should combine
several optimization flags. The default values contained in the stage3 archive
you unpacked should be good enough. The following example is just an example:
</p>

<pre test="not(contains('AMD64 arm', func:keyval('arch')))" caption="Defining the CFLAGS and CXXFLAGS variable">
CFLAGS="<keyval id="CFLAGS"/>"
<comment># Use the same settings for both variables</comment>
CXXFLAGS="${CFLAGS}"
</pre>

<pre test="func:keyval('arch')='AMD64'" caption="Defining the CFLAGS and CXXFLAGS variable">
CFLAGS="<keyval id="CFLAGS"/>"   <comment># Intel EM64T users should use -march=core2</comment>
<comment># Use the same settings for both variables</comment>
CXXFLAGS="${CFLAGS}"
</pre>

<pre test="func:keyval('arch')='arm'" caption="Defining the CFLAGS and CXXFLAGS variable">
CFLAGS="<keyval id="CFLAGS"/>   <comment># Be sure to change -march to match your CPU type</comment>
<comment># Use the same settings for both variables</comment>
CXXFLAGS="${CFLAGS}"
</pre>

<note>
You may also want to view the <uri
link="/doc/en/gcc-optimization.xml">Compilation Optimization Guide</uri> for
more information on how the various compilation options can affect your system.
</note>

</body>
</subsection>
<subsection>
<title>MAKEOPTS</title>
<body>

<p>
With <c>MAKEOPTS</c> you define how many parallel compilations should occur when
you install a package. A good choice is the number of CPUs (or CPU cores) in
your system plus one, but this guideline isn't always perfect.
</p>

<pre caption="MAKEOPTS for a regular, 1-CPU system">
MAKEOPTS="-j2"
</pre>

</body>
</subsection>
<subsection>
<title>Ready, Set, Go!</title>
<body>

<p>
Update your <path>/mnt/gentoo/etc/portage/make.conf</path> to your own preference
and save (<c>nano</c> users would hit <c>Ctrl-X</c>). You are now ready to continue
with <uri link="?part=1&amp;chap=6">Installing the Gentoo Base System</uri>.
</p>

</body>
</subsection>
</section>
</sections>
