# TIP: Record deps before trying package (for easy removal)

## timeBandit

Suppose you want to try out a new package, and it needs a boatload of dependent packages you don't have installed. If you don't like the package and decide to remove it, how do you identify and clean out those now-unnecessary dependencies? You could review your Portage logs to find packages merged just before the main one, but that's a bit tedious. With a little forethought and a couple of one-liner commands, you can easily reverse such trial installations, without struggling to find all the pieces.

Let's use Banshee (a fairly nice media player) as an example. Before installing it, I previewed the result like every good Gentooer:

```
# emerge -p media-sound/banshee

These are the packages that would be merged, in order:

Calculating dependencies... done!

[ebuild  N    ] dev-dotnet/libgdiplus-1.2.2  USE="gif jpeg tiff -exif"

[ebuild  N    ] dev-lang/mono-1.2.2.1  USE="X nptl"

[ebuild  N    ] dev-dotnet/pe-format-0

[ebuild  N    ] dev-perl/XML-LibXML-Common-0.13

[ebuild  N    ] dev-perl/XML-LibXML-1.60

[ebuild  N    ] dev-dotnet/gtk-sharp-2.8.0  USE="-doc"

[ebuild  N    ] dev-dotnet/art-sharp-2.8.0

[ebuild  N    ] dev-dotnet/gnomevfs-sharp-2.8.0

[ebuild  N    ] dev-dotnet/gnome-sharp-2.8.0

[ebuild  N    ] dev-dotnet/glade-sharp-2.8.0

[ebuild  N    ] dev-dotnet/gconf-sharp-2.8.0

[ebuild  N    ] media-libs/gst-plugins-ugly-0.10.3  USE="-debug"

[ebuild  NS   ] media-plugins/gst-plugins-lame-0.10.4

[ebuild  N    ] media-sound/banshee-0.11.3  USE="encode mad vorbis -aac -boo -daap -debug -doc -flac -ipod -njb"
```

That's quite a lot of baggage to unload if I decide I don't like Banshee. So I did this first, to snapshot the changes:

```
# emerge -p media-sound/banshee | awk '/ebuild/{print "="$4}' >~/media-sound:banshee.deps
```

This recorded the packages I was about to install, in ~/media-sound:banshee.deps:

```
# cat ~/media-sound:banshee.deps

=dev-dotnet/libgdiplus-1.2.2

=dev-lang/mono-1.2.2.1

=dev-dotnet/pe-format-0

=dev-perl/XML-LibXML-Common-0.13

=dev-perl/XML-LibXML-1.60

=dev-dotnet/gtk-sharp-2.8.0

=dev-dotnet/art-sharp-2.8.0

=dev-dotnet/gnomevfs-sharp-2.8.0

=dev-dotnet/gnome-sharp-2.8.0

=dev-dotnet/glade-sharp-2.8.0

=dev-dotnet/gconf-sharp-2.8.0

=media-libs/gst-plugins-ugly-0.10.3

=media-plugins/gst-plugins-lame-0.10.4

=media-sound/banshee-0.11.3
```

If I decide I don't want Banshee after all, I can now remove everything installed with it, using just one command:

```
# emerge -aC $(<~/media-sound:banshee.deps)
```

That's it!

This technique works best if you decide on the fate of such trials fairly soon, before installing other packages whose dependencies overlap. If you can't avoid that, you can use equery and the snapshot file to determine which dependencies to keep. Continuing the example, shortly after installing Banshee I decided to test-drive Beagle as well. The snapshot file for Beagle is:

```
=dev-libs/gmime-2.1.19

=app-misc/beagle-0.2.7
```

I know they are both Mono applications so Beagle must depend on some of the same pieces Banshee pulled in already. The question is, which ones? Easy answer:

```
# for p in $(sed -e 's/=\(.*\)-[0-9].*/\1/' media-sound:banshee.deps)

> do equery -N depends $p | egrep "Searching|beagle"; done

[ Searching for packages depending on dev-dotnet/libgdiplus... ]

[ Searching for packages depending on dev-lang/mono... ]

app-misc/beagle-0.2.7

[ Searching for packages depending on dev-dotnet/pe-format... ]

[ Searching for packages depending on dev-perl/XML-LibXML-Common... ]

[ Searching for packages depending on dev-perl/XML-LibXML... ]

[ Searching for packages depending on dev-dotnet/gtk-sharp... ]

app-misc/beagle-0.2.7

[ Searching for packages depending on dev-dotnet/art-sharp... ]

[ Searching for packages depending on dev-dotnet/gnomevfs-sharp... ]

[ Searching for packages depending on dev-dotnet/gnome-sharp... ]

app-misc/beagle-0.2.7

[ Searching for packages depending on dev-dotnet/glade-sharp... ]

app-misc/beagle-0.2.7

[ Searching for packages depending on dev-dotnet/gconf-sharp... ]

app-misc/beagle-0.2.7

[ Searching for packages depending on media-libs/gst-plugins-ugly... ]

[ Searching for packages depending on media-plugins/gst-plugins-lame... ]

[ Searching for packages depending on media-sound/banshee... ]
```

