<?xml version='1.0' encoding="UTF-8"?>
<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/proj/en/portage/doc/manually-fixing-portage.xml,v 1.19 2011/03/14 19:17:54 zmedico Exp $ -->
<!DOCTYPE guide SYSTEM "/dtd/guide.dtd" [
	<!ENTITY stable_version "2.1.9.42">
]>

<guide link="/proj/en/portage/doc/manually-fixing-portage.xml">

<title>Manually fixing broken portage installations</title>

<author>
  <mail link="genone@gentoo.org">Marius Mauch</mail>
</author>

<abstract>
This document attempts to help people to manually fix a broken sys-apps/portage
installation.
</abstract>

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

<version>0.3</version>
<date>2007-05-31</date>

<chapter>
<title>Manually fixing portage</title>
<section>
<title>Purpose</title>
<body>

<p>
This section will tell you how to manually update/fix your portage installation
in case you can't run <c>emerge sys-apps/portage</c>. While not hard it is
still to be done with great care, so please follow the listed steps exactly
(but apply common sense when necessary).
</p>

</body>
</section>
<section>
<title>Getting a portage tarball</title>
<body>

<p>
The first step to do is to get the tarball of a current portage version. In the
following text we will use <e>portage-&stable_version;</e> as an example (as this is the
current stable version at the time of this writing), please replace that with a
version present in the tree if possible. 
</p>

<table>
<tr><th>Python Version</th><th>Portage Version</th></tr>
<tr>
	<ti>&lt;= Python 2.5</ti>
	<ti>
		<uri link="http://distfiles.gentoo.org/distfiles/portage-2.1.6.tar.bz2">
			portage-2.1.6.tar.bz2
		</uri>
	</ti>
</tr>
<tr>
	<ti>&gt;= Python 2.6</ti>
	<ti>
		<uri link="http://distfiles.gentoo.org/distfiles/portage-&stable_version;.tar.bz2">
			portage-&stable_version;.tar.bz2
		</uri>
	</ti>
</tr>
</table>

<warn>
If your currently installed version of python reported by <c>python -V</c>
is less than 2.6 then you must choose a version of portage that is compatible with 
it. If you have at least python 2.6 then use <e>portage-&stable_version;.tar.bz2</e>. If 
you have python 2.4 or 2.5 then use <e>portage-2.1.6.tar.bz2</e>.
</warn>

<p>
Depending on the exact reason portage doesn't work for you anymore it may still
be possible to use it to fetch the tarball for you, so as a first step please
try to run <c>emerge --fetchonly sys-apps/portage</c>, only if that doesn't
work you have to manually fetch the tarball with:
</p>

<pre caption="Fetching portage tarball with wget">
# <i>wget -P /usr/portage/distfiles http://distfiles.gentoo.org/distfiles/portage-&stable_version;.tar.bz2</i>
</pre>

<p>
After that you should have the tarball available as
<path>/usr/portage/distfiles/portage-&stable_version;.tar.bz2</path>.
</p>

</body>
</section>
<section>
<title>Replacing the installed version</title>
<body>

<p>
The next step is to unpack the tarball to a temporary location, using
<path>/root/portage-recover</path> as example the commands to do that are:
</p>

<pre caption="Unpacking portage tarball">
# <i>cd /root</i>
# <i>mkdir portage-recover</i>
# <i>cd portage-recover</i>
# <i>tar xfj /usr/portage/distfiles/portage-&stable_version;.tar.bz2</i>
</pre>

<p>
After you have done this it's just a matter of replacing the python and bash
files of your existing installation with the ones from the tarball (in most
cases anyway). To do so please run:
</p>

<pre caption="Replacing installed files">
# <i>cd /root/portage-recover/portage-&stable_version;</i>
# <i>rm -rf /usr/lib/portage/*</i>
# <i>cp -R pym bin /usr/lib/portage/</i>
</pre>

<p>
If you are not using Gentoo on FreeBSD then you should remove the sed wrapper
script since it's not needed and it is known to cause problems with old
versions of bash:
</p>

<pre caption="Removing the sed wrapper script">
# <i>rm -f /usr/lib/portage/bin/sed</i>
</pre>

<note>
If you accidently unmerged portage before or lost 
<path>/etc/make.globals</path> for other reasons you should also copy 
<path>cnf/make.globals</path> back into <path>/etc</path>, otherwise
portage might behave in strange ways.
</note>
<note>
If the previous version of portage was less than 2.1 then you should now run
<c>emerge --metadata</c> before continuing to the next step. This is necessary
in order to convert the ebuild metadata to the new format that is used by
portage 2.1 and above. It is okay to run this command even if you are not sure
what the previous version of portage was.
</note>

<p>
Now you should have a working portage install again. To ensure a consistent
system state however you should now run <c>emerge sys-apps/portage</c> again
immediately.
</p>

<p>
If you get a <e>command not found</e> error message when you try to run
<c>emerge</c> you have to recreate the symlink:
</p>

<pre caption="Recreating the emerge symlink">
# <i>ln -sf ../lib/portage/bin/emerge /usr/bin/emerge</i>
</pre>

<p>
If these steps didn't work for you your problem is likely not a broken portage
installation but something else beyond the scope of this document. Please
recheck the <uri link="/proj/en/portage/doc/common-problems.xml">list of common
problems</uri> and also look in <uri
link="http://bugs.gentoo.org">bugzilla</uri> if the problem is reported there.
</p>

</body>
</section>
</chapter>
</guide>
