Sam
Sam

Reputation: 2331

Installing Python3.7 from source

I need to use Python3.7, so I followed these instructions to install it

curPath=${pwd}
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
sudo tar xzf Python-3.7.9.tgz
cd Python-3.7.9
sudo ./configure --enable-optimizations
cd ${pwd}

I then tried to run python -V and got

Command 'python3.7' not found, did you mean:

  command 'python2.7' from deb python2.7 (2.7.18-1~20.04)
  command 'python3.9' from deb python3.9 (3.9.0-5~20.04)
  command 'python3.8' from deb python3.8 (3.8.5-1~20.04.2)

Try: sudo apt install <deb name>

Also I tried to run whereis python and got

whereis python
python: /usr/bin/python3.8 /usr/bin/python3.8-config /usr/lib/python3.8 /usr/lib/python2.7 /usr/lib/python3.9 /etc/python3.8 /usr/local/lib/python3.8 /usr/include/python3.8

Upvotes: 2

Views: 6583

Answers (2)

Sam
Sam

Reputation: 2331

With this script, I was able to install python3.7 on ubuntu 18

curPath=${pwd}
sudo apt-get update
sudo apt-get install gcc
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev wget liblzma-dev lzma
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
sudo tar xzf Python-3.7.9.tgz
cd Python-3.7.9
sudo ./configure --enable-optimizations
sudo make
sudo make altinstall
cd ${pwd}

But you should probably just install it with pyenv, here's a script that will install pyenv on linux, and then install python 3.7.9 . This method happens a lot faster, the first installation takes about an hour.

sudo apt-get install gcc build-essential libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev liblzma-dev lzma

curl https://pyenv.run | bash

bashrc="${HOME}/.bashrc"
bash_profile="${HOME}/.bash_profile"
profile="${HOME}/.profile"

checkAndAppend () {
    # Checks if some text is in a file, and if it isn't, it appends that text at the end of the file
    # $1 - The text to insert into a file
    # $2 - A file to insert text into
    if ! grep -q $2 <<< "${1}" ; then 
        echo $1 >> $2
    fi 2>/dev/null
}

checkAndAppend 'export PYENV_ROOT="$HOME/.pyenv"' $profile
checkAndAppend 'export PATH="$PYENV_ROOT/bin:$PATH"' $profile
checkAndAppend 'eval "$(pyenv init --path)"' $profile
checkAndAppend 'eval "$(pyenv init -)"' $bashrc
checkAndAppend 'eval "$(pyenv virtualenv-init -)"' $bashrc
checkAndAppend 'source $HOME/.profile' $bash_profile

RED='\033[0;31m'
printf $RED'
If your $HOME/.profile sources $HOME/.bashrc, then these lines must be inserted into $HOME/.profile before that

    export PYENV_ROOT="$HOME/.pyenv"
    export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init --path)"

'

source $profile
source $bashrc
source $bash_profile

pyenv install 3.7.9

Upvotes: 0

Marcin
Marcin

Reputation: 238209

I adapted your script to work as UserData script on Ubuntu 20.04 instance:

#!/bin/bash

apt update
apt install -y build-essential zlib1g-dev

curPath=${pwd}
cd /usr/src
wget https://www.python.org/ftp/python/3.7.9/Python-3.7.9.tgz
tar xzf Python-3.7.9.tgz
cd Python-3.7.9
#./configure --enable-optimizations
./configure 
make
make install
cd ${pwd}

Upvotes: 1

Related Questions