Skip to content

Instantly share code, notes, and snippets.

@hadisfr
Last active January 14, 2024 17:03
Show Gist options
  • Save hadisfr/5c055c2d850cabeecaf51d1b599bd363 to your computer and use it in GitHub Desktop.
Save hadisfr/5c055c2d850cabeecaf51d1b599bd363 to your computer and use it in GitHub Desktop.
a short guide to install python3 and pip3 on Ubuntu Server

Python on Ubuntu

Tricks

Ubuntu

Disable X-Server on Ubuntu Desktop

  • edit /etc/default/grub and change
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

to

GRUB_CMDLINE_LINUX_DEFAULT="text"
  • update GRUB
sudo update-grub
  • tell systemd to not load the graphical login manager
sudo systemctl enable multi-user.target --force
sudo systemctl set-default multi-user.target

To run X-Server again, run startx, or you may need to start lightdm or gdm daemon:

sudo /etc/init.d/lightdm start
sudo /etc/init.d/gdm start

[+]

pip

Upgrading all outdated packages

pip list --format=freeze --outdated | cut -d = -f 1 | xargs pip install --upgrade

Troubleshooting

You may encounter some problems while installing python3 and pip3 on Ubuntu Server. This is a short guide to solve some of these problems.

Ubuntu

Cannot SSH to Ubuntu Desktop.

You should install an ssh daemon like openssh-server to make it possible connect to a computer via ssh.

sudo apt install openssh-server

apt-get update encounters error with appstreamcli.

If you get errors related to appstreamcli or appstreamcli3 on first sudo apt update:

E: Problem executing scripts APT::Update::Post-Invoke-Success 'if 
/usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then 
appstreamcli refresh > /dev/null; fi'
E: Sub-process returned an error code

you may need to purge appstreamcli or appstreamcli3:

sudo apt purge appstream

python

python version which is available in in repo is very old.

You may need to add an external PPA (such as deadsnakes) to install newer python versions on older Ubuntu ditributions. [+]

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7 python3.7-dev

pip

Pip installed from repo doesn't work.

Exception:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/list.py", line 157, in run
    packages = self.get_outdated(packages, options)
  File "/usr/lib/python3/dist-packages/pip/commands/list.py", line 168, in get_outdated
    dist for dist in self.iter_packages_latest_infos(packages, options)
  File "/usr/lib/python3/dist-packages/pip/commands/list.py", line 169, in <listcomp>
    if dist.latest_version > dist.parsed_version
TypeError: '>' not supported between instances of 'Version' and 'SetuptoolsVersion'

Currently, version 9.0.1 of pip3 is in repos of Ubuntu, and it's a buggy version.

You may want to install a newer version of pip3 using get-pip.py. [+]

curl https://bootstrap.pypa.io/get-pip.py | python3

This may be worth a try, too: [+]

pip install --ignore-installed pip

pip3 list gives DEPRECATION format error.

DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.

Create .pip/pip.conf with following context:

[list]
format=columns

Distutils installed packages could not be deleted or updated.

Cannot uninstall '*'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

Distutils installed packages does not provide needed metadata to provide "update" feature. [+] You may use --ignore-installed flag to update them. [+]

Pygobject package could not be updated.

...
Failed building wheel for pycairo
...
Command ['pkg-config', '--print-errors', '--exists', 'cairo >= 1.13.1']
|
Command '('pkg-config', '--print-errors', '--exists', 'gobject-introspection-1.0 >= 1.46.0')' returned non-zero exit status 1.
...
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-tayqn2l3/pycairo/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-jj3i8kig/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-tayqn2l3/pycairo/

Pygobject needs some external dependencies to be installed. [+] Use:

sudo apt install libcairo2-dev libgirepository1.0-dev

Some package could not be updated due to absence of Python.h

    cairo/device.c:30:10: fatal error: Python.h: No such file or directory
     #include <Python.h>
              ^~~~~~~~~~
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Install python3.7-dev.

sudo apt install python3.7-dev

Python venv conflict with pip

root:~# python3.7 -m venv temp
Error: Command '['/root/temp/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1.

It seems theres some confilcts in ubuntu. A trick can be used: [+]

python3.7 -m venv --without-pip .env
source test/bin/activate
curl https://bootstrap.pypa.io/get-pip.py | python
deactivate
source test/bin/activate

mysql_config not found error while installing mysql DBC

OSError: mysql_config not found

Mysql DBC needs mysql_config which should be created by libmysqlclient. [+]

sudo apt-get install libmysqlclient-dev

For recent versions of debian/ubuntu (as of 2018) it is:

sudo apt install default-libmysqlclient-dev

⚠️ Upgrading packages using pip makes server unusable

It seems after upgrading all packages using pip in a Ubuntu 18.04 LTS Digital Ocean server, it becomes completely unusable and doesn't respond to any request.

I either don't know the reason or any way to recover server. Just don't upgrade anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment