August 3, 2022

Upgrade perl or python to newer versions on FreeBSD

FreeBSD text image

The default versions languages like perl or python often get updated in FreeBSD ports as the language versions get updated upstream. The default version of the language mostly lags the upstream version because many other ports depend on it. If you run the default version you have a much better chance that other ports that use perl or python will work correctly. There is however nothing stopping you to run a a newer or older version if it is available in the ports.

At the time of writing there are multiple versions of both perl and python available in the FreeBSD ports tree e.g. perl5.32, perl5.34, perl5.36, python27, python3.7, python3.8, python3.9, python3.10 and python3.11. The default versions are perl5.32 and python3.9.

If you do upgrades to ports you should always have a look at /usr/ports/UPDATING. If you install ports using pre-compiled packages you may not have the ports tree on your system. The UPDATING file is available online at github.com/freebsd/freebsd-ports

Instructions for perl

For perl the upgrade instructions given are the following since 2016, but check the UPDATING file just in case. Substitute your version numbers for 5.20 and 5.24

The default Perl version has been switched to Perl 5.24. If you are using binary packages to upgrade your system, you do not have anything to do, pkg upgrade will do the right thing. For the other people, assuming you are migrating from 5.20 to 5.24, do:

First, add to /etc/make.conf:

DEFAULT_VERSIONS+=  perl5=5.24

Portupgrade users:

portupgrade -o lang/perl5.24 -f lang/perl5.20

You can now remove the DEFAULT_VERSIONS line added earlier from /etc/make.conf

Then you will need to rebuild everything that uses libperl.so, you can do so with:

portupgrade -f `pkg shlib -qR libperl.so.5.20`

If, for some reason, this command fails at one point, it is safe to run it again, it will not rebuild what it already rebuilt, as the ports that have been rebuilt no longer depend on libperl.so.5.20 but on libperl.so.5.24.

Portmaster users:

portmaster -o lang/perl5.24 lang/perl5.20

You can now remove the DEFAULT_VERSIONS line added earlier from /etc/make.conf

Then you will need to rebuild everything that uses libperl.so, you can do so with:

portmaster -f `pkg shlib -qR libperl.so.5.20`

If, for some reason, this command fails at one point, it is safe to run it again, it will not rebuild what it already rebuilt, as the ports that have been rebuilt no longer depend on libperl.so.5.20 but on libperl.so.5.24.

Instructions for python

For python the upgrade instructions given are the following, but check the UPDATING file just in case. Substitute your version numbers for python 3.8 and python 3.9

Note: The default shell for the superuser on FreeBSD is tcsh. The instructions below is for the Bourne shell /bin/sh. They will not work in tcsh, therfore the sh command at the start. Also note the # is the prompt, you mustn’t type it for the commands to work.

The default version of python3 and python was switched to 3.9.

For ports users wanting to keep version 3.8 as default, add DEFAULT_VERSIONS+= python=3.8 to make.conf

The following procedures may ease the upgrade:

For users of pre-build packages:

# sh
# for i in $(pkg query -g %n 'py38-*'); do pkg set -yn ${i}:py39-${i#py38-}; done
# pkg upgrade

For portmaster users:

# sh
# portmaster -o lang/python39 python38
# REINSTALL="$(pkg info -o "*py38*" | awk '{printf "%s ", $2}')"
# pkg delete -f "*py38*"
# portmaster $REINSTALL
# REBUILD=$(pkg query -g "%n:%dn" '*' | grep py3 | grep -v py39 | cut -d : -f 1 | sort -u)
# portmaster $REBUILD
# REBUILD2=$(pkg list | grep python-38 | xargs pkg which | awk '{print $6}' | sort -u)
# portmaster $REBUILD2

Final steps (for pre-built packages & portmaster):

If no longer required, Python 3.8 can be removed via pkg remove python38 and the directory /usr/local/lib/python3.8 can then be deleted afterwards, if not empty.

If you have installed any python packages via PyPI or directly from source you will have to re-install them as well.

© Arnold Greyling 2023