Jake_Howard
Jake_Howard

Reputation: 2693

How do I install Python 3 on an AWS EC2 instance?

I'm trying to install python 3.x on an AWS EC2 instance and:

sudo yum install python3

doesn't work:

No package python3 available.

I've googled around and I can't find anyone else who has this problem so I'm asking here. Do I have to manually download and install it?

Upvotes: 191

Views: 254004

Answers (14)

Knight H.
Knight H.

Reputation: 1

For Amazon AL2023 version, use dnf. DNF is the successor to YUM (from AL2).

https://docs.aws.amazon.com/linux/al2023/ug/package-management.html

To install python3:

sudo dnf install python3

Some images should have it already installed:

Package python3-3.9.16-1.amzn2023.0.9.aarch64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

Upvotes: 0

SFM61319
SFM61319

Reputation: 162

I was looking for a solution for Amazon Linux 2023 (a.k.a. AL2023), but none of the answers as of 2024-09-04 addressed it. So, here's my tiny solution.


According to the docs, AL2023 comes with Python 3.9 built into it at /usr/bin/python3.9 (/usr/bin/python3 is a symlink to this executable).

You can reinstall it if needed by running:

sudo dnf install -y python3

Package python3.9 is an alias for package python3.

However, it does not come with pip. You'll have to install pip by running:

sudo dnf install -y python3-pip

Packages python-pip and python3.9-pip are aliases for package python3-pip.

Combined, the command looks like:

sudo dnf install -y python3 python3-pip

You can also install newer Python versions easily. For example, to install Python 3.11 and pip, run:

sudo dnf install -y python3.11 python3.11-pip

Do note that all Python packages installed using pip without sudo are installed to $HOME/.local/. This may not necessarily be on the $PATH (pip will warn you about it - and you can silence pip by passing the --no-warn-script-location flag), but that shouldn't affect your programs when you run them with the correct Python executable.

Upvotes: 0

Jake_Howard
Jake_Howard

Reputation: 2693

Here are the steps I used to manually compile and install python3 for anyone else who wants to do it as it's not super straight forward. EDIT: It's almost certainly easier to use the yum package manager (see other answers).

Note, you'll probably want to do sudo yum groupinstall 'Development Tools' before doing this otherwise pip won't install.