(Please note the > in the midst of that command is a shell continuation prompt, not a redirection [don't type it]).

So should I decide to keep Beagle but not Banshee, I would manually excise mono, gtk-sharp, gnome-sharp, glade-sharp and gconf-sharp from media-sound:banshee.deps before using it as input to emerge -aC, and I won't need to run revdep-rebuild afterward.

Edit: Fixed typo to create proper package atoms in .deps files.

----------

## Dan

why not use app-portage/udept  and dep --help   :Shocked: 

----------

## Conan

```
emerge --depclean -av
```

much easier.

----------

## timeBandit

 *Conan wrote:*   

> 
> 
> ```
> emerge --depclean -av
> ```
> ...

 

Actually I've always found it rather worthless--it invariably selects many essential packages for removal. The point here was to locate/record only the deps just installed, not all the deps. If --depclean works for you...I'm envious.   :Smile: 

As for dep, IIRC the last time I used it (or maybe its predecessor--it was years ago), it lacked a lot of the power it seems to have now. I'll check it out.

Ah well, I've made a fool of myself in far bigger ways than this.   :Embarassed:   :Smile: 

----------

## red-wolf76

Not to worry, I'd have suggested a notepad and pencil for this.   :Laughing: 

----------

## vandien

hey, nice work. I was doing something similar (though more manually) until I figured out how to make depclean function. So this might help, I dunno.

depclean needs two things to work:

1. Your world file (/var/lib/portage/world) needs to be complete, with all the packages you've emerged and want installed. You can edit this file manually, or probably safer, just "emerge -n package" anything missing from the world file.

2. Your USE flags need to be set correctly (as in, never running "USE=blah emerge package"). Make sure to set up the global USE variable in /etc/make.conf as well as per package USE flags in /etc/portage/package.use

Also, after you remove anything with depclean, it's a very good idea to run revdep-rebuild (from app-portage/gentoolkit), just to be safe. My routine is now (removing -p after each command if everything looks okay):

emerge --sync

emerge -DNupv world

emerge --depclean -p

revdep-rebuild -p

Gentoo has been running smoothly since I got my world and use flags in order and started doing this :)

----------

## defenderBG

@timeBandit: do u really need that stuff? u can awlays take a look at ur log: 

cat /var/log/emerge.log | grep 'emerge (' | less

once there just press '/' and search for the package...

@vandien: i used it once and forgot the revdev-rebuild... i had some nasty time and thus I don't use it anymore

----------

## urcindalo

 *vandien wrote:*   

> My routine is now (removing -p after each command if everything looks okay):
> 
> emerge --sync
> 
> emerge -DNupv world
> ...

 

Same for me. You're absolutely right about the conditions for --depclean to work. I use a slightly different four-step update routine: fis, fiuw, fic, fir. Each one of these commands is an alias entry in my .bashrc:

```
alias fis="sudo eix-sync && sudo update-eix-remote update"

alias fiuw="sudo emerge --update --newuse --deep --ask -vt world"

alias fic="sudo emerge --ask -v --depclean && sudo eclean -d distfiles && sudo eclean -d packages && sudo etcportclean -c111111 -r1 -w1 -d1 -v1 && sudo find /var/log/ -name "*.log" -mtime +1 -exec bzip2 -z '{}' \; && sudo find /var/log -name "*.bz2" -mtime +30 -exec rm '{}' \;"

alias fir="sudo revdep-rebuild -v"
```

Why I chose those alias names is out of the scope of this explanation, but has to be with 'fink' in OS X  :Smile: 

I've never had a problem with this update/maintenance procedure, and the command names to remember are short and convenient. As a benefit, the procedure eliminates packages you don't need anymore as a dependency when you did nothing special to log them. This is what portage is all about, in the first place. For instance, today I got rid of:

```
>>> These are the packages that would be unmerged:

 virtual/jdk

    selected: 1.4.2

   protected: none

     omitted: 1.5.0

 dev-java/blackdown-jdk

    selected: 1.4.2.03-r12

   protected: none

     omitted: none

>>> 'Selected' packages are slated for removal.

>>> 'Protected' and 'omitted' packages will not be removed.

Would you like to unmerge these packages? [Yes/No]
```

that were installed way back in the past on my AMD64 box.

----------

## timeBandit

 *defenderBG wrote:*   

> @timeBandit: do u really need that stuff? u can awlays take a look at ur log: ... and search for the package...

 

Yes of course, and the individual log files in /var/log/portage provide the same info with a simple ls -l. The points of the exercise are (1) not to search at all and (2) save a list for input to emerge to clean the packages.

@vandien and others: I know how to make --depclean work, but it's unusable on my system. I only update world once or twice a year, on average, so depclean rarely understands my dependency graph. Thanks anyway for trying to help.   :Smile: 

----------

## jonnevers

 *timeBandit wrote:*   

>  *defenderBG wrote:*   @timeBandit: do u really need that stuff? u can awlays take a look at ur log: ... and search for the package... 
> 
> Yes of course, and the individual log files in /var/log/portage provide the same info with a simple ls -l. The points of the exercise are (1) not to search at all and (2) save a list for input to emerge to clean the packages.
> 
> @vandien and others: I know how to make --depclean work, but it's unusable on my system. I only update world once or twice a year, on average, so depclean rarely understands my dependency graph. Thanks anyway for trying to help.  

 

while this is a very nice hack, having to use sed (or any utility that is not specifically part of portage )to maintain a consistent portage is just silly.

I propose the next revision of Portage includes this functionality some how... so us users don't need to do manual hacks to keep portage clean and clear.

btw, this made GWN!

----------

## timeBandit

 *jonnevers wrote:*   

> btw, this made GWN!

 

 :Shocked:   :Surprised:   :Cool: 

----------

## ian!

 *jonnevers wrote:*   

> while this is a very nice hack, having to use sed (or any utility that is not specifically part of portage )to maintain a consistent portage is just silly.

 

You might want to have a look at app-portage/demerge and the thread 'demerge - emerge the other ways around' then.

----------

## mikenerone

Thought I'd chime in with my upgrade procedure:

# gsync

...

# gup

...

gsync:

```

#! /bin/bash

termtitle 'Syncing layman overlays'

layman -S

termtitle ''

emerge --sync

```

gup:

```

#! /bin/bash

EMERGELOG=/var/log/emerge.log

TMPFILE=$(mktemp)

STOP=0

# Update

let SIZE=$(stat -c '%s' "${EMERGELOG}")+1

emerge -auvDN world || exit 1

tail --bytes=+${SIZE} "${EMERGELOG}" > ${TMPFILE}

qlop -lC -f ${TMPFILE} | grep -q ' >>> ' || STOP=1

rm -f ${TMPFILE}

[[ $STOP -eq 1 ]] && exit

# Handle configuration changes

termtitle 'Config changes'

dispatch-conf || { termtitle '' ; exit 1 ; }

# Clean stale dependencies

emerge -av --depclean

# Clean PORTDIR/distfiles and PORTDIR/packages

termtitle 'Cleaning distfiles directory'

eclean -d distfiles

termtitle 'Cleaning packages directory'

eclean packages

symlinks -dr /usr/portage/packages

# Check for broken packages

termtitle 'Checking for broken dependencies'

revdep-rebuild -p --nocolor | tee ${TMPFILE}

grep -q '^\[ebuild' <${TMPFILE} || STOP=1

rm -f ${TMPFILE}

if [[ $STOP -eq 1 ]]; then

  termtitle ''

  exit

fi

# Rebuild broken packages

echo ''

read -p 'Do you want me to remerge these packages? [Yes/No] '

if echo $REPLY | grep -Eq '^([yY]|$)'; then

  termtitle 'Rebuilding broken dependencies'

  revdep-rebuild

else

  rm -f ~/.revdep-rebuild.*

fi

termtitle ''

```

termtitle:

```

#! /bin/bash

printf "\e]0;$*\a"

```

BTW, if anyone knows a better way to do anything (such as the way I detect if any packages actually got merged by the "emerge -auvDN world"), do speak up.

Mike Nerone

----------

## desultory

 *mikenerone wrote:*   

> BTW, if anyone knows a better way to do anything (such as the way I detect if any packages actually got merged by the "emerge -auvDN world"), do speak up.

 One way would be to check /var/log/emerge.log.

----------

## STEDevil

 *timeBandit wrote:*   

> Suppose you want to try out a new package, and it needs a boatload of dependent packages you don't have installed. If you don't like the package and decide to remove it, how do you identify and clean out those now-unnecessary dependencies? 

 

paludis media-sound/banshee --uninstall --with-unused-dependencies -p

----------

## Cyker

Someone mentioned a utility called demerge - It's awesome; It sortof takes a snapshot of the state of Portage, so you can try a bunch of stuff, then make it revert back to that state by 'intelligently' unmerging and re-merging anything that was added/removed  :Very Happy: 

----------

