Python is a robust programming language extensively utilized for tasks such as web development, data analysis, automation, and machine learning. While Ubuntu includes Python by default, it frequently features an older iteration.
This tutorial demonstrates the process of installing the most recent version of Python 3 on Ubuntu.
Looking for other platforms?
- Install Python on Windows
- Install Python on Mac
Check Current Python Version
Prior to installation, verify whether Python is already present on your system:
python3 --version
If Python is installed, you'll see output like:
Python 3.10.6
Installing Python on Ubuntu
Method 1: Using APT Package Manager (Recommended)
This technique installs Python directly from the official repositories of Ubuntu.
Step 1: Update Package List
sudo apt update
Step 2: Install Python 3
sudo apt install python3
Step 3: Install pip (Python Package Manager)
sudo apt install python3-pip
Step 4: Verify Installation
python3 --version
pip3 --version
Method 2: Install Specific Python Version from Deadsnakes PPA
The Deadsnakes PPA offers more recent versions of Python that are not currently available in the default repositories of Ubuntu.
Step 1: Add Deadsnakes PPA
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Step 2: Install Specific Python Version
For example, to install Python 3.12:
sudo apt install python3.12
Step 3: Install pip for the New Version
sudo apt install python3.12-distutils
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
Step 4: Verify Installation
python3.12 --version
pip3.12 --version
Method 3: Build from Source (Advanced)
To obtain the most current version or tailored setups, you have the option to compile Python from its source code.
Step 1: Install Dependencies
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
Step 2: Download Python Source
Access https://www.python.org/downloads/source/** to obtain the most recent version, or utilize wget:
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
Step 3: Extract and Navigate
tar -xf Python-3.12.2.tgz
cd Python-3.12.2
Step 4: Configure and Install
./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall
Attention: make altinstall stops the default system python3 from being replaced.
Step 5: Verify Installation
python3.12 --version
Set Python 3 as Default (Optional)
To use python instead of python3:
sudo apt install python-is-python3
Or create an alias in ~/.bashrc:
echo "alias python=python3" >> ~/.bashrc
echo "alias pip=pip3" >> ~/.bashrc
source ~/.bashrc
Test Python Installation
- Open Terminal.
- Start Python interactive shell:
python3
- Run a test:
print("Hello, World!")
Output:
Hello, World!
- Exit the shell:
exit()
Installing Virtual Environment (Recommended)
Virtual environments assist in the independent management of project dependencies.
sudo apt install python3-venv
Create a virtual environment:
python3 -m venv myenv
source myenv/bin/activate
You can now install packages independently of the system Python:
pip install package_name
Deactivate the environment:
deactivate
Uninstalling Python (If Needed)
If you installed via APT:
sudo apt remove python3.x
sudo apt autoremove
Replace 3.x with the version number.
In the event that you performed a source installation, please remove the files located in /usr/local/bin/ and /usr/local/lib/ manually.
Conclusion
Python has been successfully installed on your Ubuntu system. You are now ready to begin developing Python applications, installing libraries, and creating projects.
How to Install Python on Ubuntu 1.FAQs
1. Does Ubuntu come with Python?
Indeed, Ubuntu comes with Python 3 pre-installed. Nevertheless, the version may not be the most recent. You can upgrade it using the previously mentioned methods.
2. What is the latest Python version for Ubuntu?
The most recent stable release of Python can be found at https://www.python.org/downloads/**.. You have the option to install it via the Deadsnakes PPA or compile it from the source.
3. How do I check the Python version on Ubuntu?
Run:
python3 --version
4. What is the difference between python and python3?
-
pythonmay point to Python 2 (deprecated) or not exist. -
python3points to Python 3 (recommended).
You can set python to point to python3 by installing python-is-python3.
5. How do I install pip on Ubuntu?
sudo apt install python3-pip
6. Should I use APT or build from source?
- APT: More convenient, quicker, advised for the majority of users.
- Deadsnakes PPA: Suitable for more recent releases.
- Compile from source: Intended for experienced users requiring tailored setups or the most up-to-date version.
7. How do I update Python on Ubuntu?
Using APT:
sudo apt update
sudo apt upgrade python3
Utilize the Deadsnakes PPA to install the latest version and modify your scripts to incorporate it.
8. What are virtual environments and why should I use them?
Virtual environments establish separate Python environments for various projects, thereby avoiding dependency conflicts. It is essential to utilize virtual environments during project development.