Get 50% Discount Offer 26 Days 

Contact Info

1st Floor, Vivek Vihar II, Jagatpura, Jaipur, Rajasthan INDIA

+919636233337

sales{@}nexahost.com

Offer Zone
< All Topics
Print

How to Install Python 3 on Ubuntu 18.04 or 20.04

 

How to Install Python 3 on Ubuntu 18.04 or 20.04

install python 3 on ubuntu
install python 3 on ubuntu

Python is a popular and versatile programming language used for a wide range of applications, including web development, data analysis, scientific computing, and automation. Ubuntu is a widely used Linux distribution, and if you’re running Ubuntu 18.04 or 20.04, you may want to install Python 3 to harness its power. In this article, we’ll guide you through the steps to install Python 3 on your Ubuntu 18.04 or 20.04 system.

Checking for Existing Python Versions

Before installing Python 3, it’s a good idea to check if Python is already installed on your system. Both Ubuntu 18.04 and 20.04 come with Python 3 pre-installed. To check the version, open your terminal and type the following command:

    python3 --version

You should see output that indicates the Python 3 version. For example, if Python 3.6 is installed, you might see something like:

    Python 3.6.9

If you don’t see any output, or if the version is earlier than you’d like, follow the installation steps below to get the desired Python 3 version.

Installing Python 3 on Ubuntu 18.04 or 20.04

To install Python 3 on your Ubuntu 18.04 or 20.04 system, you have a few options. You can use the default package manager, APT, or you can compile Python from source. We’ll cover both methods, starting with the APT package manager, which is the recommended approach.

Method 1: Using APT

1. Update Package List

Start by updating your system’s package list to ensure you have the latest information about available packages. Open a terminal and run:

    sudo apt update

You’ll be prompted to enter your password, as sudo is used to run the command with superuser privileges.

2. Install Python 3

To install Python 3, use the following command:

    sudo apt install python3

You’ll see a list of packages that will be installed, and you’ll be asked to confirm the installation by typing ‘Y’ and then pressing Enter. The package manager will download and install Python 3 for you.

3. Verify the Installation

After the installation is complete, verify that Python 3 is installed and working correctly:

    python3 --version

You should see the Python 3 version you installed.


Method 2: Compiling Python from Source

While using APT is the recommended method, you can also compile Python from source if you have specific requirements or if you need a different version of Python.

1. Install Build Dependencies

You’ll need to install some build dependencies to compile Python. Open a terminal and run the following command:

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

This command installs a set of packages necessary for building Python from source.

2. Download and Extract Python

Next, download the Python source code from the official Python website. Replace X.Y.Z with the version you want to install (e.g., 3.9.7):

    wget https://www.python.org/ftp/python/X.Y.Z/Python-X.Y.Z.tgz

Extract the source code:

    tar -xf Python-X.Y.Z.tgz

Replace X.Y.Z with the version number you downloaded.

3. Configure and Compile

Navigate to the extracted Python source directory:

    cd Python-X.Y.Z

Configure the build process with the following command:

    ./configure --enable-optimizations

This command will optimize Python for your system. It may take a few minutes to complete.

Compile Python:

    make -j8

The -j8 flag indicates that the compilation process should use 8 CPU cores. Adjust the number to match the number of CPU cores on your system.

4. Install Python

After the compilation is complete, install Python:

    sudo make altinstall

The altinstall target ensures that the newly installed Python won’t overwrite the system’s default Python version.

5. Verify the Installation

Check if the installation was successful by running:

    python3.X --version

Replace X with the minor version number you installed. For example:

    python3.9 --version

You should see the version you installed.


Setting Up a Virtual Environment

Once you have Python 3 installed, it’s a good practice to set up a virtual environment for your projects. Virtual environments allow you to isolate your project’s dependencies and avoid conflicts with system packages.

To create a virtual environment, use the venv module, which comes with Python. Here’s how to create a virtual environment and activate it:

1. Create a Virtual Environment

Navigate to the directory where you want to create the virtual environment, and run:

    python3 -m venv myenv

Replace myenv with the name you want to give to your virtual environment.

2. Activate the Virtual Environment

To activate the virtual environment, use the following command:

    source myenv/bin/activate

Your terminal prompt will change to indicate that the virtual environment is active. You can now install packages in this isolated environment without affecting the system-wide Python installation.

3. Deactivate the Virtual Environment

To deactivate the virtual environment and return to the system-wide Python installation, simply run:

    deactivate

Conclusion

In this article, you’ve learned how to install Python 3 on Ubuntu 18.04 or 20.04 using two different methods: APT package manager and compiling from source. The APT method is the recommended and simpler way for most users, while compiling from source gives you more control over the installation.

Additionally, we’ve covered how to set up a virtual environment, which is essential for managing your project-specific dependencies. With Python 3 successfully installed and a virtual environment in place, you’re ready to start developing and running Python applications on your Ubuntu system.

 

Table of Contents