How to Install an Alternate Python Version

Noting these steps here for the next time I need to do this.

First, there are many tools like pyenv that want to manage your python versions for you. I mostly find they create more problems than they solve. As it turns out, they're largely unnecessary. I've found the best option is to manually install a new version from source.

Build Dependencies

Before starting, make sure you have all the necessary build dependencies installed. If you're missing some libraries before you build the new python, you'll find certain features not working and you'll have to rebuild it. This isn't very difficult, but better just to install everything up front. Disk space is cheap.

Python's setup dependencies guide should tell you everything you need to know for your distribution.

Download the source tarball

This should be easy to find on Python's source download page. At the time of this writing the latest version is 3.10.2.

Once downloaded go to your downloads directory and extract the files.

$ cd /path/to/downloads
$ tar -xvvf Python-3.10.2.xz
$ cd Python-3.10.2

This should create a new directory with the same name as the tar file.

./configure, make, build and install

Inside this directory, there should be a file called README.rst. This has all the instructions for building and installing python. It's pretty straightforward. Since we want this to be an alternate version of Python and not overwrite the main system python, be sure to read the section titled Installing multiple versions.

Run these commands in the directory extracted from the tarball:

$ ./configure
$ make
$ make test
$ sudo make altinstall

Note the last line says altinstall. This is what makes it so the installation doesn't overwrite your default python. Very important!

If you installed all the build dependencies correctly above, everything should proceed without any errors. You now have a shiny new version of Python to play with.

Create a virtual environment

You should be able to run the python REPL from the command line.

$ python3.10
Python 3.10.2 (main, Jan 30 2022, 10:58:44) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

If you're following Python best practices, you're probably creating a new virtual environment for every project. It's easy to create one with your new python version. I like to make a catchall environment for short scripts and messing around.

$ python3.10 -m venv py310

This gives me a place to test out random libraries without messing up any other installations. Some basic libraries that are always handy to have are: