Right now, I'm doing a security course, and some times, we would need to use some pretty old tools, that depends on on version 2.x of Python, so you could run into problems. How do we solve that, enter a little software called pyenv.
It allows us to run multiple versions of Python, and Pip, without f** things up, so here's a quick and dirty tutorial about it.
This is ONLY tested on Kali, so for anything else, you're on your own..
First of, let's be lazy, and make an install script. Here's a quick one.
#!/bin/bash
# Kali Pyenv installer for multiple python environments
# version 0.1 - 12 - 07 -2022
# By NX <This email address is being protected from spambots. You need JavaScript enabled to view it.> - <https://www.blackdragon.se>
# Bare metal install of pyenv - python multiple environment manager and installer
# Based on https://www.kali.org/docs/general-use/using-eol-python-versions/
IS_ROOT()
{
if [ "$EUID" -ne 0 ]; then
echo "Not installing dependencies, run as root"
echo " "
exit 1
else
echo " "
echo "Running as ROOT - continuing installing dependencies.."
echo " "
fi
}
#check for missing arguments
ARG_CHECK()
{
if [ "$#" -eq 0 ]; then
echo "Missing arguments.. use INSTALL | SETUP | HELP"
echo "INSTALL - Install dependencies and setup pyenv for root"
echo "SETUP - setup pyenv for normal user"
echo "HELP - See pyenv help"
exit 1
fi
}
INSTALL()
{
echo " Installing tools needed..stand by.."
sleep 1
echo " "
apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
}
PYENV()
{
echo "Installing pyenv.."
echo " "
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)"\nfi' >> ~/.zshrc
exec $SHELL
}
HELP()
{
echo " Run with pyenv"
echo " "
echo " To see available python versions, run "pyenv install --list""
echo " To install, use "pyenv install python-version""
echo " "
echo " To switch version globally, use pyenv global python-version"
echo " To switch locally, use "pyenv local python-version""
echo " "
echo "To see python version, set global and "run python -V""
echo " To see pip version, use "python -m pip -V""
}
# Program main logic goes here..
#ARG_CHECK
if [ "$#" -eq 0 ]; then
echo "Missing arguments.. use INSTALL | SETUP | HELP"
echo "INSTALL - Install dependencies and setup pyenv for root"
echo "SETUP - setup pyenv for normal user"
echo "HELP - See pyenv help"
exit 1
fi
if [ "$1" = "INSTALL" ]; then
IS_ROOT
INSTALL
PYENV
fi
if [ "$1" = "SETUP" ]; then
PYENV
fi
if [ "$1" = "HELP" ]; then
HELP
fi
exit 0
Simply paste it into a textfile, save it with an sh extension, and chmod +x.
To install the dependencies we need, run as root, with the parameter INSTALL.
To install for a normal user, run with parameter SETUP, and to see pyenv help, run with HELP.
When that's done, and you got pyenv installed, restart your shellsession, and install some python. To see the versions of Python we can install call pyenv like this.
pyenv install --list
To install a python version, use this
pyenv install <python-version>
# install python 2.7.18
pyenv install 2.7.18
Now, the trick here is that, you can set Python versions global or local. Global is for the entire shell session, local is for the current work-folder we're in.
Note that this does NOT change the values of the systems /usr/bin/python or /usr/bin/python3, only the user who is running pyenv.
So, when you got couple of python versions installed, try to list what python you're on, like this.
pyenv versions
system
* 2.7.18 (set by /home/nx/pytest/.python-version)
3.10.5
So, here on python version 2.7.18. To see what pip version were on, try this
python -m pip -V
pip 19.2.3 from /home/nx/.pyenv/versions/2.7.18/lib/python2.7/site-packages/pip (python 2.7)
So, we're on pip 19.2.3 that fits python 2.7, so were good.
To change version we can do this.
pyenv local 3.10.5
┌──(nx㉿T14)-[~/pytest]
└─$ pyenv versions
system
2.7.18
* 3.10.5 (set by /home/nx/pytest/.python-version)
Now, to confirm that pip also switched, try this.
python -m pip -V
pip 22.0.4 from /home/nx/.pyenv/versions/3.10.5/lib/python3.10/site-packages/pip (python 3.10)
And now, we're running a newer version of pip. Now, the pyenv global command switches the shell to run that version globally, and the local command, makes whatever python version you set, the version for that specific folder. So, now you can run different python versions side by side, in an easy to configure way, so go explore and have fun.
Much Happy Hacking :)