This is how Python is usually installed in a development environment to be used with other developers. This is the way I usually installed it with my teams and the way other developers use it. If you are using Python only for your own projects, then maybe there’s no need to go the extra mile.

We are going to use a command called pyenv, which basically allows you to have multiple versions of Python installed in your system. This is useful because you might clone different projects that were developed using a particular version of Python, so you can run them with the right version.

The instructions here are command line based and for Linux, but they are very similar for macOS as well. This doesn’t cover how to install it on Windows.

I’m going to assume a minimum Linux installation, so you can see exactly all that is required.

I also assume you have some knowledge of the command line interface, if you don’t, then checkout my video about getting from zero to hero with the command line.

Installation

macOS

If you are using macOS just go to brew.sh follow the instructions to install Homebrew, which is a package manager for macOS.
Then run:

brew install pyenv

And that’s pretty much it, pyenv will be installed in your system. Ir order to configure it, you can follow the same instructions at the end of the Linux installation where we talk about configuring the shell init script.

Linux

If you want to save time and you don’t care much about the details, you can run this command:

curl https://pyenv.run | bash

Basically it downloads a script and does the rest for you. I found some issues running it in some systems, so I will go ahead and do the manual part, but if it works for you and that’s all you care, feel free to skip to the next section where we learn how to use pyenv.

Here I’m using Debian, this works on any Debian derivative Linux distribution. For other distributions the process is very similar, just make sure you are using the right package manager:

$ sudo apt-get update
$ sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev xz-utils libncursesw5-dev tk-dev libffi-dev liblzma-dev git

Finally we need to clone pyenv repository. Usually it’s cloned in the home directory, but you can technically clone it anywhere you want:

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Configuration

This part is common for macOS and Linux. Basically add the following lines to your shell init script. It could be .bashrc or .zshrc depending on which shell you are using.

if [ -d "$HOME/.pyenv" ] ; then
    export PYENV_ROOT="$HOME/.pyenv"
    command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
fi

That’s it, now when you login again in your machine, you should have fully functional pyenv.

Using pyenv

This part is common for macOS and Linux. At this point, most probably you already have some version of Python installed in your system, but we don’t really care about it at this point because we will manage all of that with pyenv.

$ pyenv versions
$ pyenv versions
* system (set by /home/mc/.pyenv/version)
$

This means that no particular version of Python has been installed. To get a full list of all the versions and flavours of Python that pyenv supports, execute:

pyenv install --list

The list is huge, so let’s just focus for now to install the latest version, that at the time of writing this article the latest version is 3.12.2, just run:

pyenv install 3.12

This will install the latest version of 3.12, in your system. It might take some time depending on how fast is your machine. If you run again:

$ pyenv versions
* system (set by /home/mc/.pyenv/version)
  3.12.2

We can confirm that 3.12.2 has been installed. We want to make it global so we can use it every time we open a terminal. To do that, type:

$ pyenv global 3.12.2

Checking again the versions:

$ pyenv versions
  system
* 3.12.2 (set by /home/mc/.pyenv/version)

The asterisk shows which is the current active version. To confirm that this is really the version of python available, just execute:

$ python --version
Python 3.12.2

This confirms that Python is available and the version that we selected. Go ahead and experiment installing other versions of Python in your system.

Remember to check the other videos in Messy Circuits YouTube channel for more.