wget https://www.python.org/ftp/python/3.4.2/Python-3.4.2.tgz
tar zxvf Python-3.4.2.tgz
cd Python-3.4.2
sudo yum install gcc
./configure --prefix=/opt/python3
make
sudo yum install openssl-devel
sudo make install
sudo ln -s /opt/python3/bin/python3 /usr/bin/python3
python3 (should start the interpreter if it's worked (quit() to exit)

Upvotes: 51

Bhanuka Gajanayake
Bhanuka Gajanayake

Reputation: 1

try this

sudo yum install python34 python34-pip

Upvotes: 0

Saurav Bhowmick
Saurav Bhowmick

Reputation: 328

Check the packages available

A> amazon-linux-extras

Find the required python package with version from the list

B> sodo amazon-linux-extras enable python<version_available>

C> sudo yum clean metadata && sudo yum install python<version_available>

Upvotes: 3

Pruthvi Chitrala
Pruthvi Chitrala

Reputation: 184

Here is the one command to install python3 on Amazon linux ec2 instance:

$sudo yum install python3 -y

$python3 --version

Python 3.7.6

Upvotes: -4

Light Bringer
Light Bringer

Reputation: 829

On Debian derivatives such as Ubuntu, use apt. Check the apt repository for the versions of Python available to you. Then, run a command similar to the following, substituting the correct package name:

sudo apt-get install python3

On Red Hat and derivatives, use yum. Check the yum repository for the versions of Python available to you. Then, run a command similar to the following, substituting the correct package name:

sudo yum install python36

On SUSE and derivatives, use zypper. Check the repository for the versions of Python available to you. Then. run a command similar to the following, substituting the correct package name:

sudo zypper install python3

Upvotes: -5

TaborKelly
TaborKelly

Reputation: 3585

If you do a

sudo yum list | grep python3

you will see that while they don't have a "python3" package, they do have a "python34" package, or a more recent release, such as "python36". Installing it is as easy as:

sudo yum install python34 python34-pip

Upvotes: 318

nambastha
nambastha

Reputation: 127

Adding to all the answers already available for this question, I would like to add the steps I followed to install Python3 on AWS EC2 instance running CentOS 7. You can find the entire details at this link.

https://aws-labs.com/install-python-3-centos-7-2/

First, we need to enable SCL. SCL is a community project that allows you to build, install, and use multiple versions of software on the same system, without affecting system default packages.

sudo yum install centos-release-scl

Now that we have SCL repository, we can install the python3

sudo yum install rh-python36

To access Python 3.6 you need to launch a new shell instance using the Software Collection scl tool:

scl enable rh-python36 bash

If you check the Python version now you’ll notice that Python 3.6 is the default version

python --version

It is important to point out that Python 3.6 is the default Python version only in this shell session. If you exit the session or open a new session from another terminal Python 2.7 will be the default Python version.

Now, Install the python development tools by typing:

sudo yum groupinstall ‘Development Tools’

Now create a virtual environment so that the default python packages don't get messed up.

mkdir ~/my_new_project
cd ~/my_new_project
python -m venv my_project_venv

To use this virtual environment,

source my_project_venv/bin/activate

Now, you have your virtual environment set up with python3.

Upvotes: 1

Nick T
Nick T

Reputation: 26717

Note: This may be obsolete for current versions of Amazon Linux 2 since late 2018 (see comments), you can now directly install it via yum install python3.

In Amazon Linux 2, there isn't a python3[4-6] in the default yum repos, instead there's the Amazon Extras Library.

sudo amazon-linux-extras install python3

If you want to set up isolated virtual environments with it; using yum install'd virtualenv tools don't seem to reliably work.

virtualenv --python=python3 my_venv

Calling the venv module/tool is less finicky, and you could double check it's what you want/expect with python3 --version beforehand.

python3 -m venv my_venv

Other things it can install (versions as of 18 Jan 18):

[ec2-user@x ~]$ amazon-linux-extras list
  0  ansible2   disabled  [ =2.4.2 ]
  1  emacs   disabled  [ =25.3 ]
  2  memcached1.5   disabled  [ =1.5.1 ]
  3  nginx1.12   disabled  [ =1.12.2 ]
  4  postgresql9.6   disabled  [ =9.6.6 ]
  5  python3=latest  enabled  [ =3.6.2 ]
  6  redis4.0   disabled  [ =4.0.5 ]
  7  R3.4   disabled  [ =3.4.3 ]
  8  rust1   disabled  [ =1.22.1 ]
  9  vim   disabled  [ =8.0 ]
 10  golang1.9   disabled  [ =1.9.2 ]
 11  ruby2.4   disabled  [ =2.4.2 ]
 12  nano   disabled  [ =2.9.1 ]
 13  php7.2   disabled  [ =7.2.0 ]
 14  lamp-mariadb10.2-php7.2   disabled  [ =10.2.10_7.2.0 ]

Upvotes: 86

Putnik
Putnik

Reputation: 6794

As @NickT said, there's no python3[4-6] in the default yum repos in Amazon Linux 2, as of today it uses 3.7 and looking at all answers here we can say it will be changed over time.

I was looking for python3.6 on Amazon Linux 2 but amazon-linux-extras shows a lot of options but no python at all. in fact, you can try to find the version you know in epel repo:

sudo amazon-linux-extras install epel

yum search python | grep "^python3..x8"

python34.x86_64 : Version 3 of the Python programming language aka Python 3000
python36.x86_64 : Interpreter of the Python programming language

Upvotes: 4

Sairam Krish
Sairam Krish

Reputation: 11691

Amazon Linux now supports python36.

python36-pip is not available. So need to follow a different route.

sudo yum install python36 python36-devel python36-libs python36-tools

# If you like to have pip3.6:
curl -O https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py

Upvotes: 4

Realistic
Realistic

Reputation: 1068

As of Amazon Linux version 2017.09 python 3.6 is now available:

sudo yum install python36 python36-virtualenv python36-pip

See the Release Notes for more info and other packages

Upvotes: 21

Russ McFatter
Russ McFatter

Reputation: 492

EC2 (on the Amazon Linux AMI) currently supports python3.4 and python3.5.

sudo yum install python35
sudo yum install python35-pip

Upvotes: 27

Related Questions