Easily Install Python 3 in Ubuntu

Easily Install Python 3 in Ubuntu

artistic python battleship

You have a Python script you’d like to use, but you’re not sure if your server has Python installed, or if it’s running the latest version. No problem! We address that common issue in today’s article. Learn how to install Python 3 in Ubuntu easily in this comprehensive guide.

At IOFLOOD, most of the internal tools we create these days are written in Python. We’re happy to share the tips and tricks we use to keep our customer’s dedicated servers running smoothly, whether you’re an IOFLOOD customer or just a curious onlooker.

Python is an incredibly versatile programming language with applications ranging from web development to data science and artificial intelligence. It’s no wonder that Python has become a staple in the software development world. Continue reading to learn step-by-step how to update and install Python on Ubuntu systems.

TL;DR How to install Python 3 for Ubuntu?

sudo apt install python3 is the command that will install your OS’s default version of Python 3 in Ubuntu. Read the rest of the article for tips on getting the latest version, as well as more advanced installation methods.

Checking if Python is Already Installed

Before installing Python on your installation of Ubuntu, it’s a good idea to check if Python is already installed. You can do this by running a simple command in the terminal:

$ python3 --version

If Python is installed, this command will display the version number. For example, you might see something like:

Python 3.8.10

If you see a version number, you’re good to go! Python is already installed on your system.

If you want to make sure you have the latest version, check out https://www.python.org/downloads/ As of March 24, 2023, the latest stable Python release was 3.11.2.

If you’d like to upgrade versions or you don’t have Python installed at all, keep reading, we’ll cover how to install Python in the next section.

Installing Python on Ubuntu

There are several methods to install Python on Ubuntu, including using the APT package manager, Deadsnakes PPA, and installing from source code. We’ll discuss each of these methods in detail below.

Installing Python using APT

To install Python on Ubuntu, you can make use of the convenient APT (Advanced Package Tool) package manager. We recommend you begin by updating your system’s package list with the following command:

$ sudo apt update

Next, install Python using the command below. This command will install the latest version of Python 3 available in the Ubuntu repositories:

$ sudo apt install python3

Once the installation is complete, you can verify the installation by checking the Python version again:

$ python3 --version

Installing Python using Deadsnakes PPA

Another option to install Python on Ubuntu is to use the Deadsnakes PPA (Personal Package Archive). This PPA contains newer versions of Python that may not be available in the official Ubuntu repositories.

To install Python using Deadsnakes PPA, first, add the repository to your system:

$ sudo add-apt-repository ppa:deadsnakes/ppa

If you aren’t able to do this, you may be missing the “software-properties-common” dependency, which you can install with this command and then try again:

$ sudo apt install software-properties-common

Update your package list:

$ sudo apt update

And finally, install the desired Python version. For example, to install Python 3.9, run the following command:

$ sudo apt install python3.9

Remember to replace “3.9” with the version number you want to install. Reminder, the latest Python release version is listed here: https://www.python.org/downloads/

Installing Python from source code

This final method or compiling Python from the raw source code offers the most customization and the latest versions. While installing Python from source code offers flexibility in installing a specific Python version, it may not be ideal for beginners due to its complexity and manual work involved.

First, install the required build dependencies:

$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev

Next, download the Python source code from the official website. Replace “X.Y.Z” with the desired version number. For example, as of March 24, 2023, the latest version was 3.11.2

$ curl -O https://www.python.org/ftp/python/X.Y.Z/Python-X.Y.Z.tar.xz

Extract the downloaded archive:

$ tar -xf Python-X.Y.Z.tar.xz

Change to the extracted directory:

$ cd Python-X.Y.Z

Configure the build and install Python:

$ ./configure --enable-optimizations
$ make -j 4
$ sudo make altinstall

Once the installation is complete, you can verify the installation by checking the Python version:

$ python3 --version

Updating Python to the Latest Version

Keeping your Python installation up-to-date is crucial not only for the latest compatibility with packages and libraries but also for ensuring a secure system. The following are the different ways you can update Python using various installation methods:

Updating Python using APT

If you installed Python using the APT package manager, you can update it by running the following command:

$ sudo apt update
$ sudo apt upgrade

NOTE: This command will update all the packages on your system, including Python. This is generally recommended to make sure that all dependencies are also up to the latest version. If you’d prefer to update only Python, you can instead run this command:

$ sudo apt install --only-upgrade package_name python3

Again, you might want to (or need to) replace “python3” with “python3.X” where X is the version you have installed currently, such as python3.9.

Updating Python using Deadsnakes PPA

If you installed Python using the Deadsnakes PPA, updating Python is as simple as adding the repository, updating your package list, and upgrading your packages:

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt update
$ sudo apt upgrade

Again, please note that this command will update all the packages on your system, including Python.

Updating Python from source code

To update Python installed from source code, the process is the same as the installation process. You’ll need to download the source code of the desired version, extract it, and then execute the configure, make, and make altinstall commands.

You can also remove the old version if it’s no longer necessary. However, some software requires specific Python versions, so you may want to keep multiple versions installed simultaneously to accommodate this.

Running Python on Ubuntu

Python is an immensely popular choice across many industries and applications, ranging from web development to data analysis, and even machine learning. It plays a crucial role in the Linux ecosystem and is often bundled with popular distributions like Ubuntu.

To run Python on Ubuntu, simply open a terminal and type “python3” followed by the desired version number, if applicable:

$ python3

Or:

$ python3.9

This command will start the Python interpreter, where you can run Python code interactively. To run a Python script, use the following command:

$ python3 script.py

Remember to replace “script.py” with the path to your Python script.

More Resources for Python Setups

> Whether you’re a beginner or advanced coder, our Python syntax cheat sheet is an invaluable resource. To enhance your skillsets in Python even further, consider delving into the following learning materials:

1. Python Virtual Environments (venv): Creating Isolated Development Spaces – Learn how to create and manage Python virtual environments using python venv.

2. How to Check Your Python Version: A Quick and Easy Guide – Discover various methods to check the Python version installed on your system.

3. Pip Installation Guide – A step-by-step guide on installing Pip, Python’s recommended package installer.

4. Python Package Installation Tutorial – Learn how to install Python packages to set up your environment effectively.

5. Download and Installation – A direct source for downloading and setting up Python on your machine.

By making use of these resources, you can make Python environment setup and installation a breeze and boost your Python coding efficiency. Remember, a well-organized Python environment can significantly contribute to your productivity and coding comfort.By making use of these resources, you can make Python environment setup and installation a breeze and boost your Python coding efficiency. Remember, a well-organized Python environment can significantly contribute to your productivity and coding comfort.

Conclusion

This blog has dived right into the ins and outs of checking, installing, and updating Python on Ubuntu systems. We’ve covered several installation methods, such as APT, Deadsnakes PPA, and source code installation. With a recent and up-to-date version of Python on your system, the doors are now open for you to explore its diverse applications and immerse yourself in the exciting world of programming.

Do you love servers?

We do! Whether you’re new to programming in Python or you’re an experienced developer, it helps to have a good home to store and run your code. IOFLOOD would be happy to help with this! We offer high performance, reliable dedicated servers that are ideal for startups who want the best possible systems to run their code on.

Email us at sales[at]ioflood.com or view our website at https://ioflood.com to learn more about how an IOFLOOD dedicated server could help with your next big project.