how to install/upgrade to latest python3.9.6
how-to-build- latest version of python-from-source-code from /usr/local/bin/python from root system
Step 1: Download the Source Code
To start, you need to get the Python source code. Python.org makes this fairly straightforward. If you go to the Downloads page, then you’ll see the latest source for Python 3 at the top. Just make sure you don’t grab Legacy Python, Python 2!
When you select the Python 3 version, you’ll see a “Files” section at the bottom of the page. Select Gzipped source tarball and download it to your machine. If you prefer a command-line method, you can use wget to download the file to your current directory:
$ wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz
When the tarball finishes downloading, there are a few things you’ll need to do to prepare your system for building Python.
Step 2: Prepare Your System
There are a few distro-specific steps involved in building Python from scratch. The goal of each step is the same on all distros, but you might need to translate to your distribution if it doesn’t use apt-get:
First, update your package manager and upgrade your packages:
$ sudo apt-get update
$ sudo apt-get upgrade
Next, make sure you have all of the build requirements installed:
# For apt-based systems (like Debian, Ubuntu, and Mint)
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncurses5-dev libncursesw5-dev xz-utils tk-dev
# For yum-based systems (like CentOS)
$ sudo yum -y groupinstall "Development Tools"
$ sudo yum -y install gcc openssl-devel bzip2-devel libffi-devel
It’s fine if you already have some of the requirements installed on your system. You can execute the above commands and any existing packages will not be overwritten.
Now that your system is ready to go, it’s time to start building Python!
Step 3: Build Python
Once you have the prerequisites and the TAR file, you can unpack the source into a directory. Note that the following command will create a new directory called Python-3.8.3 under the one you’re in:
$ tar xvf Python-3.9.6.tgz
$ cd Python-3.9.6
Now you need to run the ./configure tool to prepare the build:
$ ./configure --enable-optimizations --with-ensurepip=install
The enable-optimizations flag will enable some optimizations within Python to make it run about 10 percent faster. Doing this may add twenty or thirty minutes to the compilation time. The with-ensurepip=install flag will install pip bundled with this installation.
Next, you build Python using make. The -j option simply tells make to split the building into parallel steps to speed up the compilation. Even with the parallel builds, this step can take several minutes:
$ make -j 8
Finally, you’ll want to install your new version of Python. You’ll use the altinstall target here to avoid overwriting the system Python. Since you’re installing into /usr/bin, you’ll need to run as root:
$ sudo make altinstall
It might take a while to finish installation. Once it’s done, you can verify that Python is set up correctly.
Step 4: Verify Your Installation
Test that the python3.9 --version command returns the latest version:
$ python3.8 --version
Python 3.9.6
If you see Python 3.9.6, then you’re all set!
If you have some extra time on your hands, you can also run the test suite to make sure everything is working properly on your system.
To run the test suite, type the following command:
$ python3.9 -m test
You’ll probably want to find something else to do for a while, as your computer will be running tests for some time. If all the tests pass, then you can be confident that your brand-new Python build is working as expected!
You’ll probably want to find something else to do for a while, as your computer will be running tests for some time. If all the tests pass, then you can be confident that your brand-new Python build is working as expected!