This post shows students and new users the steps to install the latest version of Python on Ubuntu Linux from the repository or manually build it from the source code.
Python, a versatile and popular general-purpose programming language, can easily be installed on Ubuntu via multiple methods. Whether you’re building an advanced program or creating a simple task in Python, you’ll need Python installed to use it.
With Python, you can write simple or advanced scripts, build and program robots, complicated machinery, develop websites, and many more. Python also lets you work quickly and integrates systems more efficiently.
The latest versions of Python come with many new features, including assignment expression — which assigns values to variables as part of a larger expression, position-only parameters, f-strings support, and many more.
There are many ways to install Python on Ubuntu. Below are two methods one can use to install Python on Ubuntu Linux.
How to install Python from Ubuntu repositories
The quickest way to install Python on Ubuntu is from Ubuntu’s default repositories. Python is available in Ubuntu default repositories, so all one needs to do is simply run the apt-get command to install it.
However, the versions of Python in Ubuntu repositories may not necessarily be the latest. Ubuntu repositories do not always have the latest software builds.
To install Python from the Ubuntu repository, run the commands below
sudo apt update sudo apt install python
After installing Python above, run the commands below to see which versions of Python are installed.
python --version
That should output a similar line as below with the version of Python installed. As you can see, version 2.7.18 is the current build of Python available in Ubuntu.
Python 2.7.18
How to install Python from source code on Ubuntu
If you want to install the latest versions of Python on Ubuntu, you will want to build it from the source code. Installing Python from source code gives you control over which version to install.
Before installing Python from its source code, you must install some required packages to build it from the source. To get these packages installed, run the commands below:
sudo apt update sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
After installing the above packages, download the latest release’s source code from the Python download page using the following wget command.
When writing this post, 3.9.7 is the latest Python version. If you find a later version on the site, you can download it instead.
cd /tmp wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz
After downloading the package, run the commands below, extract the file, and install it.
tar -xf Python-3.9.7.tgz cd Python-3.9.7 ./configure --enable-optimizations
Next, start the building process using the make command. Replace #4 with the number of CPU cores on your system for faster build time.
My machine has 4 CPU cores, so I use the make command with the -j 4 option.
make -j 4
sudo make altinstall
Please do not use the standard make install, as it will overwrite the default system with the python3 binary.
After that, Python version 3.9 should be installed and ready to use.
To test if the latest version of Python (which is 3.9) is installed and ready to use, run the commands below
python3.9 --version
You should see an output similar to the one below:
Python 3.9.7
That’s how you install Python from its source
Conclusion:
This post showed you how to install Python on Ubuntu Linux. Please use the comment form below if you find any errors above or have something to add.
Leave a Reply Cancel reply