Skip to main content

Installing Python on Ubuntu

Hello in this tutorial I will guide on how to install Python in ubuntu, if it isn't already installed, or how to update it.

Python is a versatile and powerful programming language that has become a staple in various fields, from web development to data science, machine learning, and beyond. Ubuntu, a popular Linux distribution, comes with Python pre-installed, but it's often a good idea to have the latest version for the newest features and security updates.

In this tutorial, we'll walk through the process of installing Python on Ubuntu step by step. We'll cover different methods, including using the system's package manager, as well as alternative tools for more advanced use cases. Whether you're a beginner looking to get your feet wet or a seasoned developer needing the latest Python features, this guide has you covered.

Prerequisites

Before we begin, ensure you have:

  • An Ubuntu system (we'll be using Ubuntu 20.04 LTS for our examples).
  • Access to a terminal window (you can open this by pressing Ctrl + Alt + T).
  • Basic knowledge of using the command line.

Step 1: Checking the Default Python Installation

Ubuntu typically comes with Python pre-installed. To check the version of Python available on your system, open a terminal and type:.

python --version

You might notice that this command refers to Python 2.x, which has reached its end of life and should not be used for new projects. Instead, we're interested in Python 3.x. To check for Python 3, use:.

python3 --version

You should see an output similar to Python 3.8.5. Make a note of the version number, as it will be useful later on.

Step 2: Updating the Package List

Before installing new software, it's a good practice to update your package list to ensure you have the latest information on available packages and their dependencies:.

sudo apt update

Step 3: Upgrading Existing Packages

Next, upgrade the existing packages to their latest versions:.

sudo apt upgrade

This step ensures that you have the most recent package versions, which can help prevent conflicts during the installation process.

Step 4: Installing Python 3

Even though Ubuntu comes with Python 3, it might not be the latest version. To install the latest stable release of Python 3, along with the development headers and other necessary tools, run:.

sudo apt install python3 python3-dev python3-venv build-essential

This command installs Python 3, the development headers required for compiling Python modules, the venv module for creating virtual environments, and the build-essential package, which includes the GCC compiler and other essential tools for building software from source.

Step 5: Verifying the Installation

After the installation completes, verify it by checking the Python version again:.

python3 --version

You should see the latest Python 3.x version installed on your system.

Step 6: Using Python Virtual Environments

Virtual environments are a best practice in Python development, allowing you to manage dependencies for different projects separately. To create a virtual environment, choose a directory for your project and run:.

python3 -m venv myenv

Replace myenv with the name you wish to give to your virtual environment. To activate the virtual environment, use:.

source myenv/bin/activate

Your command prompt should now indicate that you are working inside the virtual environment (e.g., (myenv) user@host:~$). To deactivate the virtual environment and return to the system's default Python interpreter, simply run:.

deactivate

Step 7: Installing Python via Alternative Methods

For more control over the Python versions and the ability to switch between them, you might consider using tools like pyenv or asdf. Here's how to install Python using pyenv:.

  1. Install pyenv dependencies:.
```bash
sudo apt 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 libffi-dev liblzma-dev python-openssl git

```
  1. Install pyenv itself:.
```bash
curl https://pyenv.run | bash

```
  1. Add pyenv to your shell by adding the following lines to your .bashrc or .zshrc file:.
```bash
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"


```
  1. Install the desired Python version:.
```bash
pyenv install 3.9.1


```
  1. Set the global Python version:.
```bash
pyenv global 3.9.1

```

Step 8: Managing Python Versions with update-alternatives

If you have multiple versions of Python installed, you can manage them using the update-alternatives system:.

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 2

You can then switch between versions using:.

sudo update-alternatives --config python

Step 9: Using pip to Install Python Packages

pip is the package installer for Python. You can use it to install packages from the Python Package Index (PyPI). To install pip for Python 3, run:.

sudo apt install python3-pip

Once installed, you can install Python packages using:.

pip3 install package_name

Replace package_name with the name of the package you wish to install.

Step 10: Keeping Python Updated

To keep Python and pip updated, you can use the following commands:.

sudo apt update
sudo apt install --only-upgrade python3
sudo apt install --only-upgrade python3-pip

If you're using pyenv, you can update to the latest Python version with:.

pyenv install --list | grep "^ *[0-9]"
pyenv install 3.9.1

Troubleshooting Common Issues

  • Permission Issues: If you encounter permission errors when installing packages with pip, you might need to run the command with sudo or configure pip to work without superuser privileges.
  • Path Conflicts: Ensure that your PATH environment variable includes the directory where Python is installed. You can check your PATH with echo $PATH.
  • SSL Certificate Errors: If you encounter SSL certificate errors when using pip, you may need to update your Certificates. Install the ca-certificates package with sudo apt install ca-certificates.

Last word

You now have a solid understanding of how to install Python on Ubuntu, manage different versions, and set up a development environment. Whether you're using the system's package manager or tools like pyenv, you're equipped to start developing in Python on your Ubuntu system. I wish you well.