2020-12-15
The purpose of this guide is to allow you to install python 3.x into Ubuntu Linux (or its variants) without affecting the system python installation used for system scripts. We’ll install to:
The reason for opt in our home folder is simple. We won’t need
sudo
permissions and it is easier to deal with multiple
versions depending on our needs.
This article was updated to work with Ubuntu 20.04 and Python 3.9.1 as of 2020-12-15.
NOTE: The instructions are mostly identical for newer version of python. Probably the only changes would be the version numbers
In order to build from source you will need the build essentials. Install them with the following command:
sudo apt install build-essential \
bzip2 \
checkinstall \
libbz2-dev\
libc6-dev \
libgdbm-dev \
libncursesw5-dev \
libncurses5-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
libffi-dev \
libbz2-dev \
sqlite3 \
tk-dev \
libnss3-dev \
liblzma-dev \
zlib1g-dev \
libgdbm-compat-dev \
wget \
xz-utils
Or (This may or may not work - it didn’t for Ubuntu 20.04):
NOTE: You may need to install other dependencies later on in order to properly build python from source.
Create a temporary folder:
Download the source files:
Extract the source code:
Run configure
and specify the prefix path, that is the
path to our target folder in ~/opt
:
NOTE: Set the –prefix option to point to the folder where we want to install python, in our case
~/opt/python_3.9.1
.
NOTE: For faster build times, modify the number of available cores (logical threads) with -j switch.
NOTE: After running make, check to see if it complains about any dependencies that might be missing. Once the dependencies are installed, run make again to confirm. The dependencies can be installed from the Ubuntu repos.
Since we have installed python to ~/opt
, it isn’t on our
path yet. Let’s add it! We’ll need to modify .bashrc
,
adding some aliases so that we can use the newly installed python. I
like setting up python3 as the python to run:
# Python Path
python_path="~/opt/python_3.9.1/bin"
alias python3="$python_path/python3.9"
alias pip3="$python_path/pip3.9"
alias virtualenv="$python_path/virtualenv"
Install virtualenv so that we can create virtual environments easily.
sh {.numberLines startFrom="1"} {.numberLines startFrom="1"} pip3 install virtualenv
NOTE:
type
will show us where a command binary is located or where it points to. This helps to confirm our aliases are setup correctly.
It is straight forward to install python from source. I like this approach because I can control where it is installed and what version I install. It doesn’t depend on your O/S’s package maintainers.