Post

Install Custom Python Version

Similarly to Node.js, Python is a popular programming language that is used for a variety of applications. It is known for its simplicity, versatility, and ease of use. Python is also a great language for beginners to learn, as it is easy to read and understand. Python is available on most, if not all, Shared, VPS, and Dedicated Servers. However, some of these servers may be installed as the root user, which can cause issues when installing Python packages. To avoid these issues, you can install Python locally under your Shell user.

So this post will walk you through the steps of installing a custom Python version on your server or any other Linux-based machine.

Don’t want to read through the whole post? You can jump to the TLDR section.

Installation

The first step is to determine what version of Python you want to install. Personally, I’d recommend the most stable version of Python 3 (3.8.10 at the time of writing this post). You can check the latest version of Python on the Python website.

Change to your home directory:

1
$ cd ~

Create a temporary directory:

1
$ mkdir tmp

Change to the temporary directory:

1
$ cd tmp/
Note: To make it easier to not have to go through the list of versions trying to find the downloadable files for the version you want, you can use the following commands to list and download the tarball for the version of your choosing.

List all available versions of Python:

1
$ curl -sKL "https://www.python.org/ftp/python/" | sed -n 's!.*href="\([0-9]\+\.[0-9]\+\.[0-9]\+\)/".*!\1!p' | sort -V | while read -r version; do echo "- $version"; done
Warning: From this point on, I'll be using Python 3.8.10 as an example. However, you can replace this with the version that you have selected/chosen.

Once you’ve determined a version, use the following command to download the tarball:

1
$ wget "https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz"

Extract the Python source code:

1
$ tar -zxvf Python-3.8.10.tgz 

Remove the Python source code archive:

1
$ cd Python-3.8.10/
1
$ ./configure --prefix=$HOME/opt/python-3.8.10
1
$ make
1
$ make install

Add the following to your ~/.bashrc file:

1
export PATH="$HOME/opt/python-3.8.10/bin:$PATH"

Load the changes to your ~/.bashrc file:

1
$ source ~/.bashrc

Check which Python version is being used:

1
$ which python3

Check the Python version:

1
$ python3 --version

Optionally, you can remove the temporary directory:

1
2
$ cd ~
$ rm -rf tmp/

For those who would like to use a virtual environment, take a look at Setting Up Python Virtual Environment.

TLDR

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ cd ~                      # Change to your home directory
$ mkdir tmp                 # Create a temporary directory
$ cd tmp/                   # Change to the temporary directory
$ wget "https://www.python.org/ftp/python/3.8.10/Python-3.8.10.tgz"  # Download the tarball
$ tar -zxvf Python-3.8.10.tgz  # Extract the Python source code
$ rm Python-3.8.10.tgz      # Remove the Python source code archive
$ cd Python-3.8.10/         # Change to the Python source code directory
$ ./configure --prefix=$HOME/opt/python-3.8.10  # Configure the Python source code
$ make                      # Compile the Python source code
$ make install              # Install the Python source code
$ echo 'export PATH="$HOME/opt/python-3.8.10/bin:$PATH"' >> ~/.bashrc  # Add the Python path to your ~/.bashrc file
$ source ~/.bashrc          # Load the changes to your ~/.bashrc file
$ which python3             # Check which Python version is being used
$ python3 --version         # Check the Python version
$ cd ~                      # Change to your home directory
$ rm -rf tmp/               # Remove the temporary directory

Conclusion

That’s it! You’ve successfully installed a custom Python version on your server or any other Linux-based machine.

Happy Coding!

This post is licensed under CC BY 4.0 by the author.